/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function(){
    $("#selopt").change(function(){
        sendValue($(this).val());
    });
    $(".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){
       $('#price').html(data.price);
       $('#productDetails #dimentions').html(data.dimentions);
       $('#productDetails #material').html(data.material);
       $('#productDetails #color').html(data.color);
    },"json");
}

function sendSim(){

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

function addProduct(){
    $.post("ajax.php",{
        o: "ap" ,
        pid: getUrlVars()["pid"] ,
        sgid: getUrlVars()["sgid"],
        oid: $('#selopt').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()
    },
    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;
}
