﻿
var root = "";


$(document).ready(function () {
	if (root.length > 1) {
		$("img").each(function (i) {
			var src = $(this).attr("src");
			if (src.indexOf(root) != 0 && src.indexOf("http://") == -1 && src.indexOf("..") == -1) {
				$(this).attr("src", (root + src).replace("//", "/"));
			}
		});
	}

	$(".textwatermark").focus(function () {
		if (this.value == this.defaultValue) {
			$(this).val("");
		}
	});

	$(".textwatermark").blur(function () {
		if (this.value == "" && this.defaultValue != "") {
			this.value = this.defaultValue;
		}
	});

	//Login popup form
	$("#top_line div.inner div.right ul.top_menu li.mylbd").hover(
		  function () {
		  	$("#top_line div.inner div.right ul.top_menu li.mylbd .basketcontainer").stop(true, true).slideDown("500", function () {
		  		$(this).find("input:text").focus();
		  	});

		  },
		  function () {
		  	$("#top_line div.inner div.right ul.top_menu li.mylbd .basketcontainer").stop(true, true).slideUp("500");
		  }
		);
	// iDevice fallback
	$("#top_line div.inner div.right ul.top_menu li.mylbd").click(function () {
		$("#top_line div.inner div.right ul.top_menu li.mylbd .basketcontainer").stop(true, true).slideDown("500");
	});

	$("#login input.text").keypress(function (e) {
		if (e.keyCode == 13) {
			validatelogin();
		}
	});

	//Fancy basket at topbar
	$("#top_line div.inner div.right ul.top_menu li.libasket").hover(
		  function () {
		  	$("#top_line div.inner div.right ul.top_menu li.libasket .basketcontainer").stop(true, true).slideDown("500");

		  	$('#top_line div.inner div.right ul.top_menu li.libasket .basketcontainer .basketcontent').jcarousel({
		  		vertical: true,
		  		scroll: 1,
		  		initCallback: function (p, p2) {
		  			var items = $('#top_line div.inner div.right ul.top_menu li.libasket .basketcontainer .basketcontent .basketitem').length;
		  			if (items < 3) {
		  				$('#top_line div.inner div.right ul.top_menu li.libasket .basketcontainer .basketcontent').css("height", (116 * items));
		  				$("#top_line div.inner div.right ul.top_menu li.libasket .basketcontainer .jcarousel-container").css("height", (116 * items)).css("padding-top", "0px").css("padding-bottom", "0px");
		  				$("#top_line div.inner div.right ul.top_menu li.libasket .basketcontainer .jcarousel-container .jcarousel-clip").css("height", (116 * items));
		  			}
		  		}
		  	});

		  	//$('#top_line div.inner div.right ul.top_menu li.libasket .basketcontainer').css("visibility", "visible");
		  },
		  function () {
		  	$("#top_line div.inner div.right ul.top_menu li.libasket .basketcontainer").stop(true, true).slideUp("500");
		  }
		);
	// iDevice fallback
	$("#top_line div.inner div.right ul.top_menu li.libasket").click(function () {
		$("#top_line div.inner div.right ul.top_menu li.libasket .basketcontainer").stop(true, true).slideDown("500");
	});



});


function openmodal(modalid) {
	clearmodal();
	var modal = $("#" + modalid);
	if (modal.length > 0) {
		var winheight = $(window).height();
		var winwidth = $(window).width();

		var overlay = $("<div></div>").addClass("modaloverlay");
		overlay.bind("click", function (e) {
			if (e.target == this) {
				clearmodal();
			}
		}, true);

		modal.css("top", ((winheight / 2) - (modal.height() / 2)).toString() + "px");

		//modal.click(function (e) { return false; });
//		modal.find("a").click(function (e) {
//			e.stopPropagation();
//		});

		modal.show();
		modal.appendTo(overlay);
		$("body form").prepend(overlay);
		
		$(window).scroll(function () {
			repositionmodal(modalid);
		});

		$(window).resize(function () {
			repositionmodal(modalid);
		});

		$("#" + modalid).find("input.focus").focus();

	}
}

function clearmodal() {
	var overlay = $(".modaloverlay").remove();
	overlay.find("div:first").appendTo($("body form")).hide().unbind("click");
	overlay.remove();
	$(window).unbind('scroll');
	$(window).unbind('resize');
}

function repositionmodal(modalid) {
	var winheight = $(window).height();
	var modal = $("#" + modalid);
	modal.css("top", ((winheight / 2) - (modal.height() / 2)).toString() + "px");
}

function validatelogin() {
	var username = $("input[name$='tbUsername']").val();
	var pass = $("input[name$='tbPassword']").val();

	if (username != null && username != "undefined" && pass != null && pass != "undefined" && username.length > 0 && pass.length > 0) {
		$.ajax({
			url: root + "cms_files/plugins/c5/frontend/ajaxpage.ashx?job=checklogin",
			dataType: "json",
			async: false,
			type: "POST",
			data: { username: username, password: pass },
			success: function (JSON) {
				if (JSON[0] == true) {
					location.reload(true);
					return true;
				}
				else {
					$("span.warning.login").css("visibility", "visible");
					$("input[name$='tbPassword']").focus();
					return false;
				}
			},
			error: function (s, ss, sss) { alert("trace error"); alert(s); alert(ss); alert(sss); return false; }
		});
	}
	else {
		return false;
	}
}

function signout() {
	$.ajax({
		url: root + "cms_files/plugins/c5/frontend/ajaxpage.ashx",
		dataType: "json",
		type: "GET",
		async: false,
		data: { job: "signout" },
		success: function (JSON) {
			
		},
		error: function (s, ss, sss) { alert("trace error"); alert(s); alert(ss); }
	});
	location.reload(true);
	return false;
}
