jQuery(document).ready(function($){
    
    var ease_ = "easeOutExpo";
    var dur_ = 350;
    
    $('.listingCategory .showInfo').click(function(e){
        var infomask = $('.catDescriptionMask',$(this).parent())
        var height_ = $('.catDescription',$(this).parent()).height();
        if(infomask.height()==0){
            infomask.animate({height:height_},{duration:dur_,easing:ease_}); 
            $(this).html('скрий инфото')
        } else {
            infomask.animate({height:0},{duration:dur_,easing:ease_}); 
            $(this).html('допълнителна информация')
        }
        e.preventDefault();
    })

    $("#nav ul").lavaLamp({ fx: "easeOutExpo", speed: 500 ,target:'a'})
    
    //$(".searchHolder .input").Watermark($(this).val());
    $(".searchHolder .input").each(function(){
    	$(this).Watermark($(this).val());
    });
    
    $("#email_input").each(function(){
    	$(this).Watermark($(this).val());
    });
    
    buttonOverState('.blockBulletin .submit_btn', 'hov');
    buttonOverState('.searchHolder .subm', 'hov');
	buttonOverState('.registrationStep2 .input_submit_container .inputSubmit', 'inputSubmitOver');
    buttonOverState('.commonInputSubmit_93', 'commonInputSubmit_93_Over');
    buttonOverState('.commonInputSubmit_113', 'commonInputSubmit_113_Over');
    buttonOverState('.commonInputSubmit_133', 'commonInputSubmit_133_Over');
    buttonOverState('.commonInputSubmit_69', 'commonInputSubmit_69_Over');
	
	//button hover
	$('.buttonDark,.buttonLight').hover(function(){$(this).addClass('buttonOver')},function(){$(this).removeClass('buttonOver')})
	
	// Set captcha by JS
	$("input[name='cap_prot']").val('1');
	
	$(".teaser").cycle({
		    fx:     'fade',
		    speed: 800,
		    timeout: 6000, // > 0(miliseconds) to scroll automaticly, 0 for manual scroll
		    pause: false
	});
	
	//var LNmt = $('.boxPromo .shapeTop').height() - ($('.accentHome').height()-($('.accentHome h2').height()+15)) - $('.lastNewslist').height() + 5;
	//$('.lastNews').css('margin-top',LNmt);
	
	
	$('.mapHolder iframe').width(413);
	$('.mapHolder iframe').height(262);
	
	if(typeof(products_rows)=='number'){
    	for(var i = 1;i<products_rows+1;i++){
    	    var mht = 0;
    	    var mhd = 0;
    	    $('.blockCatalogue div[class ~= "row_'+i+'"]').each(function(){
    	        if($('.itemTitle',$(this)).height()>mht)mht = $('.itemTitle',$(this)).height();
    	        if($('.itemDescription',$(this)).height()>mhd)mhd = $('.itemDescription',$(this)).height();
    	    })
    	    $('.row_'+i+' .itemTitle').height(mht);
    	    $('.row_'+i+' .itemDescription').height(mhd)
    	}
	}
	
	dynamicImageFrame();
	
	hs.registerOverlay({
		thumbnailId: 'reference_thumb',
		html: '<div class="closebutton"	onclick="return hs.close(this)" title="Close"></div>',
		position: 'top right',
		fade: 2 // fading the semi-transparent overlay looks bad in IE
	});
	
	//equalizeFileNamesWidth();
	
	$('#brandFilters').jcarousel({
		//auto: 4, // seconds
		scroll: 4,
		//wrap: 'last',
		initCallback: carousel_brands_initCallback
	});
	
	menuLeftHeadingCollapse();
	
	
	// 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, 'Грешка');
	}
	
});

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 clas 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 onBefore(){}
function onAfter(){}


function dynamicImageFrame()
{
	$('.userText .imageHolder').each(function(){
		
		// This doesn't work on paragraphs in FF3.5!!!
		$(this).parent().css('clear', 'both').css('marginTop', '15px'); // fix for FF when using multiple images on a page. This dont work: .imageHolder { clear: both; }. Require the parent to have clear: both.
		
		if($('img', this).width()>145){
			$(this).width( $('img', this).width()+12 );
		}
		else {
			if($('img', this).width()>0) {
				$(this).width(157);
			} else {
				$(this).width( $('iframe', this).width()+12 );
			}
		}
		
	});
}

function equalizeFileNamesWidth()
{
	// Equalize ".tech_specs_list_content .icon Width"
	$('.tech_specs_list_content .item').each(function(){
		var highest = 0;
		$('.iconsDownload .icon a', this).each(function() {
			//alert( $(this).height() );
			var elem_height = $(this).width();
			if(elem_height>highest) {
				highest = elem_height;
			}
		});
		//$('.iconsDownload .icon a', this).width(highest); // use min-height except ie6
		$('.iconsDownload .icon', this).each(function() {
			//alert( $(this).height() );
			var elem_height = $('a', this).width();
			if(highest-elem_height>0) {
				//alert($(this).width());
				
				// VAR 1
				//$(this).width(highest + 65); //65px right padding of the A tags
				
				// VAR 2
				//$('a', this).css('marginLeft', highest-elem_height); //ie6/7 - somtimes the text goes to more than 1 row, even if it really short ("the file"). We may add 2-3 px error to handle this unexpected behaviuor.
				var temp = highest-elem_height;
				if( temp>3 ) {
					temp = temp-3;
				}
				else {
					//skip update - do not change margin
					temp = 0;
				}
				//$('a', this).css('marginLeft', temp); // used when aligning right
				//$('a', this).css('marginRight', temp); // equalize .icon width
			}
		});
		
	});
	// END Equalize ".GsmBoxRow"
}

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 menuLeftHeadingCollapse()
{
	if( $('#menu_heading_collapsed').length > 0 ) {
		//alert('#menu_heading_collapsed exists');
		$('#menu_left_products').hide();
		
		$('#menu_heading_collapsed').addClass('menu_heading_expand_over');
		$('#menu_heading_collapsed').bind('click', function(){
			$('#menu_left_products').show();
			
			$(this).removeClass('menu_heading_expand_over').unbind('click');
		});
	}	
}




