/// <reference path="~/scripts/jquery.intellisense.js"/>
/**
* Common scripts
**/
function preventReserve() {
	$("#form-sign-in").bind("keypress", function(e) {
		if (e.keyCode == 13) {
			submitLoginForm();
			return false;
		}
	});
	$("#submit-login").click(function() {
		submitLoginForm();
		return false;
	});
}
function submitLoginForm() {
	var login = $("#login");
	var password = $("#password");
	var remember_me = $("#remember_me");
	var temp = $("#tempId");
	var store = $("#storeId");
	var data = "login=" + login.val() + "&password=" + password.val() + "&remember_me=" + remember_me.val() + "&tempId=" + temp.val() + "&storeId=" + store.val();
	$.ajax({
		type: "POST",
		url: "/login",
		data: data,
		cache: false,
		success: function(msg) {
			msg = $.evalJSON(msg);
			if (msg.status) {
				//$.fn.colorbox.close();
				$.fn.colorbox({ href: '/transactions/reserve/' + msg.backUrl, open: true });
			}
			else {
				login.parent().parent().removeClass("error");
				password.parent().parent().removeClass("error"); var errorString = "";
				for (var i = 0; i < msg.errors.length; i++) {
					switch (msg.errors[i].key) {
						case "login":
							{
								var l = login.parent().parent();
								l.addClass("error");
							}
							break;
						case "password":
							{
								var p = password.parent().parent();
								p.addClass("error");
							}
							break;
						case "form":
							{

							}
							break;
					}
					errorString += msg.errors[i].value + "\r\n";
				}
				alert(errorString);
			}
		}
	});

}
$(document).ready(function() {
	/*if ($.browser.msie && $.browser.version == "6.0") {
		window.location.href = "/be-modern";
	}*/
	$("div.tipInner div.closeBtn a").click(function() {
		$.cookie("HideFirstTip", "", { expires: 365 });
		$("div.tipInner").attr("style", "display:none");
		return false;
	});
	$("div.tipFirst div.closeBtn a").click(function() {
		$.cookie("HideFirstTip", "", { expires: 365 });
		$("div.tipFirst").attr("style", "display:none");
		return false;
	});

	$("span.google").click(function() {
		$(".search form").toggleClass("default-search");
		$(this).toggleClass("instance");
	});
	$("div.tip div.cont div.title a").click(function() {
		//alert('saaas');
		$(this).parent().parent().parent().toggleClass('closed');
		//$(this).parent().parent().parent().toggleClass('open');
		return false;
	});

	$("div.closeBtn").click(function() {
		window.location = $(this).children()[0].href;
	});

	$("a.popup").colorbox({}, function() {
		if ($(this).hasClass("finder"))
			initPF();
		if ($(this).hasClass("login-required"))
			preventReserve();
	});
	$("#lang-selector").click(function() {
		langChange();
		return false;
	});
	$(".langs span").click(function() {
		langChange();
	});
	$(".menu li.tab").hoverIntent(toggleMenu, toggleMenu);
	$(".menu li.tab a.tabPart").click(function() {
		if (!$(this).hasClass("exclude"))
			return false;
	});
	//Location panel
	$(".location-panel input[type=checkbox]").click(function() {
		var x = $(this).parent().parent().find("select");
		if ($(this).attr("checked") == true) {
			x.trigger("change.cascade");
			if (x.children().length > 0) {
				x.attr("disabled", false);
				$("#selectedLocation").val(x.val());
			}
			else
				$(this).attr("checked", false);
		}
		else {
			var string = x.attr("id").toString();
			var id = string.substring(string.length - 1, string.length);
			if (id == "1") {
				$("#selectedLocation").val("");
			}
			else {
				$("#selectedLocation").val($("#chained" + (id - 1)).val());
			}
			x.attr("disabled", true);
		}
	});
	$(".search .location a").click(function() {
		togglePanel("location-panel");
		return false;
	});
	$(".location-panel b").click(function() {
		togglePanel("location-panel");
	});
	$(".change-location").click(function() {
		window.location.href = window.location.href;
	});
	$("#chained2").cascade("#chained1", {
		ajax: {
			url: '/locations/getChildren'
		},
		template: commonTemplate,
		match: commonMatch
	});
	$("#chained3").cascade("#chained2", {
		ajax: {
			url: '/locations/getChildren'
		},
		template: commonTemplate,
		match: commonMatch
	});
	$("#chained4").cascade("#chained3", {
		ajax: {
			url: '/locations/getChildren'
		},
		template: commonTemplate,
		match: commonMatch
	});
	$(".location-panel input[type='button']").click(function() {
		var location = $("#selectedLocation").val();
		window.location.href = "/locations/change/" + location;
	});


});
function commonMatch(selectedValue, status) {
	if (status)
		$("#selectedLocation").val(selectedValue);
	return this.When == selectedValue;
};
function commonTemplate(item) {
	return "<option value='" + item.Id + "'>" + item.Name + "</option>";
};
var _state = true;
function toggleMenu() {
	var _classes = $(this).attr("class").toString().split(' ');
	if (_classes.length > 1 && _classes[_classes.length - 1] != "fright") {
		var _class = _classes[_classes.length - 1];
		$(this).find("span.arrow-tab").toggleClass("arrow-up");
		if (_state)
			$("div." + _class).show();
		else
			$("div." + _class).hide();
		_state = !_state;
		return false;
	}
}
function togglePanel(className) {
	$("." + className).slideToggle("fast");
}
function langChange() {
	$(".langs span").toggleClass("arrow-up");
	$(".languages").slideToggle("fast");
	$(".languages").toggleClass("show");
}

$.notifyBar = function(settings) {
	var bar = {};

	this.shown = false;

	if (!settings) {
		settings = {};
	}
	// HTML inside bar
	this.html = settings.html || "Your message here";

	//How long bar will be delayed, doesn't count animation time.
	this.delay = settings.delay || 2500;

	//How long this bar will be slided up and down
	this.animationSpeed = settings.animationSpeed || "normal";

	//Use own jquery object usually DIV, or use default
	this.jqObject = settings.jqObject;

	if (this.jqObject) {
		bar = this.jqObject;
	} else {
		bar = $("<div></div>")
		//basic css rules
                  .attr("id", "notifyBar")
                  .css("width", "100%")
                  .css("position", "fixed")
                  .css("top", "0px")
                  .css("left", "0px")
                  .css("z-index", "32768")
		//additional css rules, which you can modify as you wish.
                  .css("background-color", "#dfdfdf")
                  .css("font-size", "18px")
                  .css("color", "#000")
                  .css("text-align", "center")
                  .css("font-family", "Arial, Helvetica, serif")
                  .css("padding", "30px 0px")
                  .css("border-bottom", "1px solid #bbb");
	}

	bar.html(this.html).hide();
	var id = bar.attr("id");
	switch (this.animationSpeed) {
		case "slow":
			asTime = 600;
			break;
		case "normal":
			asTime = 400;
			break;
		case "fast":
			asTime = 200;
			break;
		default:
			asTime = this.animationSpeed;
	}
	$("body").prepend(bar);
	bar.slideDown(asTime);
	setTimeout("$('#" + id + "').slideUp(" + asTime + ");", this.delay + asTime);
};

function initPF() {
	$(".product-finder-alphabet a").click(function() {
		$(".product-finder-alphabet a").removeClass("cur");
		$("#product-finder div.items a").removeClass("cur");
		$(this).addClass("cur");
		var div = $($(this).attr("href"));
		div.find("a:first").addClass("cur");
	});
	$('#product-finder input').bind("change keyup", function() {
		if (this.value != null && this.value.length > 0) {
			$("#product-finder div.items a").removeClass("cur");
			var val = this.value.toLowerCase();
			var element = $("#product-finder .items a:[rel^='" + val + "']");
			if (element.length > 0) {
				element[0].className = "cur";
				$("#product-finder div.scrollable").animate({ scrollTop: element[0].offsetTop }, 100);
			}
		}
	});
	$(".product-finder-alphabet li b").click(function() {

		var letter = $(this).attr("rel");
		if (letter == "num") {
			$("#product-finder div.items a").removeClass("cur");
			$("#product-finder div.items a:first").addClass("cur");
			$("#product-finder div.scrollable").animate({ scrollTop: 0 }, 100);
		}
		else {
			var element = $("#product-finder .items a:[rel^='" + (letter + "").toLowerCase() + "']");
			if (element.length > 0) {
				$("#product-finder div.items a").removeClass("cur");
				element[0].className = "cur";
				$("#product-finder div.scrollable").animate({ scrollTop: element[0].offsetTop }, 100);
			}
		}
		return false;
	});
}
