﻿
var CriteriaJsonURL = "/FindCriterias";
var ProductJsonURL = "/FindProducts";

var ColorParam=null;
var PageParam=1;
var ColumnCount=5;
var Keyword="";
var ParamsCriteria = "";
var LangID = "tr-tr";
var LangID2 = '';

$.address.init(function(event) {
    //log('init: ' + event.value);
}).change(function(event) {
    
    //var text = (event.value == '/') ? 'Home' : 
    //event.pathNames[0].substr(0, 1).toUpperCase() + 
    //event.pathNames[0].substr(1);
    //$('a').each(function() {
    //	$(this).toggleClass('selected', $(this).text() == text);
    //});
}).internalChange(function(event) {
    //log('internalChange: ' + event.value);
}).externalChange(function(event) {
    BoxInit();
    //log('externalChange: ' + event.value);
});


$(document).ready(function() {
    $('#keyword').keypress(function(e) {
        if (e.keyCode == 13) {
            kyw = $('#keyword').val();
            onSearch(kyw);
            return false;
        }
    });
    $('#keyword2').keypress(function(e) {
        if (e.keyCode == 13) {
            kyw = $('#keyword2').val();
            location.href = "/tr-TR/products.aspx#/Keyword=" + encodeURI(kyw);
            return false;
        }
    });
    $(".ara").click(function() {

    kyw = $('#keyword').val();
    onSearch(kyw);
    return false;
        
    });
});


var ThbWidth = 127;
var currProd = 0;
function scrol_jump(GetPo, GetDiv) {

    if (!GetDiv)
        GetDiv = "galleryBox";

    var prodWidth = $("#" + GetDiv).children("div.galleryItem").size();

    currProd = currProd + GetPo;
    GalLeft = (currProd*-127)

    if ((currProd >= 0) && (currProd <= (prodWidth - 5))) {
        $("#" + GetDiv).animate({
            left: GalLeft + "px"
        }, 250);
    } else {
        currProd = currProd - GetPo;
    }
}





// using (multiline textbox, Current label count, multiline textbox maxlength)
function GetBigImg(Gimg,Gfull) 
{
    //var oImgBox = document.getElementById('ctl00_ContentPlaceHolder1_prodBigImg');
    //	oImgBox.href=Gfull;
    //var href = document.getElementById("prodHref");

    $('#prodHref').prepend('<div class="iloading">&nbsp;</div>');
    
	var img = new Image();
    $(img).load(function () {
        $(this).hide();
        $(".iloading").remove();
        $('#prodHref').attr('href', Gfull).html(this);
        $(this).fadeIn();
    }).error(function () {
    }).attr('src', Gimg);
    	
}

function GetMainMenu(){
	var MainMenu = document.getElementById('MainMenu');
	var SubMenu = document.getElementById('SubMenu');
	
	if (MainMenu){
	MainMenu.style.display="block";
	}
	
	if (SubMenu){
	    SubMenu.style.display="none";
	}
}

function OpenSubProds(GID,lng){
    //$("body").prepend("");
    $("body").prepend("<div class='subProdPanel' id='subProdPanel'><div class='subProdAjax' id='subProdAjax'></div><div class='subProdClose'><a href=\"javascript:;\" id='popUpClose' class='popUpClose' onclick=\"CloseSubProds();\"><img src='/images/buttonPopupClose.jpg' /></a></div></div>");
    $("body").prepend("<div class='subProdBg' id='subProdBg' onclick=\"CloseSubProds();\">&nbsp;</div>");

    if (lng != '')
        $("#subProdAjax").load("/" + lng + "/ProductDetailHandler.aspx?" + GID);
    else
        $("#subProdAjax").load("/ProductDetailHandler.aspx?" + GID);


        SubLeft = ($("body").width() - 897) / 2;

        SubTop = $(window).scrollTop() + 50;

        $("#subProdPanel")
			.css("top", SubTop + "px")
			.css("left",SubLeft+"px")
			.fadeIn("fast");

    }

    function OpenSubProds2(GID) {
        //$("body").prepend("");
        $("body").prepend("<div class='subProdPanel2' id='subProdPanel'><div class='subProdAjax' id='subProdAjax'></div><div class='subProdClose'><a href=\"javascript:;\" id='popUpClose' class='popUpClose' onclick=\"CloseSubProds();\"><img src='../images/buttonPopupClose.jpg' /></a></div></div>");
        $("body").prepend("<div class='subProdBg' id='subProdBg' onclick=\"CloseSubProds();\">&nbsp;</div>");

        $("#subProdAjax").load("ProductDetailHandler.aspx?" + GID);


        SubLeft = ($("body").width() - 470) / 2;

        SubTop = $(window).scrollTop() + 50;

        $("#subProdPanel")
			.css("top", SubTop + "px")
			.css("left", SubLeft + "px")
			.fadeIn("fast");

    }

LastSales="";
LastSalesID = "Sa_0";
function GetSalePoints(GID,Gelement)
{
    if(LastSalesID!=""){
        $(LastSales).css("background-image", "url(../images/arrow_title_bottom.png)");
        $('#'+LastSalesID).css("display", "none");
    }
    
    $(Gelement).css("background-image", "url(../images/arrow_title_top.png)");
    $('#'+GID).css("display", "block");
    
    LastSales=Gelement;
    LastSalesID=GID;
}

function CloseSubProds(){
    $("#subProdBg").remove();
    $("#subProdAjax").remove();
    $("#popUpClose").remove();
}

function BoxInit()
{
    if ($.address.value() != "/") {
        RenderProduct($.address.value().replace("/",""));
        FillBoxes();

        StartNum =$.address.value().indexOf("SearchType");
        EndNum = $.address.value().indexOf("&Page");

        ParamsCriteria = $.address.value().substring(StartNum, EndNum);
    }
    else
        GetItems();
	
}

function FillBoxes() {
    //alert($.address.value().replace("partial", "bulk"));
    ClearNext(-1);
    var rq = CriteriaJsonURL;
    rq += '?' + $.address.value().replace("/","").replace("partial","bulk");
    
    $.getJSON(rq, function(data) {

        $.each(data, function(ix, dt) {
            //data.ItemIndex;
            //data.ItemTitle;

            CreateNext(dt.ItemIndex, dt.ItemTitle);
            //data.Items;

            var SubMenu = "<ul>";
            $.each(dt.Items, function(i, item) {
                MCss = "";
                if (item.IsSubNodes == 0)
                    MCss = "noSub";

                if (item.IsCurrent == 1)
                    MCss = MCss + " current";
                
                SubMenu += "<li><a class='" + MCss + "' href=\"javascript:;\" onclick=\"OnItemClick(" + dt.ItemIndex + ",'" + item.ItemTitle + "','" + item.ItemParams + "'," + item.IsSubNodes + ",this);\">" + item.ItemTitle + "</a></li>";
            });

            SubMenu += "</ul>";
            $("#S_" + dt.ItemIndex).html(SubMenu);
        });
    });
}

function GetItems() {
    ClearNext(-1);
    if (LangID.toLowerCase()=="en-us")
        CreateNext(0, 'Search Types');
    else if (LangID.toLowerCase() == "de-de")
        CreateNext(0, 'Suchtypen');
    else
        CreateNext(0, 'Arama Tipleri');
	    
	AjaxGetItem(0, "Fill=partial&LangID="+LangID);
}





function Toogle(itemThis,itemIndex)
{
	var prods = document.getElementById('S_'+itemIndex).getElementsByTagName('a');
    for(var i=0;i<prods.length;i++){
        if ((prods[i].className == 'current') || (prods[i].className == 'noSub current') || (prods[i].className == ' current')) {
            $(prods[i]).removeClass("current");
        }
    }
	
	$(itemThis).addClass("current");
}

function ClearNext(itemIndex)
{
	$("#prodListMenu").children().each(function(n,itm) {
		if(itemIndex<n)
		$("#" + itm.id).remove();
    });
}

function CreateNext(itemIndex,itemTitle){
	$("#prodListMenu")
		.append("<div class='prodListMenuItm' id='M_"+itemIndex+"'></div>");
	$("#M_"+itemIndex)
		.append("<div class='prodListMenuTitle'>"+itemTitle+"</div>")
		.append("<div class='prodListMenuDesc' id='S_"+itemIndex+"'></div>");
	$("#S_"+itemIndex).html("<div class='loading'><img src='../images/loading.gif' /></div>");
	
	
	// scrolling
	A=0;
	$("#prodListMenu").children().each(function(n,itm) {
		A++;
    });
	if (A>4){
		Wa=(A-4)*229;	
		$("#prodListMenu").animate({ 
			left: '-'+Wa+'px'
		}, 1500 );
	}else{
		$("#prodListMenu").animate({ 
			left: '0px'
		}, 1500 );
	}
	

}


function CreateBox(itemIndex, itemThis, itemTitle, itemParams, isSubNodes, CallBackMethod) 
{
    Toogle(itemThis, itemIndex);
    ClearNext(itemIndex);
    if (isSubNodes == 1) {
        CreateNext(itemIndex + 1, itemTitle);
        AjaxGetItem(itemIndex + 1, itemParams);
    }
    ParamsCriteria = itemParams;
    PageParam = 1;
    ColorParam = 0;
    var Params = GetParams();
}

function AjaxGetItem(itemIndex,itemParams){
	
		var rq = CriteriaJsonURL;
		if (itemParams != '')
			rq += '?' + itemParams;

		$.getJSON(rq, function(data) {

			var SubMenu = "<ul>";
			
			$.each(data[0].Items, function(i,item){
			    MCss="";
			    if(item.IsSubNodes==0)
			        MCss="class='noSub'";
			        
				SubMenu += "<li><a "+MCss+" href=\"javascript:;\" onclick=\"OnItemClick("+data[0].ItemIndex+",'"+item.ItemTitle+"','"+item.ItemParams+"',"+item.IsSubNodes+",this);\">"+item.ItemTitle+"</a></li>";
			  });
		  
			SubMenu += "</ul>";
			$("#S_"+itemIndex).html(SubMenu);
		});

		
}

function HrefMarkup(AllParams){
	$.address.value(AllParams);
}

function GetImage(filePath) 
{
    if (filePath != "") {
        return filePath;
    }
    else {
        if (ColumnCount == 7)
            return "/images/blank_t1.jpg";
        else if (ColumnCount == 5)
            return "/images/blank_t2.jpg";
        else if (ColumnCount == 3)
            return "/images/blank_t3.jpg";
    }

}
function RenderProduct(AllParams) {
    
    var strHref = $.address.value().toLowerCase();
    if ((strHref.indexOf("materialid") != -1) || (strHref.indexOf("ambianceid") != -1) || (strHref.indexOf("groupid") != -1) || (strHref.indexOf("keyword") != -1)) {
        //
        if($("#prodListPanel").css("display")=="none")
            $("#prodListPanel").css("display","block");
    } else {
        if($("#prodListPanel").css("display")!="none")
            $("#prodListPanel").css("display","none");
        return;
    }

	$("#prodListPanel").html("<img src='/images/loading.gif' />");
	$("#prodListColor").html("<img src='/images/loading.gif' />");

	$.getJSON(ProductJsonURL + '?' + AllParams, function(data) {

	    var ProductList = "";

	    prodCss = "";
	    if (ColumnCount == 3) {
	        prodCss = "proListItemSort3";
	    } else if (ColumnCount == 5) {
	        prodCss = "proListItemSort2";
	    } else if (ColumnCount == 7) {
	        prodCss = "proListItemSort1";
	    }

	    $.each(data.Products, function(i, prod) {
	        ProductList += "<div class='proListItem " + prodCss + "'><a href='" + prod.Link + "'><img src='" + GetImage(prod.Img) + "' /><span>" + prod.Title + "</span></a></div>";
	    });

	    ProductList += "<div class='cleaner'></div>";

	    //Paging
	    if (data.PageTotal > 1) {

	        ProductList += "<div class='prodListPaging'>";
	        ProductList += "<ul>";
	        if (data.PageCurrent > 1) {
	            ProductList += "<li><a href=\"javascript:onPageChanged('" + (parseInt(data.PageCurrent) - 1) + "')\" class='prev'>&nbsp;</a></li>";
	        }

	        Lstart = (data.PageCurrent - 5);
	        Lend = (data.PageCurrent + 5);

	        if (Lstart < 1) {
	            Lend = Lend + (Lstart * -1);
	            Lstart = 1;
	        }

	        if (Lend > data.PageTotal) {
	            Lstart = Lstart - (Lend - data.PageTotal);
	            Lend = data.PageTotal;
	        }
	        if (Lstart < 1) {
	            Lstart = 1;
	        }
	        //alert("Başla:"+Lstart+" Bitir:"+Lend);


	        //for (var i = 1; i <= data.PageTotal; i++) {
	        for (var i = Lstart; i <= Lend; i++) {
	            if (data.PageCurrent == i) {
	                ProductList += "<li><a href=\"javascript:onPageChanged('" + i + "')\" class='current'>" + i + "</a></li>";
	            } else {
	                ProductList += "<li><a href=\"javascript:onPageChanged('" + i + "')\">" + i + "</a></li>";
	            }
	        }

	        if (data.PageCurrent < data.PageTotal) {
	            ProductList += "<li><a href=\"javascript:onPageChanged('" + (parseInt(data.PageCurrent) + 1) + "')\" class='next'>&nbsp;</a></li>";
	        }
	        ProductList += "</ul>";
	        ProductList += "</div>";
	    }
	    $("#prodListPanel").html(ProductList);


	    //ColorBox
	    var prodListColor = "";

	    if (LangID.toLowerCase() == "en-us") {
	        prodListColor += "<div class='colorText'>Color Options: </div>";
	    } else {
	        prodListColor += "<div class='colorText'>Renk seçenekleri: </div>";
	    }

	    var ColorCount = 0;
	    prodListColor += "<ul>";
	    $.each(data.Colors, function(i, color) {
	        ColorCount++;
	        if (color.IsSelected == 1) {
	            prodListColor += "<li class='current'><a title=\"" + color.Name + "\" href=\"javascript:onColorDelete('" + color.ID + "');\"><img src='../images/color_delete.jpg' /></a></li>";
	        } else {
	            prodListColor += "<li><a title=\"" + color.Name + "\" href=\"javascript:onColorChanged('" + color.ID + "');\"><img src='" + color.Img + "' /></a></li>";
	        }
	    });
	    prodListColor += "</ul>";
	    prodListColor += "<div class='cleaner'></div>";

	    if (ColorCount != 0) {
	        $("#prodListColor").html(prodListColor);
	    } else {
	        $("#prodListColor").html("");
	    }

	});
}




function GetParams()
{   
    var Param = "";
    if(ParamsCriteria!="")
        Param += ParamsCriteria;
    if(PageParam!=null)
        Param += "&Page=" + encodeURI(PageParam);
    if(ColumnCount!=null)
        Param += "&RowsCount=" + encodeURI(ColumnCount*3);
    if(ColorParam!=null)
        Param += "&Color="+ encodeURI(ColorParam);
    if(Keyword!="")
        Param += "&Keyword=" + encodeURI(Keyword);
    if (LangID2 != "")
        Param += "&LangID2=" + encodeURI(LangID2);
    
    if(Param.substring(0,1)=="&")
        Param = Param.substr(1);
    
    return Param;
}






//Event Functions
function OnItemClick(itemIndex,itemTitle,itemParams,isSubNodes,itemThis){
	
	Toogle(itemThis,itemIndex);
	ClearNext(itemIndex);

	ParamsCriteria = "Fill=partial&LangID="+ LangID +"&"+ itemParams;
	PageParam = 1;
	ColorParam = 0;
	
	if(isSubNodes==1){
		CreateNext(itemIndex+1,itemTitle);
		AjaxGetItem(itemIndex + 1, ParamsCriteria);
	}
	
	var Params = GetParams();

	HrefMarkup(Params);
	RenderProduct(Params);
}

//Sütun sayısı değişti
function onSliderChanged(sCount)
{
    if(sCount!=null)
    {
        if(sCount=="1")
            ColumnCount = 7;
        else if(sCount=="2")
            ColumnCount = 5;
        else
            ColumnCount = 3;
        PageParam = 1;
        
        var Params = GetParams();
        
        HrefMarkup(Params);
	    RenderProduct(Params);   
    }
    
}

//Renk seçildi
function onColorChanged(color)
{
    if(color!=null)
    {
        ColorParam = color;
        PageParam = 1;
        
        var Params = GetParams();
        
        HrefMarkup(Params);
	    RenderProduct(Params);
    }
}

function GetPageCount(GetCount){
	
	if(GetCount!=null)
    {
		ColumnCount = GetCount;

        PageParam = 1;
        
        var Params = GetParams();
        
        HrefMarkup(Params);
	    RenderProduct(Params);   
    }
}

//Renk silindi
function onColorDelete(color)
{
    if(color!=null)
    {
        ColorParam = 0;
        PageParam = 1;
        
        var Params = GetParams();
        
        HrefMarkup(Params);
	    RenderProduct(Params);
    }
}

//Sayfa Değişti
function onPageChanged(page)
{
    if(page!=null)
    {
        PageParam = page;
        
        var Params = GetParams();
        
        HrefMarkup(Params);
	    RenderProduct(Params);
    }
}

//Metinsel Arama
function onSearch(keyw)
{
    if(keyw!=null)
    {
        Keyword = keyw;
        PageParam = 1;
        //var Params = GetParams();
        var Params = "RowsCount=" + encodeURI(ColumnCount * 3) + "&Keyword=" + Keyword;
        
        
        
        HrefMarkup(Params);
	    RenderProduct(Params);
    }
}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


function OpenFastMenu() {
    
    var Toggle = $("#fastMenu").css("display");

    if (Toggle == "none") {
        $("#fastMenuBack").show(0);
        $("#fastMenu").show(800);
    } else {
        $("#fastMenuBack").hide(0);
        $("#fastMenu").hide(800);
    }
}


function socialIco(id) {

    var s_title = encodeURI("Kale");

    var s_url = encodeURI(window.location);

    var w_bar;
    var w_url = "";

    if (id == 0) {

        w_url = "http://www.facebook.com/sharer.php?t=" + s_title + "&amp;u=" + s_url;
        w_bar = "height=400, width=600, titlebar=no, status=no, toolbar=no, menubar=no, location=no, resizable=no, scrollbars=1"
    }
    else if (id == 1) {
        w_url = "http://twitter.com/home?status=" + s_title + " " + s_url;
        w_bar = "height=400, width=800, titlebar=no, status=no, toolbar=no, menubar=no, location=no, resizable=no, scrollbars=1"
    }
    else if (id == 2) {
        w_url = "http://del.icio.us/post?v=4&&noui&jump=close&title=" + s_title + "&url=" + s_url;
        w_bar = "height=400, width=800, titlebar=no, status=no, toolbar=no, menubar=no, location=no, resizable=no, scrollbars=1"
    }
    else if (id == 3) {
        w_url = "http://digg.com/submit?title=" + s_title + "&url=" + s_url;
        w_bar = "height=400, width=600, titlebar=no, status=no, toolbar=no, menubar=no, location=no, resizable=no, scrollbars=1"
    }
    else if (id == 4) {
        w_url = "http://friendfeed.com/share/bookmarklet/frame#title=" + s_title + "&url=" + s_url;
        w_bar = "height=400, width=600, titlebar=no, status=no, toolbar=no, menubar=no, location=no, resizable=no, scrollbars=1"
    }


    
    window.open(w_url.replace(/amp;/gi, ""), "", w_bar, w_url);

}
