/* remove error messages IE 7 or greater */
function blockError() {
	return true
}
window.onerror = blockError;

/* globals */
var
/* global */
doc, win, ie, ie6,

tabs, tabsTotal,

mouse_x, dragMax, widthXL, dragMenu, dragMenuItems, dragMenuSlide,

urunGaleri, dragBtnStart, urunSira;

var aktifSection = "renk";

$(function() {

	/* variables */

	/* global */
	doc = $(document), win = $(window), ie = /* @cc_on!@ */false,
			ie6 = false /* @cc_on || @_jscript_version < 5.7 @ */,

			/* tabs */
			tabs = $(".tabs li"), tabsTotal = tabs.length,

			/* drag menu */
			mouse_x = dragMin = 0, dragMax = 816, widthXL = 990,
			dragMenu = $(".dragMenu"), dragMenuItems = $("li", dragMenu),
			dragMenuSlide = $("div", dragMenu),

			/* urunGallery */
			urunGaleri = $("#urunGaleri"), urunSira = dragBtnStart = 0;

	/* standart resimler */
	$(".stdImage").each(
			function() {
				$(".light").css("height",
						parseInt($("img", this).height() + 41) + "px");
			});

	/* tabs */
	tabs.eq(0).addClass("first").css("margin-left",
			(widthXL - (tabsTotal * tabs.eq(0).width())) / 2 + "px");
	tabs.eq(tabsTotal - 1).addClass("last");

	/* drag menu */
	dragGallery = parseInt(dragMenuItems.length
			* (dragMenuItems.eq(0).width() + 20));

	if (dragGallery > widthXL) {

		dragMenuSlide.css("width", dragGallery + "px");

		$("li.selected a", dragMenu).bind("click", function() {
			dragBtnStart = $("li a", dragMenu).index(this);
			return false
		}).trigger("click");

		$(".dragBtn")
				.bind(
						"mousedown",
						function() {

							doc
									.mousemove(function(e) {

										mouse_x = e.pageX - dragMin;

										if (e.pageX <= dragMin) {
											mouse_x = 0;
										}
										if (e.pageX >= dragMin + dragMax) {
											mouse_x = dragMax;
										}

										$(".dragBtn").css("margin-left",
												mouse_x + "px");
										dragMenuSlide
												.css(
														"margin-left",
														((-mouse_x / (dragMax / dragGallery)) + (mouse_x / (dragMax / widthXL)))
																+ "px");

									});

							if (ie) {
								document.body.onselectstart = function() {
									return false
								}
							}

							doc.bind("mouseup", function() {
								doc.unbind("mousemove , mouseup");
								if (ie) {
									document.body.onselectstart = function() {
										return true
									}
								}
							});

							return false

						}).css(
						"margin-left",
						((dragBtnStart * dragMax) / dragMenuItems.length)
								+ "px");
		dragMenuSlide
				.css(
						"margin-left",
						((-((dragBtnStart * dragMax) / dragMenuItems.length) / (dragMax / dragGallery)) + (((dragBtnStart * dragMax) / dragMenuItems.length) / (dragMax / widthXL)))
								+ "px");

	} else {
		dragMenuItems.addClass("dragPassive");
		$(".dragIt").remove();
	}

	var selectIt = $(".dragIt .text");

	$(".dragMenu a").not("li.selected a").hover(
			function() {

				selectIt.hide();
				$("strong", selectIt).html($("strong", this).html());
				$("span", selectIt).html($("u", this).html());
				selectIt.fadeIn("fast");

			},
			function() {

				selectIt.hide();
				$("strong", selectIt).html(
						$("strong", ".dragMenu li.selected").html());
				$("span", selectIt)
						.html($("u", ".dragMenu li.selected").html());
				selectIt.fadeIn("fast");

			});

	if (ie6) {

		$(".dragMenu li , .dragMenu li span").not(
				"li.selected , li.selected span").hover(

		function() {
			$(this).css("background-position", "0px -176px");
		}, function() {
			$(this).css("background-position", "0px 0px");
		}

		);

		$(".dragBtn").hover(

		function() {
			$(this).css("background-position", "0px 0px");
		}, function() {
			$(this).css("background-position", "0px -16px");
		}

		);

	}

	/* window resize */
	win.resize(function() {

		screenW = win.width();
		screenH = win.height();

		/* drag menu */
		dragMin = (screenW - dragMax) / 2;

	}).trigger("resize");

	/* urunGallery */
	$(".tabs a", urunGaleri).bind(
			"click",
			function() {

				var git = $(this).attr("href");

				$(".tabs a", urunGaleri).removeClass("selected");
				$(this).addClass("selected");

				$(".window", urunGaleri).hide();
				$(git).show();

				if (git == "#renkler") {
					aktifSection = "renk";
					$('#renkList').load(
							'/index/ajax/section/renk/s/0/lid/' + _lid,
							function() {
								setPager($('#listTotal_renk').val());
								setPreview();
							});

				} else {
					aktifSection = "desen";
					$('#desenList').load(
							'/index/ajax/section/desen/s/0/lid/' + _lid,
							function() {
								setPager($('#listTotal_desen').val());
								setPreview();

								$("a.desenFoto").click(function() {
									var sid = $(this).attr("prm") * 1;

								});
							});

				}

				return false

			});

	$(".arrows", urunGaleri).each(
			function() {
				$("a", this).bind(
						"click",
						function() {

							if ($(this).attr("class") == "up") {

								if (urunSira >= ($(".dotts a", $(this).parents(
										".window")).length - 1)) {
									urunSira = -1;
								}
								urunSira += 1;

							} else {
								urunSira -= 1;
							}

							$(".dotts a", $(this).parents(".window")).eq(
									urunSira).trigger("click");

							return false

						});
			});

});

/* navigation */
var navCont = navSpeedCont = 0;

$(".navSubbed").live("click", function() {

	if (navSpeedCont == 0) {
		navSpeedCont = 1;

		var thisBtn = $(this);

		if (navCont == 0) {
			navCont = 1;

			$(".navSubbed").removeClass("subSelected").removeAttr("rel");
			thisBtn.addClass("subSelected").attr("rel", "selected");

			$("#head").stop().animate({
				height : "152px"
			}, 250, function() {
				$("ul", thisBtn.parent()).show();
				navSpeedCont = 0;
			});

		} else {

			if (thisBtn.attr("rel") == "selected") {
				navCont = 0;

				$(".navSubbed").removeClass("subSelected").removeAttr("rel");

				$(".navSub").hide();
				$("#head").stop().animate({
					height : "132px"
				}, 350, function() {
					navSpeedCont = 0;
				});

			} else {
				navCont = 1;

				$(".navSubbed").removeClass("subSelected").removeAttr("rel");
				thisBtn.addClass("subSelected").attr("rel", "selected");

				$(".navSub").hide();
				$("ul", thisBtn.parent()).show();
				navSpeedCont = 0;
			}
		}
	}
	return false

});

function setPager(toplam) {

	var galeriSay = toplam;

	// alert(toplam +' - ' +aktifSection );
	if (aktifSection == "renk") {
		if (parseInt(toplam) > 0)
			$("#renklerBtn").parent().show();
		else {
			$("#renklerBtn").parent().hide();
			$("#desenBtn").parent().css("margin-left",
					(widthXL - (tabsTotal * tabs.eq(0).width())) / 2 + "px");
			$("#desenBtn").trigger("click");
		}
	}

	if (aktifSection == "desen") {
		if (parseInt(toplam) > 0)
			$("#desenBtn").parent().show();
		else
			$("#desenBtn").parent().hide();
	}

	if (galeriSay < 20) {
		$(".panel .dotts , .panel .arrows").hide();
	} else {
		$(".panel .dotts , .panel .arrows").show();
	}

	$(".panel .text strong").html(galeriSay);
	galeriSay = galeriSay / 20;

	if (galeriSay.toString().indexOf(".") == "-1") {
		galeriSay -= 1;
	}

	$(".panel .dotts").empty();

	for (i = 0; i <= galeriSay; i++) {
		$(".panel .dotts").append('<a href="javascript:void(0)">'+(i+1)+'</a>');
	}

	$(".dotts", $("#urunGaleri")).each(
			function() {
				$("a", this).bind(
						"click",
						function() {

							var thisParent = $("a", $(this).parent());
							var urunSira = thisParent.index(this);

							thisParent.removeClass("selected");
							$(this).addClass("selected");
							// if (urunSira==0) urunSira=1;
							// alert(urunSira);
							var aUrl = '/index/ajax/section/' + aktifSection
									+ '/s/' + ((urunSira) * 20) + '/lid/'
									+ _lid;
							// alert(aUrl);
							$('#' + aktifSection + 'List').load(aUrl,
									setPreview);

							return false

						}).eq(0).addClass("selected");
			});

}

function setPreview() {

	$(".liste a", $("#urunGaleri"))
			.bind(
					"mouseenter",
					function() {

						var thisBtn = $(this);
						$("body")
								.prepend(
										'<div id="urunPreview"><img src="img/spacer.gif" /></div>');
						$("#urunPreview").hide();

						doc.mousemove(function(y) {

							if (y.pageX >= ((screenW - widthXL) / 2) + 495) {
								y.pageX -= 460;
							}
							$("#urunPreview").fadeIn("fast").css({
								top : y.pageY + "px",
								left : y.pageX + "px"
							}).find("img").attr("src", thisBtn.attr("href"));

						});

						if (ie6) {
							$("li", thisBtn.parents(".window")).removeClass(
									"current");
							thisBtn.parents("li").addClass("current");
						}

						return false

					}).bind("mouseleave", function() {

				doc.unbind("mousemove");
				$("#urunPreview").remove();

				if (ie6) {
					$("li", $(this).parents(".window")).removeClass("current");
				}

				return false

			}).bind("click", function() {

				return false

			});

	$("#urunPreview").bind("mouseenter", function() {
		$(".liste a", $("#urunGaleri")).trigger("mouseleave");
	});

}
function setDesenPreveiw() {

	$("a", $("#desenList"))
			.bind(
					"mouseenter",
					function() {

						var thisBtn = $(this);
						var txt=thisBtn.attr("title");

						$("body")
								.prepend(
										'<div id="urunPreview2"><div class="darker"></div></div>');
						$("#urunPreview2").hide();

						doc.mousemove(function(y) {

							if (y.pageX >= ((screenW - widthXL) / 2) + 495) {
								y.pageX -= 260;
								
							}
							$("#urunPreview2").fadeIn("fast").css({
								top : y.pageY + "px",
								left : y.pageX + "px"
							}).find("div").html('<h3>Yüzey Ölçüleri</h3>'+txt);

						});

						return false;
					}).bind("mouseleave", function() {

				doc.unbind("mousemove");
				$("#urunPreview2").remove();

				return false

			}).bind("click", function() {

				//return false

			});

}

/* sss */
$("#sss .akordiyon").live(
		"click",
		function() {
			$(".side").slideUp(400);
			$(".side", $(this).parents("#sss")).eq(
					$("#sss .akordiyon").index(this)).slideDown(400);
			return false
		});
