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(/br_190x190/, 'br_62x62');
	$('#img-container > img')[0].src = $(this)[0].src.replace(/br_62x62/, 'br_190x190');
    href = $(this)[0].src.replace(/br_62x62/, '558x552');
	$(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;
}

function openActiveAccordion(ele) {
    var par = $(ele).get(0).parentNode;
    par = $(par);

    if ($(ele).accordion('option','collapsible') == undefined) {
        if (par.accordion('option','collapsible') != undefined) {
            return $(ele);
        }
        else {
            return openActiveAccordion($(ele).get(0).parentNode);
        }
    }
    else { 
        return $(ele);
    }
}

$(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;

