// JavaScript Document
$(function() {
	$("div.sliderContainerInner").jcarousel({
		scroll: 2,
		initCallback: mycarousel_initCallback,
		buttonNextHTML: null,
		buttonPrevHTML: null
    });
});
$(function() {

    // Link Controls.....................
    $("a[href=#]").bind("click", function(e) {
        e.preventDefault();
    });
    // Link Controls.....................

    // Image Controls.....................
    var i = 0;
    var altText, imageName, parameterPosition, image, index, imagePath, thmbImages, bigImagesLi, magnifyLink;
    var arrowBlock = $("ul.mediaThumbContainer li:first span.mediaSelector");
    bigImagesLi = $("ul.mediaContainer li");

    $("ul.mediaContainer li").not(":first").each(function() {
        $(this).css("display", "none");
    });

    thmbImages = $("ul.mediaThumbContainer img");

    thmbImages.each(function() {
        if (i == 0) { $(this).attr("clicked", "1"); }
        $(this).attr("index", i++);
    });


    $("ul.mediaThumbContainer img").bind("click", function() {
        ifClicked = $(this).attr("clicked");
        if (ifClicked != 1) {
            $("ul.mediaThumbContainer img[clicked=1]").attr("clicked", 0);
            image = $(this); altText = image.attr("alt"); imagePath = image.attr("src"); index = image.attr("index");
            parameterPosition = imagePath.lastIndexOf("/"); imageName = imagePath.substr(parameterPosition + 1);
            arrowBlock.remove().appendTo($(this).parent());
            $(this).attr("clicked", 1);
            $("ul.mediaContainer li:visible").fadeOut("normal", function() {
                bigImagesLi.eq(index).fadeIn("normal");
                //setContent(stableHomeHeight,page);
                //setContainerHeight();
            });
        }
    });

    // Image Controls.....................
    $(".ContinueCheckoutButton").bind("click", function() {
        buildScrollPane();
    });


    // News Rotator....................

    $('ul.news li:last').addClass('last');
    $('ul.news li:first').addClass('active');

    var stopFlag, Interval, timerParameter;

    timerParameter = 6000;
    Interval = setInterval("annChange()", timerParameter);

    var checkFlag = function() {
        if (stopFlag) { clearInterval(Interval); }
        else { Interval = setInterval("annChange()", timerParameter); };
    }

    $('ul.news li').bind(
		{
		    "mouseover": function() { stopFlag = true; checkFlag(); },
		    "mouseout": function() { stopFlag = false; checkFlag(); }
		});



        $(window).bind("beforeunload", function (e) {
            if (procutBasket) {
                return "Ürün sepetinizde satın almadığınız ürün bulunmaktadır!";
            }
        });
        $("a, button, input[type=submit], input[type=button], input[type=radio]").live("click", function (e) {
            procutBasket = false;
        });
        $("select").live("change", function(e) {
            procutBasket = false;
        });
        $(document).keydown(function (e) {
            if (e.keyCode == 116) {
                procutBasket = false;
            }
        });



    // News Rotator....................


    $("div.panelCollapseContainer a").not(":last").bind("click", function(e) {
        e.preventDefault();
        var linkIndex = $("div.panelCollapseContainer a").index(this);

        $("div.panel").hide();
        $("div.panel").eq(linkIndex).show();
        //buildScrollPane();

    });
});

// functions ................................
var setSideBar = function(windowHeight,stableHomeHeight) {
	$("div.SideBar, div.sideBarBlock").height(windowHeight-stableHomeHeight);
}

var setContent = function (stableHomeHeight, page) {
	if (($(window).height()-stableHomeHeight) <= $("div.Content").height()){
		$("div.contentBlock").height($("div.Content").height() +45);
	}
	else {
			$("div.contentBlock").height($(window).height()-stableHomeHeight+25);
		}
}	

var annChange = function()  {
	var a = $('ul.news li.active');
	var myclass= a.attr('class');
	
	a.fadeOut('slow', function() {
		$(this).removeClass('active');
		if (myclass == 'last active'){
			$('ul.news li:first').fadeIn('slow', function (){$(this).addClass('active');});
		}
		else {a.next().fadeIn('slow',function(){$(this).addClass('active')});}
	});
}


function mycarousel_initCallback(carousel) {			
	$('a.nextbtn').bind('click', function() {									  
		carousel.next();
		return false;
	});

	$('a.prvBtn').bind('click', function() {
		carousel.prev();
		return false;
	});
};

/*
var buildScrollPane = function() {
	// Scroll Pane Controls...................
	var stableHomeHeight;
	var windowHeight = $(window).height();
	var page = $("body").attr("class").split(" ");
	page = page[0];
	if (page == "Home" ) {stableHomeHeight =  201;}
	else if (page == "Page" || page == "Product" || page == "OnePage"){
		
		if (page == "Product") 
			stableHomeHeight = 185;
		else
			stableHomeHeight = 201-32;
			
		setSideBar(windowHeight,stableHomeHeight);
		setContent(stableHomeHeight,page);
	}
	
	var isResizing;
	// and the body scrollpane
	var setContainerHeight = function()
	{
		// IE triggers the onResize event internally when you do the stuff in this function
		// so make sure we don't enter an infinite loop and crash the browser
		if (!isResizing) { 
			isResizing = true;
			$w = $(window);
			$c = $('div#Body');
			var p = (parseInt($c.css('paddingLeft')) || 0) + (parseInt($c.css('paddingRight')) || 0);
			$('div.jScrollPaneContainer').css({height: $w.height()- stableHomeHeight, width: $w.width()});
			$c.css({height: ($w.height()-p), width: ($w.width() - p), overflow:'auto'});
			$c.jScrollPane({animateTo:true});
			$('a.anchor').bind(
					'click',
					function()
					{
						var targetElementSelectorString = $(this).attr('rel');
						$c.scrollTo(targetElementSelectorString);
						return false;
					}
				);
			isResizing = false;
			$("div.jScrollPaneContainer, div#Body").css("visibility","visible");
		}
	}
	
	$(window).bind('resize', function() {
		setSideBar($(this).height(),stableHomeHeight);
		setContent(stableHomeHeight,page);
		setContainerHeight();
	});
	
	setContainerHeight();setContainerHeight();
	setTimeout(function() {setContainerHeight();setSideBar(windowHeight,stableHomeHeight); setContent(stableHomeHeight,page);},200);	
}
	
$(document).ready(function() {   
	buildScrollPane();
});


window.onload = function() {
	buildScrollPane();
}

*/

/* ZOOM */
/*
$(function(){
	$.fn.Zoom = function(o) {
		
		o = $.extend({
			container : $("div.mediaGroupContainer .mediaContainer"),
			zoom : $("<div></div>").attr({className : "zoom"}).hide().appendTo("div.mediaGroupContainer .mediaContainer"),
			loading : $("<div>loading..</div>").attr({className:"zoomLoad"}).hide().appendTo("div.mediaGroupContainer .mediaContainer"),
			zoomOverlay : "",
			zoomWidth : 58,
			zoomHeight : 48,
			colorVariation : $("ul.mediaColorThumbContainer")
		});
		setTimeout(function() {
			$("ul.mediaThumbContainer img").eq(0).click();
		});
				
		o.container.live("mousemove", function(e) {
			
			
			if (e.pageX >= o.colorVariation.offset().left && e.pageX <= o.colorVariation.offset().left + o.colorVariation.width())
			{
				o.zoom.hide();
			}
			
			var left = e.pageX - $(this).offset().left - (o.zoom.width()/2);
			var top = e.pageY - $(this).offset().top - (o.zoom.height()/2);
			
			
			if (left < 0) 
			{
				left = 0;
			}
			if (left > o.container.width()-o.zoom.width())
			{
				left = o.container.width()-o.zoom.width();
			}
			
			if (top < 0)
			{
				top = 0;
			}
			if (top > o.container.height()-o.zoom.height()) 
			{
				top = o.container.height()-o.zoom.height();
			}
			
			$(this).find("div.zoom").css({
				left : left,
				top : top
			});
			
			
			if (o.zoomOverlay.find("img").width() <= o.container.find("li:visible img").width())
			{
				left = 0;
			}
			else
			{
				left = e.pageX - $(this).offset().left - (o.zoom.width()/2);
			}
			if (o.zoomOverlay.find("img").height() <= o.container.find("li:visible img").height())
			{
				top = 0;
			}
			else
			{
				top = e.pageY - $(this).offset().top - (o.zoom.height()/2);
			}
			
			o.zoomOverlay.find("img").css({
				left : -(left * (o.zoomOverlay.find("img").width() / o.container.find("li:visible img").width())),
				top : -(top * (o.zoomOverlay.find("img").height() / o.container.find("li:visible img").height()))
			})
		});
		
		o.container.bind("mouseover", 
			function(e){
				o.zoom.show().css({
					left : e.pageX - $(this).offset().left - (o.zoom.width()/2),
					top : e.pageY - $(this).offset().top - (o.zoom.height()/2),
					cursor : "move"
				});
				o.zoomOverlay.show();
			}
		);
		
		o.container.hover(function(){}, function(){
				o.zoom.hide();
				o.zoomOverlay.hide();
		})
		
		if ($("div.zoomOverLay").length <= 0) {
			o.zoomOverlay = $("<div></div>").attr({className : "zoomOverLay"}).prependTo("body").css({
				left : $(o.container).offset().left + $(o.container).width(),
				top : $(o.container).offset().top + 1,
				height : 250,
				width : 300
			})
		}
		if (o.loading) {
			o.loading.css({
				marginLeft : -((o.loading.width() + parseInt(o.loading.css("padding-left")) + parseInt(o.loading.css("padding-right"))) / 2),
				marginTop : -((o.loading.height() + parseInt(o.loading.css("padding-top")) + parseInt(o.loading.css("padding-bottom"))) / 2)
			});
		}
		
		return this.each(function() {
			$(this).bind("click", function() {
				o.loading.show();
				o.zoomOverlay.find("img").remove();
				var img = $("<img />").attr({src:$(this).attr("src").replace("icon", "large")}).load(function() { 
					img.prependTo("body").hide();
					o.zoomOverlay.css({
						width : (img.width() < o.zoomOverlay.width()) ? img.width() : o.zoomOverlay.width(),
						height : (img.height() < o.zoomOverlay.height()) ? img.height() : o.zoomOverlay.height()
					});
					var cloneimg = img.clone();
					cloneimg.prependTo(o.zoomOverlay).show();
					img.remove();
					o.loading.hide(); 
				});
			});
		});
	}
});


$(document).ready(function() {
	
	if ($("ul.mediaThumbContainer img").length > 0)
	{
		$("ul.mediaThumbContainer img").Zoom();
	}
	
});
*/
/* ZOOM */


/*Phone Validator*/

var previousValue = 0;

    function FormatTurkishPhoneNumber(controlId)
    {
                    var phoneTextBox = window.document.getElementById(controlId);
                    var phone = phoneTextBox.value.replace(/\(/g, "").replace(/\)/g, "").replace(/ /g, "").replace(/-/g, "");

                    if (phone.length == 0)
                    {
                                   phoneTextBox.value = '';
                    }
                    else if (previousValue > phone.length)
                    {
                                   previousValue = phone.length;
                                   return;
                    }
                    else
                    {
                                   previousValue = phone.length;
                                   
                                   if (phone.length <= 3)
                                   {
                                                   phoneTextBox.value = '(' + phone;
                                   }
                                   else if (phone.length <= 6)
                                   {
                                                   phoneTextBox.value = '(' + phone.substring(0, 3) + ') ' + phone.substr(3);
                                   }
                                   else
                                   {
                                                   phoneTextBox.value = '(' + phone.substring(0, 3) + ') ' + phone.substring(3, 6) + '-' + phone.substr(6);
                                   }
                    }
    };

