﻿//generic function to submit filters
$(document).ready(function() {


    //===== FANCYBOX ==============

    $("a.fancyPicture").fancybox({
        'speedIn': 600,
        'speedOut': 200,
        'overlayShow': true,
        'scrolling': 'no',
        'titleShow': false,
        'showCloseButton': true,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'orig': $("img.fancyPictureImg")
    });

    var stext = getUrlEncodedKey("stext", "");
    if (stext == "undefined")
        stext = "hey";
    else
        ($("#txtSearch").attr(stext));


    var DepartmentId = getUrlEncodedKey("DepartmentId", "");
    ($("#sltCategories").val(DepartmentId));


    $("#txtSearch").load(function() {

        // var stext = getUrlEncodedKey("stext", "");
        // ($("#txtSearch").val(stext));
        //        log(stext);
    });


    $("#sltCategories").change(function() {

        //        log("test");
        var stext = $("#txtSearch").val();
        if (stext == null)
            stext = getUrlEncodedKey("stext", "");

        var DepartmentId = $("#sltCategories").val();

        var store = getUrlEncodedKey("store", "");
        if (store == null)
            store = 326;
        var page = getUrlEncodedKey("page", "");
        //log(page + " this is page");
        page = 1;

        if (($("#sltCategories").attr("value")) == "Field1")
            DepartmentId = "";
        else
            DepartmentId = ($("#sltCategories").attr("value"));


//        log("to la8os einai meta");
//        setUrlEncodedKey("page", page, "");
        //        setUrlEncodedKey("stext", stext, "");



  //      setUrlEncodedKey("DepartmentId", DepartmentId, "");
        //log(setUrlEncodedKey("store", store, ""));



        //        log(setUrlEncodedKey("page", page, ""));
        //      log(setUrlEncodedKey("stext", stext, ""));
        var temp = new Array();
        temp = stext;

        if (stext == "Αναζητείστε τη μάρκα που ψάχνετε")
            self.location = "Brands.aspx?iid=29" + "&store=" + store + "&page=" + page + "&DepartmentId=" + DepartmentId;
        else
            self.location = "Brands.aspx?iid=29" + "&store=" + store /*+ "&stext=" + temp[0].toUpperCase()*/ + "&page=" + page + "&DepartmentId=" + DepartmentId;

        //        log(setUrlEncodedKey("store", store, ""));
    });


    $("#btnSearch").click(function() {



        var stext = $("#txtSearch").val();


        if (stext != "Αναζητείστε τη μάρκα που ψάχνετε") {

            //      if (stext != null)
            //        stext = getUrlEncodedKey("stext", "");

            var DepartmentId = $("#sltCategories").val();

            var store = getUrlEncodedKey("store", "");

            //        var page = getUrlEncodedKey("page", "");

            if (($("#sltCategories").attr("value")) == "Field1")
                DepartmentId = "";
            else
                DepartmentId = ($("#sltCategories").attr("value"));

            var page = 1;

            var temp = new Array();
            temp = stext;



            self.location = "Brands.aspx?iid=29" + "&store=" + store + "&stext=" + stext.toUpperCase() + "&page=" + page + "&DepartmentId=" + DepartmentId;

            setUrlEncodedKey("stext", stext, "");

            //            alert(stext);
            //        log(setUrlEncodedKey("stext",stext,""));
            //        log( setUrlEncodedKey("DepartmentId",DepartmentId,""))  ;
            //        log(setUrlEncodedKey("store",store,""));

        }
    });


    $("#txtSearch").change(function() {

        something = $(this).val()
        //log(something);
    });



    //MASTER SEARCH

    $("#btnMasterSearch").click(function() {

        var stext = $("#txtMasterSearch").val();
        document.Redi
        window.location = "SearchResults.aspx" + "?searchtext=" + stext;
        //    log(stext);


    });
});

function getUrlEncodedKey(key, query) {
    if (!query)
        query = window.location.search;
    var re = new RegExp("[?|&]" + key + "=(.*?)&");
    var matches = re.exec(query + "&");
    if (!matches || matches.length < 2)
        return "";
    return decodeURIComponent(matches[1].replace("+", " "));
}


function setUrlEncodedKey(key, value, query) {

    query = query || window.location.search;
    var q = query + "&";
    var re = new RegExp("[?|&]" + key + "=.*?&");
    if (!re.test(q))
        q += key + "=" + encodeURI(value);
    else
        q = q.replace(re, "&" + key + "=" + encodeURIComponent(value) + "&");
    q = q.trimStart("&").trimEnd("&");
    return q[0] == "?" ? q : q = "?" + q;
}

String.prototype.trimEnd = function(c) {
    if (c)
        return this.replace(new RegExp(c.escapeRegExp() + "*$"), '');
    return this.replace(/\s+$/, '');
}
String.prototype.trimStart = function(c) {
    if (c)
        return this.replace(new RegExp("^" + c.escapeRegExp() + "*"), '');
    return this.replace(/^\s+/, '');
}

String.prototype.escapeRegExp = function() {
    return this.replace(/[.*+?^${}()|[\]\/\\]/g, "\\$0");
};


function log(writeThis) {
    console.log(writeThis);
}


function submitFilters() {
    var $inputFld = $filterColumn.contents().find("select"), searchURL = "", prevName, $this, _name, _value;
    var paramsObj = function() { //object to store the parameters
        this.name = [];
        this.value = [];
        this.addName = function(val) { //name setter
            this.name.push(val);
        }
        this.addValue = function(val) { //value setter
            this.value.push(val);
        }
    }
}

function setText(itemId) {


    //        oTextarea = oForm.elements["txtSearch"];
    //      textarea_val = oTextarea.value;


    //        document.location = "Brands.aspx" + "&stext=" + oTextarea;
}


function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");

    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}
