//gallery variables - 3 sizes images!
var gal_size_middle = '372x279';
var gal_size_preview = '700x700';


hs.graphicsDir = '/images/highslide/graphics/';
hs.align = 'center';
hs.transitions = ['expand', 'crossfade'];
hs.fadeInOut = true;
hs.dimmingOpacity = 0.8;
hs.outlineType = 'rounded-white';
hs.captionEval = 'this.a.title'; //this.thumb.alt, this.thumb.title, this.a.title //Given the captionEval option is set to 'this.img.alt', the caption is grabbed from the alt attribute of the thumbnail image.
hs.marginBottom = 105; // make room for the thumbstrip and the controls
hs.numberPosition = 'caption';
hs.blockRightClick = true;
hs.showCredits = false;
//hs.lang.number = '%1 / %2';
//hs.lang.loadingText = 'Зарежда се...';

// lang = BG;
if( js_lang == 'bg' ) {
	hs.lang = {
		cssDirection: 'ltr',
		loadingText : 'Зарежда се...',
		loadingTitle : 'Кликнете за да се откажете',
		focusTitle : 'Кликнете за да прегледате',
		fullExpandTitle : 'Покажи в актуална големина (клавиш F)',
		previousText : 'Предишен',
		previousTitle : 'Предишен (стрелка наляво)',
		nextText : 'Следващ', 
		nextTitle : 'Следващ (стрелка надясно)',
		moveText : 'Премести',
		moveTitle : 'Премести',
		closeText : 'Затвори', 
		closeTitle : 'Затвори (клавиш ESC)', 
		resizeTitle : 'Оразмери',
		playText : 'Слайдшоу',
		playTitle : 'Стартирай слайдшоу (клавиш Spacebar)',
		pauseText : 'Пауза',
		pauseTitle : 'Пауза на слайдшоу (клавиш Spacebar)',
		fullExpandText : '1:1',
		number: 'Снимка %1 от %2',
		restoreTitle : 'Кликнете за да затворите картинката, кликнете и местете мишката за да преместите. Използвайте стрелките за предишен и следващ.'
	};
}

// gallery config object
var hs_config_1 = {
	slideshowGroup: 'hs_group_1_gallery',
	//outlineType: 'glossy-dark',
	dimmingOpacity: 0.8,
	align: 'center',
	transitions: ['expand', 'crossfade'],
	fadeInOut: true,
	//wrapperClassName: 'borderless floating-caption', // the numbering/caption are outside the image (on the background)
	wrapperClassName: 'highslide-white',
	marginLeft: 100,
	marginBottom: 80,
	numberPosition: 'caption'
};
var hs_config_2 = {
	wrapperClassName: 'highslide-white',
	outlineType: 'rounded-white',
	dimmingOpacity: 0.75,
	transitions: ['expand', 'crossfade'],
	numberPosition: 'null', // options: caption, heading, null // disable for the sketch
	captionEval: 'this.a.title',
	align: 'center'
};

// Add the slideshow providing the controlbar and the thumbstrip
hs.addSlideshow({
	slideshowGroup: 'hs_group_1_gallery', // list of groups for slideshow. If not specified - all links are galleries!
	interval: 5000,
	repeat: false,
	useControls: true,
	overlayOptions: {
		className: 'text-controls',
		position: 'bottom center',
		relativeTo: 'viewport',
		offsetY: -60
	},
	thumbstrip: {
		position: 'bottom center',
		mode: 'horizontal',
		relativeTo: 'viewport'
	}
});
/*
// no big image! and no trigger click!
	$('.content_gallery_view .itemImage a').bind('click', function(event){
		event.preventDefault();
		$('.content_gallery_view .imagesList a:eq(0)').trigger('click');
	});
*/


var load_done = false;

jQuery(document).ready(function($){
	
	// Center the top menu
	var mrgleft = ($('#main-nav').parent().width() - $('#main-nav').width())/2;
	$('#main-nav').css('margin-left',mrgleft);
	
	// Set captcha by JS
	$("input[name='cap_prot']").val('1');
	
	// Input text auto-suggest
	$('.buletin-form .input-back input, .block-search input').each(function(){
		$(this).watermark( $(this).val(), {'useNative': false} );
	});
	
	buttonOverState('.block-search .subm-btn', 'subm-btn-hover');
	buttonOverState('.subm-btn', 'subm-btn-hover');
	buttonOverState('.divInputSubmit', 'subm-btn-hover');
	
	jQuery('.carousel').jcarousel({
        // Configuration goes here
    });
    
    $(".carousel-small a").hover(function() {
		var temp_old_url = $(this).attr('href');
		//alert(temp_old_url);
		//Replace preview with medium size!
		var temp_new_url = temp_old_url.replace(gal_size_preview, gal_size_middle);
		//alert(temp_new_url);
		
		
		//CK if the image is current - do not animate!
		var temp_current_url = $('.prod-img img').attr('src');
		if(temp_current_url==temp_new_url) {
			//skip
		}
		else {
			// Change the href of the link:
			//$('.prod-img a').attr('href', $(this).attr('href') );
			$('.prod-img').attr('href', temp_old_url );
			
			//... then animate image...:
			$('.prod-img img').animate(
				{
					opacity: 0.1
				}
				,200
				,function(){
					$(this).attr('src', temp_new_url).animate(
						{
							opacity: 1.0
						}
						,500
					);
				}
			);
		}
		
		
		
	});
	/*
	$(".zoom-icon").click(function (event) {
		event.preventDefault(); // If not set on click you'll go to default href
		var i = 0;
		$(".carousel-small a").each(function () {			
			if ($(this).attr('href') == $('.prod-img').attr('href')) {
				if(i==0){
					$(this).trigger('click');
					i++;
				}
			}
			else {
			}
		});
	});
	*/
	/////// Highslide gallery + trigger click events
	// Zoom icon - trigger click on big image
	$(".zoom-icon").bind('click', function(e){
		e.preventDefault();
		$('.one-product .prod-image a.prod-img').trigger('click');
	});
	// big image triggers click of one of the gallery (href based)
	$(".one-product .prod-image a.prod-img").bind('click', function(e){
		e.preventDefault();
		var this_href = $('.one-product .prod-image a.prod-img').attr('href');
		//alert(this_href);
		$(".carousel-small a").each(function () {
			if( $(this).attr('href') == this_href ) {
				$(this).trigger('click');
				
				//break;
				return false; // breaking the jquery each() loop!
				//http://api.jquery.com/jQuery.each/ - We can break the $.each() loop at a particular iteration by making the callback function return false.
			}
		});
		//$('.one-product .prod-image a.prod-img').trigger('click');
	});
	
	// products list - equalize heights
	equalize_row_titles('.products-list .items-row', '.list-item .item-title');
	equalize_row_titles('.products-list .items-row', '.list-item .item-description');
	
	
	// Teaser
	$('#sequence_1').cycle({
		activePagerClass: 'selected',
		//fx:     'scrollUp',
		speed:  1000,
		timeout: 4500,
		pager:  '#nav_sequence_1',
		updateActivePagerLink: function(pager,currSlide,clsName){
			//console.log(pager);
			//console.log(currSlide);
			//console.log(clsName);
			var count = 0;
			$(pager).find('a').each(function(){
				count++;
				//console.log( this );
				//console.log(count);
				//console.log(currSlide);
				$(this).removeClass(clsName);
				if( (count-1)==currSlide ) {
					$(this).addClass(clsName);
				}
			});
		},
		pause: 1,
		pauseOnPagerHover: 1,
		pagerEvent:    'click.cycle',
		fastOnEvent: 300, // in ms
		pagerAnchorBuilder: function(idx, slide) {
			// return selector string for existing anchor
			//return '#nav_sequence_1 li:eq(' + idx + ') a';
			
			//var temp = '#nav_sequence_1 div:eq(' + idx + ')';
			//alert(temp);
			
			return '#nav_sequence_1 a:eq(' + idx + ')';
		}
	});
});



/* OnOver - add class for IE - Should be used on elements different from Links */
function buttonOverState(selector, className)
{
	/* Hover over the login button */
	$(selector).hover(function(){
		$(this).addClass(className);
	},
	function(){
		$(this).removeClass(className);
	});
}

function equalize_row_titles(selector_row, selector_title)
{
	$(selector_row).each(function(){
		// itemTitle
		var highest = 0;
		$(selector_title, this).each(function() {
			//alert( $(this).height() );
			var elem_height = $(this).height();
			if(elem_height>highest) {
				highest = elem_height;
			}
		});
		$(selector_title, this).height(highest); // use min-height except ie6
	});
}

