/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var picsurl = "http://www.aiko-bg.com/i_pictures/";

$(document).ready(function(){
    $("select#seldim").each(function(){
    var choosedoid = $(this).find("option:selected").val();
    $.post("ajax_serie.php", {o: 'seldim_changed', sgid: getUrlVars()["sgid"], pid: getUrlVars()["pid"], oid: choosedoid},
    function(data) { 
    if (data != null && data != ''){
        var html = "";
        var data_decoded= jQuery.parseJSON(data);
        $.each(data_decoded, function(k, v) {
            if (getUrlVars()["oid"] == v['oid'])
            {
                html = html+"<option value='"+v['oid']+"' selected>"+v['color']+"</option>";
                if (v['pic'] != null)
                    {
                        var href = picsurl+v['pic'];
                        $('a[rel*=lightbox]').find('img').attr('src',href);
                        $('a[rel*=lightbox]').attr('href',href);
                    } else {
                        var href = picsurl+v['opic'];
                        $('a[rel*=lightbox]').find('img').attr('src',href);
                        $('a[rel*=lightbox]').attr('href',href);
                    }
            }
            else {html = html+"<option value='"+v['oid']+"'>"+v['color']+"</option>";}
         });
        $("select#selcol").html(html);
        if (countProperties(data_decoded)>=1 && data_decoded[0].color != "" && data_decoded[0].color != null)
        {
        $("div#divcol").show();
        } else {$("div#divcol").hide()}
        sendValue($("select#selcol").val());

        }
    });
    });
    
    $("select#seldim").change(function(){
        var choosedoid = $(this).find("option:selected").val();
        $.post("ajax_serie.php", {o: 'seldim_changed', sgid: getUrlVars()["sgid"], pid: getUrlVars()["pid"], oid: choosedoid},
        function(data) { 
        if (data != null && data != ''){
            var html = "";
            var data_decoded= jQuery.parseJSON(data);
            $.each(data_decoded, function(k, v) {
                if (choosedoid == v['oid'])
                {
                    html = html+"<option value='"+v['oid']+"' selected>"+v['color']+"</option>";
                    if (v['pic'] != null)
                        {
                            var href = picsurl+v['pic'];
                            $('a[rel*=lightbox]').find('img').attr('src',href);
                            $('a[rel*=lightbox]').attr('href',href);
                        } else {
                            var href = picsurl+v['opic'];
                            $('a[rel*=lightbox]').find('img').attr('src',href);
                            $('a[rel*=lightbox]').attr('href',href);
                        }
                }
                else {html = html+"<option value='"+v['oid']+"'>"+v['color']+"</option>";}
             });
            $("select#selcol").html(html);
              if (countProperties(data_decoded)>=1 && data_decoded[0].color != "" && data_decoded[0].color != null)
            {
            $("div#divcol").show();
            } else {$("div#divcol").hide()}
            sendValue($("select#selcol").val());

            }
        });
    });

    $("select#selcol").change(function(){
        var picturenow = $('a[rel*=lightbox]').find('img').attr('src');
        $.post("ajax_serie.php", {o: 'selcol_changed', oid: $("#selcol").val()},
        function(data) { 
        //alert(data);
        if (data != null && data != 'err'){
            $("a[rel*=lightbox]").each(function(){
                if (data != picturenow)
                {
                    $(this).attr('href',data);
                    $(this).find('img').attr('src',data);
                }
            });
            }
        });
        sendValue($(this).val());
    });
     
    $("#numitems_chooser").change(function(){
        $.post("index.php", {numitems_chooser: $("#numitems_chooser").val()},
           function(data) { 
               var sgid = getUrlVars()["sgid"];
               var show = getUrlVars()["show"];
               var searchinput = getUrlVars()["searchInput"];
               var store = getUrlVars()["store"];
               var pfrom = getUrlVars()["pfrom"];
               var pto = getUrlVars()["pto"];
               var nn = getUrlVars()["nn"];
               var newurl = "index.php?show="+show;
               if (sgid != null) newurl += "&"+"sgid="+sgid;
               if (searchinput != null) newurl += "&"+"searchInput="+searchinput;
               if (store != null) newurl += "&"+"store="+store;
               if (pfrom != null) newurl += "&"+"pfrom="+pfrom;
               if (pto != null) newurl += "&"+"pto="+pto;
               if (nn != null) newurl += "&"+"nn="+nn;
               window.location = newurl;
               });
           
 
    });
    
    $("#selopt").keypress(function(){
        $("#selopt").change();
    });
    $(".relatedProducts a.btnMoreP").click(function(e){
        e.preventDefault();
        sendSim();
    });
    $("#myprod").click(function(e){
        e.preventDefault();
        addProduct();
    });
    $("#myprod_p").click(function(e){
        e.preventDefault();
        addPProduct();
    });
    $(".order_textarea a").click(function(e){
        e.preventDefault();
        var rowid = $(this).next().attr('id');
        remProduct(rowid);
    });

    $('#opr #closeopr').click(function(e){
       e.preventDefault();
       $('#opr').hide();
    });  
});

function sendValue(str){
    $.post("ajax.php",{o: "p" , oid: str},
    function(data){

       $('#serienn').html(data.serienn);
       
       if (data.promo_price != null)
       {
           $('#priceInfo').html(data.promo_price);
       }
       else {$('#priceInfo').html(data.price);}
       $('#myprod').html(data.purchase_button_text);
       $('#myprod').attr('title', data.purchase_button_text);

    },"json");
}

function sendSim(){

    $.post("ajax.php",{o: "si" , pid: getUrlVars()["pid"] , sgid: getUrlVars()["sgid"]},
    function(data){
       $('#simprod').html(data);
       //alert(data);
    });
}

function addProduct(){
    $.post("ajax.php",{
        o: "ap" ,
        pid: getUrlVars()["pid"] ,
        sgid: getUrlVars()["sgid"],
        oid: $('#selcol').val()
    },
    function(data){
        result = data.result;
        if (result == 'ok'){
            $('#header_menu #mp').html(data.html);
            $('#dialog').html(data.msg);
            $('#dialog').dialog({modal: true});
        }else if (result == 'err'){
            $('#dialog').html(data.msg);
            $('#dialog').dialog({modal: true});
        }
    },"json");
}
function addPProduct(){
    $.post("ajax.php",{
        o: "app" ,
        promo_id: getUrlVars()["promo_id"] ,
        sgid: getUrlVars()["sgid"],
        oid: $('#selopt').val(),
	nn: getUrlVars()["nn"],
        storeid: getUrlVars()["store"]
    },
    function(data){
        result = data.result;
        if (result == 'ok'){
            $('#header_menu #mp').html(data.html);
            $('#dialog').html(data.msg);
            $('#dialog').dialog({modal: true});
        } else if (result == 'err'){
            $('#dialog').html(data.msg);
            $('#dialog').dialog({modal: true});
        }
    }, "json");
}

function remProduct(rowid){

    $.post("ajax.php",{o: "rp" , ix: rowid},
    function(data){
        $('#header_menu #mp').html(data.html);
        if (data.status == 'ok'){
            $("#orderDetails tr#" + rowid).remove();
            if (data.cnt == 0){
                $(".contentBoxTop + form[name*='myprod']").remove();
            }
        }
        

    }, "json");
}

function getUrlVars(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
function countProperties(obj) {
  var prop;
  var propCount = 0;

  for (prop in obj) {
    propCount++;
  }
  return propCount;
}

