$(document).ready(function(){
	$("a[href='#']").click(function(event){
		event.preventDefault();
	});
	
	if($('#cart-total-order').length > 0)
	{
		$('#cart-total-order').corner();
	}
	
	if($('.cashier-details').length > 0)
	{
		$('.cashier-details').corner();
	}
	
	if($('#cashier-overview-products').length > 0)
	{
		$('.cashier-overview-box').corner();
		$('#cashier-overview-final-price').corner();
	}
	
	if($('#shirt-images').length > 0){
		$('#shirt-images').easySlider({numeric : true});
     
		$('a.lightbox').lightBox();
	}

	if($('#shirt-images-wider').length > 0){
		$('#shirt-images-wider').easySlider({numeric : true});
     
		$('a.lightbox').lightBox();
	}
	
  if($('.shirt-middle a img').length > 0 && $('.scaling-id').length > 0){
		
	$("div.shirt-middle a").hover(function() {
	    var image = $(this).find('img');
	    var curr_img = $(image).attr('src');
    
        var alt = $(image).attr('class').split('-');
	var new_img = alt[0];
	var base_href = document.getElementsByTagName('base')[0].href;
	
        if(alt.length > 1) {
            return;
        }
        
	    if(new_img != base_href)    {
	        $(this).css({'z-index' : '10'}); /*Add a higher z-index value so this image stays on top*/ 
	        $(this).find('img').addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup*/
            .animate({
			    marginTop: '-25px', /* The next 4 lines will vertically align this image */ 
			    marginLeft: '-16px',
			    top: '50%',
			    left: '50%',
			    width: '200px', /* Set new width */
			    height: '267px', /* Set new height */
			    padding: '0'
		    }, 400).attr('class', curr_img + '-0').attr('src', new_img); /* this value of "200" is the speed of how fast/slow this hover animates */ 	
	    }
	} , function() {
	    var image = $(this).find('img');
	    var curr_img = $(image).attr('src');
    
	    var alt = $(image).attr('class').split('-');
	    var new_img = alt[0];
	    var base_href = document.getElementsByTagName('base')[0].href
	    
	    if(new_img != base_href){
		    $(this).css({'z-index' : '0'}); /* Set z-index back to 0 */
		    $(this).find('img').removeClass("hover").stop()  /* Remove the "hover" class , then stop animation queue buildup*/
		    .animate({
				    marginTop: '0', /* Set alignment back to default */
				    marginLeft: '0',
				    top: '0',
				    left: '0',
				    width: '167px', /* Set width back to default */
				    height: '250px', /* Set height back to default */
				    padding: '0'
			}, 800, function(){ if(alt.length == 1) return; $(image).attr('class', curr_img).attr('src', new_img);});
		}
	});
  }
});

function shirt_motive_in(image_id, new_image){	
	$('#' + image_id).attr('src', new_image);
}

function shirt_motive_out(image_id, old_img){
	$('#' + image_id).attr('src', old_img);	
}

function shirts_cart_add(shirt_id)
{
	var page_url = document.getElementsByTagName('base')[0].href;
	var color = $('#product-color').val();
	var size = $('#product-size').val();
	var quantity = $('#product-quantity').val();
        var type = $('#product-type').val();
	
	$.post( page_url + 'ajax/shirts/add-to-cart/', { product_id : shirt_id, color_code : color, size_name : size, quantity : quantity, type : type }, function(data)
		{
			if(data == "true")
			{
				shirts_get_small_cart();
			}		
			else
			{
				alert('Unknow error has occured: ' + data);			
			}
		}
	);
	
	if($('#shop-product-added').length > 0)
	{
		$('#shop-product-added').dialog({
			hide : 'slide',
			modal : true,
			width: 350,
			resizable : false,
			buttons : {
				'V redu' : function()
				{
					$(this).dialog('close');
				}
			}
		});
	
		if(! $('#shop-product-added').dialog('isOpen'))
		{
			$('#shop-product-added').dialog('open');
		}
	}
	
	setTimeout('shirts_product_added_fade()', 3000);
}

function shirts_product_added_fade()
{
	$('#shop-product-added').dialog('close');
}

function shirts_get_small_cart()
{
	var page_url = document.getElementsByTagName('base')[0].href;
	
	var content = null;
	
	$.post( page_url + 'ajax/shirts/small-cart/', function(data)
		{
			$('#shirts-cart').html(data);		
		}
	);
}

function shop_on_page(id)
{
	var count = $('#' + id).val();
	var page_url =  document.getElementsByTagName('base')[0].href;
	$.post(page_url + 'ajax/shirts/change-on-page/', { count : count}, function(data){
		if(data === 'true'){
			window.location.reload();	
		}
	});
}

function shop_remove_cart_item(cart_key)
{
	var page_url = document.getElementsByTagName('base')[0].href;
	
	$.post( page_url + 'ajax/shirts/remove-cart-item/', {cart_key : cart_key}, function(data){
			if(data != 'false'){
				$('#product-' + cart_key).fadeOut();
				$('#cart-total-order-price').text(data + ' €');
				
				if(data == '0,00')
				{
					window.location.reload();	
				}
				
				shirts_get_small_cart();
			}
		}	
	);
}
function shop_set_quantity(cart_key)
{
	var page_url = document.getElementsByTagName('base')[0].href;
	var quantity = $('#quantity-' + cart_key).val();
	
	$.post(page_url + 'ajax/shirts/set-quantity/', {cart_key : cart_key, quantity : quantity}, function(data){
		if(data == 'true')
		{
			window.location.reload();
		}				
		else
		{
      alert(data);
    }
	});
}

function shop_ajax_pickup(option){
	var page_url = document.getElementsByTagName('base')[0].href;
	
	$.post(page_url + 'ajax/shirts/set-pickup/', {pickup_option : option});	
}

function set_delivery_address(delivery_id){
	var page_url = document.getElementsByTagName('base')[0].href;
	
	$.post(page_url + 'ajax/shirts/set-delivery/', {delivery_address : delivery_id}, function(data){
		if(data == 'true'){
			window.location.href = page_url + 'blagajna/';		
		}				
	});	
}

function encodeToHex(str){
    var r="";
    var e=str.length;
    var c=0;
    var h;
    while(c<e){
        h=str.charCodeAt(c++).toString(16);
        while(h.length<3) h="0"+h;
        r+=h;
    }
    return r;
}