﻿

function InputHelperIn ( obj, text ) {
	    //если при фокусе значение поля равно значению подсказки, то чистим его и вешаем стили
	if ( obj.value == text ) {
		$(obj).css({color:'#000',fontStyle:'normal'}).val('');
	}
}
	
function InputHelperOut ( obj, text ) {
	//если при потере фокуса значение поля равно пустоте или значению по умолчанию,
	//то пихаем в него текст подсказки и вешаем стили подсказки
	if ( obj.value == '' || obj.value == text ) {
		$(obj).css({color:'#b3b3b3',fontStyle:'normal'}).val(text);
	}
}
	
function InputHelperCreate ( obj, text ) {
if ( obj.val() == '' ) {
	//вешаем на поле эвенты. На фокус и потерю фокуса.
	$( obj ).bind ( 'focus', function () {
		InputHelperIn ( this, text );
	}).bind ( 'blur', function () {
		InputHelperOut ( this, text );
	});
			
	//первоначальный инит
	$( obj ).css ( { color: '#b3b3b3', fontStyle: 'normal' } ).val ( text );
}
}



function showStylePhoto(sSrc) {
	$(".style-photo").addClass("loading");
	$(".style-photo img").fadeOut("normal").remove();
	var lImage = new Image();
	$(lImage).load(function()	{
		$(this).hide();
		$(".style-photo").append(this).removeClass("loading");
		$(this).fadeIn("slow");
	});
	$(lImage).attr("src", sSrc);
	showImageScroller(sSrc);
};


// Устанавливаем  стрелки вперед-назад
function showImageScroller(sSrc) {
	$(".preview-photo li").each(function () {
		$(this).find("a").removeClass("selected");
		$(this).find("a").addClass("disabled");
		if($(this).find("a").attr("href") == sSrc.replace(/\\/g, "/").replace(/_l.jpg/g, ".jpg")){
			if( $(this).prev().find("a").attr("href") ) {
				$(".style-photo-prev a").attr("href",$(this).prev().find("a").attr("href"));
				//$(".style-photo-prev").removeClass("hidden");
			} else {
				//$(".style-photo-prev").addClass("hidden");
				$(".style-photo-prev a").attr("href",$(".preview-photo li a:last").attr("href"));
			};
			if( $(this).next().find("a").attr("href") ) {
				$(".style-photo-next a").attr("href",$(this).next().find("a").attr("href"));
				//$(".style-photo-next").removeClass("hidden");
			} else {
				$(".style-photo-next a").attr("href",$(".preview-photo li a:first").attr("href"));
			};
			$(this).find("a").removeClass("disabled");
			$(this).find("a").addClass("selected");
		};
	});
};



var showBigPhoto = function (options) {
	var that = popup_block(options);
	that.superior = function (name) {
		var that = this,
			method = that[name];
		return function () {
			return method.apply(that, arguments);
		};
	};
	var super_show = that.superior('show');
	var super_hide = that.superior('hide');
	that.show = function (event) {
		$("#popup-content div.picture").removeClass("hidden");
		showBigImage($("div.style-photo img").attr("src").replace(/.jpg/g, "_l.jpg"));
		super_show(event);
	};
	that.hide = function (event) {
		super_hide(event);
	};
	return that;
};

// Показываем большое изображение
// Устанавливаем предыдущую - слудующую
function showBigImage(sSrc) {
	$("#popup-content div.picture").addClass("loading");
	$("#popup-content div.picture #photo").fadeOut("normal").remove();
	var lImage = new Image();
	$(lImage).load(function()	{
		$(this).hide();
		$("#popup-content div.picture").append(this).removeClass("loading");
		$(this).fadeIn("slow");
	});
	$(lImage).attr("src", sSrc);
	$(lImage).attr("id", "photo");
	$(".preview-photo li").each(function () {
		if($(this).find("a").attr("href") == sSrc.replace(/\\/g, "/").replace(/_l.jpg/g, ".jpg")){
			if($(this).prev().find("a").attr("href")){
				$("#popup-content div.prev a").attr("href",$(this).prev().find("a").attr("href").replace(/.jpg/g, "_l.jpg"))
				//$("#popup-content div.prev").removeClass("hidden");
			} else {
				//$("#popup-content div.prev").addClass("hidden");
				$("#popup-content div.prev a").attr("href",$(".preview-photo li a:last").attr("href").replace(/.jpg/g, "_l.jpg"));
			};
			if($(this).next().find("a").attr("href")){
				$("#popup-content div.next a").attr("href",$(this).next().find("a").attr("href").replace(/.jpg/g, "_l.jpg"))
				//$("#popup-content div.next").removeClass("hidden");
			} else {
				//$("#popup-content div.next").addClass("hidden");
				$("#popup-content div.next a").attr("href",$(".preview-photo li a:first").attr("href").replace(/.jpg/g, "_l.jpg"));
			};
		};
	});
};


var show_location_table = function (options) {
	var that = popup_block(options);
	that.superior = function (name) {
		var that = this,
			method = that[name];
		return function () {
			return method.apply(that, arguments);
		};
	};
	var super_show = that.superior('show');
	var super_hide = that.superior('hide');
	that.show = function (event) {
		$("#popup_content .picture").addClass("hidden");
		$("#popup_content .picture_prev").addClass("hidden");
		$("#popup_content .picture_next").addClass("hidden");
		$("#popup_content .size").load(options.href);
		$("#popup_content .size").removeClass("hidden");
		super_show(event);
	};
	that.hide = function (event) {
		super_hide(event);
	};
	return that;
};


var show_frame = function (options) {
	var that = popup_block(options);
	that.superior = function (name) {
		var that = this,
			method = that[name];
		return function () {
			return method.apply(that, arguments);
		};
	};
	var super_show = that.superior('show');
	var super_hide = that.superior('hide');

	that.show = function (event) {
		$("#popup_content .picture").addClass("hidden");
		$("#popup_content .picture_prev").addClass("hidden");
		$("#popup_content .picture_next").addClass("hidden");
		$("#popup_content .size").addClass("hidden");
		if(options.href){ $("#inFrame").attr("src",options.href); }
		$("#inPop").removeClass("hidden");
//		$("#popup_content .size").load(options.href);
//		$("#popup_content .size").removeClass("hidden");
		super_show(event);
	};
	that.hide = function (event) {
		$("#inPop").addClass("hidden");
		$("#inFrame").attr("src","/Profile/Loader.aspx");
		super_hide(event);
	};
	return that;
};


var show_size_table = function (options) {
	var that = popup_block(options);
	that.superior = function (name) {
		var that = this,
			method = that[name];
		return function () {
			return method.apply(that, arguments);
		};
	};
	var super_show = that.superior('show');
	var super_hide = that.superior('hide');
	that.show = function (event) {
		$("#popup_content .picture").addClass("hidden");
		$("#popup_content .picture_prev").addClass("hidden");
		$("#popup_content .picture_next").addClass("hidden");
		$("#popup_content .size").load('/off-line/size.html');
		$("#popup_content .size").removeClass("hidden");
		super_show(event);
	};
	that.hide = function (event) {
		$("#popup_content .size").addClass("hidden");
		super_hide(event);
	};
	return that;
};


function showStyleWarning() {
	if($("#barcode").attr("value") == '') {
		$("div.style_selector h4").text("Пожалуйста, выберите цвет и размер:");
		$("div.style_selector").addClass("warning");
		setTimeout("hideStyleWarning()", 1000);
	}
};

function hideStyleWarning() {
	if($("#barcode").attr("value") == '') {
		$("div.style_selector h4").text("Укажите цвет и размер:");
		$("div.style_selector").removeClass("warning");
	}
};

function showAddComplete() {
	if($("#add-complete").height()){
		var h = $("#add-complete").height() + 30;
		$("#add-complete").animate({height: "show", top: "-" + h + "px"}, 300, "swing");
		setTimeout("hideAddComplete()", 3000);
	}
};

function hideAddComplete() {
	$("#add-complete").animate({height: "hide", top: "-30px"}, 300, "swing");
};



function showGiftsAlert() {
	if($("#gifts-alert").height()){
		var h = $("#gifts-alert").height() + 30;
		$("#gifts-alert").animate({height: "show", top: "-" + h + "px"}, 300, "swing");
		setTimeout("hideGiftsAlert()", 3000);
	}
};

function hideAddComplete() {
	$("#gifts-alert").animate({height: "hide", top: "-30px"}, 300, "swing");
};




function testAPI() {
	$("div.icon_close").click();
};

$(function() {

	InputHelperCreate ( $("input.iemail"), 'Подписка на новости' );
	InputHelperCreate ( $("input.search-input"), 'Поиск' );


	// style photo
	$(".preview-photo a").each(function () {
		$(this).click(function () {
			var sSource = $(this).attr("href");
			showStylePhoto(sSource);
			$(".preview-photo a").removeClass("selected");
			$(this).addClass("selected");
			return false;
		});
	});

	$(".style-photo-next").hide();
	$(".style-photo-prev").hide();
	$(".style-photo-zoom").hide();

	$(".style-photo-next a").click(function () {
		showStylePhoto($(this).attr("href"));
		return false;
	});
	$(".style-photo-prev a").click(function () {
		showStylePhoto($(this).attr("href"));
		return false;
	});


	$("#style-photo-container").hover(function () {
		$(".style-photo-next").show();
		$(".style-photo-prev").show();
		$(".style-photo-zoom").show();
	}, function () {
		$(".style-photo-next").hide();
		$(".style-photo-prev").hide();
		$(".style-photo-zoom").hide();
	});

	$("#popup-content .picture").hover(function () {
		$("#popup-content .next").show();
		$("#popup-content .prev").show();
	}, function () {
		$("#popup-content .next").hide();
		$("#popup-content .prev").hide();
	});



	showImageScroller($(".preview-photo a").attr("href"));

	// style BIG photo
	showBigPhoto({
		container: $("#popup-content"),
		link: $("#style-photo-container .zoom a"),
		fader: $("#fader"),
		close: $("#popup-content .icon-close")
	});

	$("#popup-content div.next a").click(function () {
		showBigImage($(this).attr("href"));
		return false;
	});
	$("#popup-content div.prev a").click(function () {
		showBigImage($(this).attr("href"));
		return false;
	});



	// menu toggle
	$(".toggle_next").click(function () {
		$(this).next().toggle();
	});


	// basket
	$(".one_click_button input").mouseover(function () {
		var h = $("#order_params").height() + 25;
		$("#order_params").animate({height: "show", top: "-" + h + "px"}, 300, "swing");
	}).mouseout(function () {
		$("#order_params").animate({height: "hide", top: "-25px"}, 300, "swing");
	});


	// подарки в корзине
	if ( $("#gifts-alert").length ) {
//		showGiftsAlert();
		alert("!");
	};




	// vote
	$("#rating_vote img").each(function () {
		$(this).addClass("clickable");
		var hRateItems = $("#rating_vote img").get();
		$(this).mouseover(function () {
			var pos = $("#rating_vote img").index(this);
			while(pos + 1) {
				var iIdPos = pos + 1;
				$("#rating_vote #r_" + iIdPos).attr("src", "/off-line/i/i-rs-plus.gif")
				pos--;
			};
		}).mouseout(function () {
				$("#rating_vote img").attr("src", "/off-line/i/i-rs-hover.gif")
		});
	});

	$(".rate_container").mouseover(function () {
		$("#"+sRatePanel).show();
	}).mouseout(function () {
		$("#"+sRatePanel).hide();
	});

	// адреса доставки
	$("span.city_selector").click(function () {
		$("dl.region_item").show();
		$("dl.city_item").hide();
	});
	$("span.region_selector").click(function () {
		$("dl.region_item").hide();
		$("dl.city_item").show();
	});
	$("select.selhdvis").change(function () {
		if($('select.selhdvis :selected').val() != '') {
			$("dl.region_item").hide();
			$("span.city_selector").hide();
		} else {
			//$("dl.hdvis").show();
			$("span.city_selector").show();
		};
	});

});


