var load_done = false;
var sequence_1_timeout = '';

jQuery(document).ready(function($){
	
	buttonOverState('.contactForm .inputSubmit', 'inputSubmitOver');
	
	//call - catch also the last row
	equalize_row_titles('.content_home', '.blockPromoProducts, .blockNewProducts, .blockNewsLatest'); //equalize_product_articles_titles
	
	$('#jcarousel-brands').jcarousel({
		//auto: 4, // seconds
		scroll: 8,
		//wrap: 'last',
		initCallback: carousel_brands_initCallback
	});
	
	$('.productDetails .itemImage a.itemImageImg, .productDetails .itemGallery a').lightBox({
		imageLoading:			'/images/lightbox/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
		imageBtnPrev:			'/images/lightbox/lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
		imageBtnNext:			'/images/lightbox/lightbox-btn-next.gif',			// (string) Path and the name of the next button image
		imageBtnClose:			'/images/lightbox/lightbox-btn-close.gif',		// (string) Path and the name of the close btn
		imageBlank:				'/images/lightbox/lightbox-blank.gif',			// (string) Path and the name of a blank image (one pixel)
		txtImage: '',
		txtOf: ' / '
	}); //a[rel*=lightbox]
	
	// Zoom icon
	$(".trigger_image_click").bind('click', function(e){
		e.preventDefault();
		$('.productDetails .itemImage a.itemImageImg').trigger('click');
	});
	
	
	
	
	bindCarouselBrandsEffect();
	
	// Keep these on bottom!!!
	var success_message=$("#success_message").html();
	if (success_message != '' && success_message != null){
	   return create({modal: true}, success_message, 'Благодарим Ви');
	}
	var error_message=$("#error_message").html();
	if (error_message != '' && error_message != null){
	   return create({modal: true}, error_message, 'Грешка');
	}
	
	/*
	$('#share').click(function(){
		createModal();
		return false;
	});
	*/
	
	
	
	// Teaser
	$('#sequence_1').cycle({
		fx:      'fade',
		speed:   500,
		timeout: 4000,
		pager:   '#nav_sequence_1',
		pause:   1,
		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 div:eq(' + idx + ')';
			//return '<a href="#">&nbsp;</a>';
		}
	});
	
    // Here an another option - pause/stop sliding on HOVER on the buttons!
    /*
	$("#nav_sequence_1 div").hover(
		function () {
			$("#sequence_1").cycle('pause');
		}, 
		function () {
			if( !sequence_1_timeout ) { // not working!!!
				$("#sequence_1").cycle('resume');
			}
			// else - let the timer resume (if needed)!
		}
	);
	*/
	
	// Here an option - stop this cycle! after displaying selected slide
	$("#nav_sequence_1 div").click(function () { 
		$('#sequence_1').cycle('pause');
		
		// Resume sliding after NN miliseconds - timer
		var sequence_1_temp = function(){
			$("#sequence_1").cycle('resume');
		};
		
		if( sequence_1_timeout ) { // not working as expected!? May be we need a function to check this variable, not inline as a var at runtime!!!
			clearTimeout(sequence_1_timeout);
		}
		//var sequence_1_timeout = ''; // this should be defined before (outside) document ready
		var sequence_1_timeout = setTimeout(sequence_1_temp, 5000); // if we have 3 clicks before 5000 ms, the Timeout (5000ms) after the first click will start the cycle. But we need the timeout after the 3-rd click to start the cycle!
    });
    
    autoCenterTeaserButtons();
    // END Teaser
    
});

function autoCenterTeaserButtons()
{
	var container_width = 439;
	var buttons_width = $('#nav_sequence_1').width();
	//alert(buttons_width);
	var temp = (container_width - buttons_width) / 2;
	temp = Math.floor(temp);
	
	$('#nav_sequence_1').css('paddingLeft', temp);
}

function createModal(){
    $('#contactFormSendRequest').modal({
		opacity:70,
		overlayCss: {backgroundColor:"#000"},
		overlayClose:true,
		closeHTML: "<a class='modal_1_ButtonClose' href='#'></a>"

	});
	return false;
}

function create(options, custom_text, custom_title) {
	options = $.extend({title: custom_title}, options || {});
	var dialog = new Boxy('<div style="min-width: 300px; min-height: 73px;"><p>'+custom_text+'</p></div>', options);
	return false;
}


/* 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
	});
}


function carousel_brands_initCallback(carousel)
{
	// Disable autoscrolling if the user clicks the prev or next button.
	carousel.buttonNext.bind('click', function() {
	    carousel.startAuto(0);
	});
	
	carousel.buttonPrev.bind('click', function() {
	    carousel.startAuto(0);
	});
	
	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
	    carousel.stopAuto();
	}, function() {
	    carousel.startAuto();
	});
}

function bindCarouselBrandsEffect()
{
	$(".blockBrandsScroller li").each(function(){
		// Hide all Hovers
		$(".d2", this).hide();
		
		// Bind Over event
		$("a", this).hover(
			function(){
				$(".d1", this).hide();
				$(".d2", this).show();
			},
			function(){
				$(".d2", this).hide();
				$(".d1", this).show();
			}
		);
		
	});
}


