﻿/// <reference path="jquery-1.3.2-vsdoc.js" />


function haydarization(txt2wrap) {
	var output = "";
	for (var t = 0; t < txt2wrap.length; t++) {
		if (t > 0 && t % 10 == 0)
			output += "<wbr />";
		output += txt2wrap.charAt(t);
	}
	return output;
}


shuffle = function(o) {
	for (var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};


// 0 ile limit-1 arasinda bir tam sayi
function getRandomLimit(limit) {
	return Math.floor(Math.random() * 1000000) % limit;
}


jQuery(function() {


	// rotating banner
	var rotating_contents = [
		["Vişneli Jöleli Parfe", "visneli-joleli-parfe"],
		["Meyveli Kaplar", "meyveli-kaplar"],
		["Ananaslı Sakızlı Panna Cotta", "ananasli-sakizli-panna-cotta"],
		["Enerji Barları", "enerji-barlari"],
		["Çilek Rüyası", "cilek-ruyasi"],
		["Kirpi", "kirpi"],
		["Frambuazlı Cheesecake", "frambuazli-cheesecake", "video"],
		["Naneli Çikolata", "naneli-cikolata", "video"],
		["Kayısılı Kaşık Tatlısı", "kayisili-kasik-tatlisi", "video"],
		["Kap Kek", "kap-kek", "video"]
	];

	for (var i = 0; i < rotating_contents.length; i++)
		shuffle(rotating_contents);

	rotating_contents.splice(4, 4);

	/*
	console.log(rotating_contents.length + "\n");
	for (var i = 0; i < rotating_contents.length; i++) {
		console.log(i + ": " + rotating_contents[i][0]);
	}
	*/

	var popped;
	for (i = 1; i <= 2; i++) {
		popped = rotating_contents.pop();
		$("#box-" + i + " a").html(popped[0]);
		$("#box-" + i + " a").attr("href", "/tarif/" + popped[1]);
		$("#box-" + i + " .recipelist_item div").css("background-image", "url(/recipes/thumb/" + popped[1] + ".jpg)");
		$("#box-" + i + " .recipelist_item div a").html((popped[2] == "video") ? "<img src=\"/_img/play_small.png\" width=\"50\" height=\"50\" class=\"png\" alt=\"\" />" : "");
	}


	$("#rotating #play").after("<ul></ul>");
	$("#rotating").append("<img src='' width='580' height='300' alt='' />");

	jQuery.each(rotating_contents, function(i) {
		$("#rotating ul").append("<li>" + (i + 1) + "</li>");
	});

	$("#rotating ul li")
		.click(function() { rotate(parseInt($(this).html()) - 1); })
		.hover(function() { $(this).css("background-position", "bottom"); }, function() { if ($(this).attr("class") == "") { $(this).css("background-position", "top"); } });

	function rotate(i) {
		if (i == null) i = 0;
		$("#rotating #title").html(rotating_contents[i][0]);
		$("#rotating #title_bg + img").attr("src", "recipes/image/" + rotating_contents[i][1] + ".jpg");
		$("#rotating #title_bg + img").attr("alt", rotating_contents[i][0]);
		$("#rotating #play")
			.attr("title", "Videoyu izlemek için tıklayınız.")
			.css("display", rotating_contents[i][2] == "video" ? "block" : "none");
		$("#rotating #play a").attr("href", "/tarif/" + rotating_contents[i][1]);
		$("#rotating a").attr("href", "/tarif/" + rotating_contents[i][1]);
		$("#rotating ul li").removeClass("selected");
		$("#rotating ul li").css("background-position", "top");
		$("#rotating ul li:contains('" + (i + 1) + "')").addClass("selected");
		$("#rotating ul li:contains('" + (i + 1) + "')").css("background-position", "bottom");
	}

	rotate(/*Math.floor(Math.random() * 3)*/);


	// login area resize
	var biscuitHeight = $(".biscuit_bottom").height();
	var biscuitRepeat = Math.floor((biscuitHeight - 44) / 30);
	var biscuitMod = (biscuitHeight - 44) % 30;
	var biscuitAdd = (biscuitMod > 16 && biscuitMod != 0) ? 30 : 16;
	$(".biscuit_bottom").height((30 * biscuitRepeat) + 44 + biscuitAdd);


	// submit on enter
	$("#txtPassword_Sidebar_Login").keypress(function(e) { if (e.which == 13 && $(this).val() != "") Login(); });


	// file input
	$(".divFileUpload input").change(function() {
		var filename = $(this).val();
		for (var i = filename.length - 1; i > 0; i--) {
			if (filename.substr(i, 1) == ("\\")) {
				filename = filename.substr(i + 1, filename.length - i);
				break;
			}
		}
		$("#ctl00_content_txtFile").val(filename);
	});
	$(".divFileUpload").hover(
		function() { $(this).css("background-position", "0 -" + $(this).height() + "px"); },
		function() { $(this).css("background-position", "0 0"); }
	);


	// only numbers allowed
	$("input[id*='ServingSize'], input[id*='PreperationTime']")
		.keyup(function() {
			$(this).val($(this).val().replace(/\D+/, ""));
		}).blur(function() {
			$(this).val($(this).val().replace(/\D+/, ""));
		});


	// auto nickname creation
	$("input[id*='NameSurname']").blur(function() {
		if ($("input[id*='Nickname']").val() == "") $("input[id*='Nickname']").val($("input[id*='NameSurname']").val()).blur();
	});

});


// thickbox resize
function tb_resize(w, h) {
	$("#TB_window").css({ width: w + "px", height: h + "px" });
	$("#TB_iframeContent").css({ width: w + "px", height: h + "px" });
	TB_WIDTH = w;
	TB_HEIGHT = h;
	tb_position();
}


// ajax stuff
$.ajaxSetup({
	type: "POST",
	contentType: "application/json; charset=utf-8",
	data: "{}",
	error: function(XMLHttpRequest, textStatus, errorThrown) {
		//console.log(XMLHttpRequest.responseText + " / " + textStatus + " / " + errorThrown);
	}
});

function Login() {
	var email = $("#txtEmail_Sidebar_Login").val();
	var password = $("#txtPassword_Sidebar_Login").val();

	$.ajax({
		url: "/AjaxServices.asmx/Login",
		data: "{ 'email':'" + email + "', 'password':'" + password + "' }",
		dataType: "json",
		success: function(msg) {
			var success = msg.d.Success;
			if (success) {
				if (msg.d.Nickname == null)
					tb_show_manual('/TbNickname.aspx?KeepThis=true&TB_iframe=true&height=150&width=300&modal=true');
				else
					RefreshParent();
			}
			else {
				tb_show_manual('/TbLogin.aspx?loginerr=true&KeepThis=true&TB_iframe=true&height=180&width=300&modal=true');
			}
		}
	});
}

function Logout() {
	$.ajax({
		url: "/AjaxServices.asmx/Logout",
		success: function(msg) {
			RefreshParent();
		}
	});
}

function LoadTownships(ctrl) {

	var cityId = $(ctrl).val();

	if (cityId == "0") {
		var townSelect = document.getElementById("ctl00_content_ddlTown");
		townSelect.disabled = "disabled";
		townSelect.length = 0;
		townSelect.options[0] = new Option("Önce İlinizi Seçiniz", "0");
	}
	else {
		$.ajax({
			url: "/AjaxServices.asmx/GetTownshipList",
			data: "{ 'id':" + cityId + " }",
			dataType: "json",
			success: function(msg) {

				var townSelect = document.getElementById("ctl00_content_ddlTown");
				townSelect.disabled = "disabled";
				townSelect.length = 0;
				$(msg.d.Items).each(function() {
					var n = this.TownShipName;
					var i = this.TownShipId;
					townSelect.options[townSelect.length] = new Option(n, i);
				});

				townSelect.disabled = "";
			}
		});
	}
}

function Refresh() {
	location.reload(true);
}

function RefreshParent() {
	var loc = parent.document.location.href;
	var karakter = (loc.indexOf("?") < 0) ? "?" : "&";
	var qs = window.location.search.substring(1);
	var array = qs.split("&");
	for (i = 0; i < array.length; i++) {
		nameValuePair = array[i].split("=");
		if (nameValuePair[0] == "r") {
			new_qs = qs.replace(nameValuePair[1], GetRandom());
			parent.document.location.href = loc.replace(qs, new_qs);
		} else {
			parent.document.location.href = loc + karakter + "r=" + GetRandom();
		}
	}
}

function GetRandom() {
	var rnd = Math.floor(Math.random() * 1000000);
	return rnd;
}
