﻿

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:'italic'}).val(text);
	}
}
	
function InputHelperCreate ( obj, text ) {
	    //вешаем на поле эвенты. На фокус и потерю фокуса.
		$( obj ).bind ( 'focus', function () {
				InputHelperIn ( this, text );
			}).bind ( 'blur', function () {
				InputHelperOut ( this, text );
			});
			
		//первоначальный инит
		$( obj ).css ( { color: '#b3b3b3', fontStyle: 'italic' } ).val ( text );
}



function show_image(sSrc) {
	$(".style-photo img").fadeOut("normal").remove();
	var lImage = new Image();
	$(lImage).load(function()	{
		$(this).hide();
		$(".style-photo a").append(this).removeClass("loading");
		$(this).fadeIn("slow");
	});
	$(lImage).attr("src", sSrc);
};


var show_big_photo = 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 .size").addClass("hidden");
		$("#popup_content .picture").removeClass("hidden");
		show_big_image($(".style-photo img").attr("src").replace(/.jpg/g, "_l.jpg"));
		super_show(event);
	};
	that.hide = function (event) {
		super_hide(event);
	};
	return that;
};


function show_big_image(sSrc) {
	$("#popup_content .picture").addClass("loading");
	$("#popup_content .picture img").fadeOut("normal").remove();
	var lImage = new Image();
	$(lImage).load(function()	{
		$(this).hide();
		$("#popup_content .picture").append(this).removeClass("loading");
		$(this).fadeIn("slow");
	});
	$(lImage).attr("src", sSrc);
	$(".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")){
				$(".picture_prev a").attr("href",$(this).prev().find("a").attr("href").replace(/.jpg/g, "_l.jpg"))
				$(".picture_prev").removeClass("hidden");
			} else {
				$(".picture_prev").addClass("hidden");
			};
			if($(this).next().find("a").attr("href")){
				$(".picture_next a").attr("href",$(this).next().find("a").attr("href").replace(/.jpg/g, "_l.jpg"))
				$(".picture_next").removeClass("hidden");
			} else {
				$(".picture_next").addClass("hidden");
			};
		};
	});
};


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 testAPI() {
	//alert("Ok");
	$("div.icon_close").click();
};

$(function() {

	InputHelperCreate ( $("input.iemail"), 'Подписка на новости' );

	// style photo
	$(".preview-photo a").each(function () {
		$(this).click(function () {
			var sSource = $(this).attr("href");
			$(".style-photo").addClass("loading");
			show_image(sSource);
			return false;
		});
	});

	// style BIG photo
	show_big_photo({
		container: $("#popup_content"),
		link: $(".style-photo a"),
		fader: $("#fader"),
		close: $("#popup_content .icon_close")
	});

	$(".picture_next a").click(function () {
		show_big_image($(this).attr("href"));
		return false;
	});
	$(".picture_prev a").click(function () {
		show_big_image($(this).attr("href"));
		return false;
	});

	// menu toggle
	$(".toggle_next").click(function () {
		$(this).next().toggle();
	});


	// basket
	$(".one_click_button input").mouseover(function () {
		$("#order_params").slideToggle("fast");
	}).mouseout(function () {
		$("#order_params").slideToggle("fast");
	});

	// 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();
		};
	});

});

