var jquery_imgs_a = "#topper a";
var fast_open = false;
var q_has_changed = false;

$(function(){


	//    (    - jquery data cache)
	$(jquery_imgs_a + " img").each(function() {
		rollsrc = $(this).attr("src");
		if ($("body").data(rollsrc) == undefined) {
			rollON = rollsrc.replace(/-off/ig,"-over");
			$("body").data(rollsrc, "1");
			$("<img>").attr("src", rollON);
		}
	});


	//     GIFs
	$(jquery_imgs_a).each(function(){
		
		var imgsrc = $(this).children("img").attr("src");
		
		$(this).mouseover(function(){
			matches = imgsrc.match(/-over/);
			
			// don't do the rollover if state is already ON
			if (!matches) {
				imgsrcON = imgsrc.replace(/-off/ig,"-over"); // strip off extension
				$(this).children("img").attr("src", imgsrcON);
			}
		});
		
		$(this).mouseout(function(){
			$(this).children("img").attr("src", imgsrc);
		});
	});


	$("#photoslides").cycle({ 
		timeout:       3000,
		speed:         3000
	});

	$("#fast-selector").click(function(){ 
		if (!fast_open) {
			$("#fast-list").slideDown();
			fast_open = true;
		}
		else {
			$("#fast-list").slideUp();
			fast_open = false;
		}
	});
	$("#fast-list").bind("mouseleave",function(){ 
		$("#fast-list").slideUp();
		fast_open = false;
	});
	
	
	$("#edit-search-theme-form-keys").focus(function(){
		if (!q_has_changed) {
			$(this).val("");
		}
	});
	$("#edit-search-theme-form-keys").blur(function(){
		if (!q_has_changed) {
			var lang = $("html").attr("lang");
			if (lang == 'ru') {
				$(this).val("Искать...");
			}
			else {
				$(this).val("Search query...");
			}
		}	
	});
	$("#edit-search-theme-form-keys").change(function(){
		q_has_changed = true;
	});
	
	
	//     
	//var now = false;
	$("#block-cck_blocks-field_overview .content p").hide();
	$("#block-cck_blocks-field_textright h4, #block-cck_blocks-field_textleft h4, #block-cck_blocks-field_textright h6, #block-cck_blocks-field_textleft h6").next("p").hide();
	$("#center .content h6").next("p[class!='opened']").hide();	
	$("#block-cck_blocks-field_overview .content h6, #block-cck_blocks-field_textright h4, #block-cck_blocks-field_textleft h4, #block-cck_blocks-field_textright h6, #block-cck_blocks-field_textleft h6, #center .content h6").css("cursor", "pointer").click(function(){
		//$("#block-cck_blocks-field_overview .content p").each(function(){
			//$(this).slideUp("fast");
		//});
		//if (this != now) {
			if ($(this).next("p").css("display") == 'none') {
				//now = this;
				$(this).addClass("active");
				$(this).html($(this).html().replace(/Открыть/,"Закрыть"));
			}
			else {
				//now = false;
				$(this).removeClass("active");
				$(this).html($(this).html().replace(/Закрыть/,"Открыть"));
			}			
			$(this).next("p").slideToggle("fast");
		//}
		//else {
		//	$(this).next("p").slideUp("fast");
		//	$(this).removeClass("active");
		//	$(this).html($(this).html().replace(/Закрыть/,"Открыть"));
		//	now = false;
		//}
	});
	
	$("div[class*='field'][class*='field-type-image'][class*='field-field-foto'] a").each(function(){
		var src = $(this).children("img").attr("src");
		var src2 = src.replace(/\/preview\//,'/preview2/');
		$("<img>").attr("src", src2);
		$(this).mouseover(function(){
			$(this).children("img").addClass("preview2");
			$(this).children("img").attr("src", src2);
		});
		$(this).mouseout(function(){
			$(this).children("img").removeClass("preview2");
			$(this).children("img").attr("src", src);
		});
	});
	
	$("div[class*='field'][class*='field-product-added'] a").each(function(){
		var src = $(this).children("img").attr("src");
		var src2 = src.replace(/\/micro\//,'/micro2/');
		$("<img>").attr("src", src2);
		$(this).mouseover(function(){
			$(this).children("img").addClass("micro2");
			$(this).children("img").attr("src", src2);
		});
		$(this).mouseout(function(){
			$(this).children("img").removeClass("micro2");
			$(this).children("img").attr("src", src);
		});
	});
	
});