var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;
var cart = 'hidden';

function addToCart(a) {
    var id_variants = a.target.hash.replace(/#add=/,'');
    var quantity = $('#q'+id_variants).get(0).value;
    $.get("/cart", {add: id_variants, quantity: quantity }, function(html){
			$("#cartDiv").empty().append(html);
		 }
	 ,'html');
}

function jsddm_open()
{  jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

function jsddm_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{  closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}

function switch_image()
{
    var link = $('#bImage');
    var title = link[0].title;
    var src = $('#img-container > img')[0].src.replace(/190x190/, 'br_62x62');
	$('#img-container > img')[0].src = $(this)[0].src.replace(/br_62x62/, 'br_190x190');
    href = $(this)[0].src.replace(/br_62x62/, '613x552');
	$(this)[0].src = src;
    link[0].href = href;
    link[0].title = $(this)[0].title;
    $(this)[0].title = title;

}

// analogicky kopíruje funkci třídy
// Products::makeOrderCode()
function makeOrderCode(orderCode, orderCodeAdd) {
    if (orderCodeAdd && (orderCode.search(/\*/) >=0 && orderCodeAdd.search(/\*/) >= 0)) {
        return orderCode.replace(/\*/,orderCodeAdd.replace(/\*/,''));
    }
    
    if (orderCodeAdd) {
        return orderCode + ' ' + orderCodeAdd;
    }

    return orderCode;
}

// přepínání variant
function switchVars(o) {
    var x='';
    $('.variants').removeClass('selected');
    $(o.target).addClass('selected');

    var color
    $('.colors').each(function(i) {
        if (this.checked == true) {
             var e = this.value;
             color = $("#color-def-" + parseInt(e)).get(0).innerHTML;
        }
     });
    
    $('#add-product-variant').get(0).value = o.data.id;

    var price;
    var spec_price;
    var del_price;

    for (var i in o.data) {
        if (i == 'order_code') {
            $('#add-product-variant-code').get(0).value = o.data[i];
            $('#code_value_' + i).empty().append(makeOrderCode(o.data[i], color));
        }
        else if (i == 'price') {
            price = o.data[i];
        }
        else if (i == 'special_price') {
            spec_price = o.data[i];
        }
        else {
            if (o.data[i] != '') {
                $('#tex_value_' + i).css('display','block');
                $('#tex_label_' + i).css('display','block');
                $('#tex_value_' + i + ' span').empty().append(o.data[i]);
            }
            else {
                $('#tex_value_' + i).css('display','none');
                $('#tex_label_' + i).css('display','none');
            }
        }
    }
    if (spec_price) { del_price = price; price = spec_price; }
    $('#product_price_value_price').empty().append(price);
    $('#product_price_value_del_price').empty().append(del_price);
}

// přepínání barev
function switchColor (o) {
    
    var order_code = $('#add-product-variant-code').get(0).value;
    
    $('.colors').each(function(i) {
        if (this.checked == true) {
            var e = this.value;
            var color = $('#color-def-' + e).get(0).innerHTML;
            $('#add-color').get(0).value = e;
            $('#code_value_order_code').empty().append(makeOrderCode(order_code, color));
        }
    });
}


$(document).ready(function()
{  
    // addToCart BIND
    $('.addToCart').unbind().bind('click',  addToCart);

	// switch images
	$('#other-images > div > img').bind('click',  switch_image);
  
    // big image
    $("#main-image").photos({});

});

document.onclick = jsddm_close;

