﻿function closeMiniBasket(){
 	$('li.cart .items').fadeOut(300);
}

function addToBasketWithCheck(itm,la,pid,col,siz,md){
	var quant, cs;
	var now = new Date();
	$('li.cart .items').hide();
	if($('#product_quantity')[0]!=undefined){quant=$('#product_quantity')[0].value}else{quant=1};
  if (md != 2) {  // add to basket an item without color - size
    if($('#colorsize')[0]!=undefined){cs=$('#colorsize')[0].value}else{cs=''};
  }
	
	$.get('default.asp',{pid:pid,la:la,m:'checkitm',itm:itm,col:col,siz:siz,cs:cs,qnt:quant,x:now.toUTCString()},function(data) {
		if(result){
			if(md==1){
				add2basket(itm,la,pid);
			}else if(md==2){
				addTobasket(itm,la,pid);
			}else if(md==3){
				addTobasketFromWish(itm,la,pid,col,siz);
			}
		}else{
      $('#BoxOverlay').css('height', $(document).height());
			csscody.error(err_msg);	
		}
	},'script');
}

function addToBasketWithCheckLbox(itm,la,pid,col,siz,md){
	var quant, cs;
	var now = new Date();
	$('li.cart .items').hide();
	quant=1;
  if (md != 2) {  // add to basket an item without color - size
    if($('#colorsize-lbox')[0]!=undefined){cs=$('#colorsize-lbox')[0].value}else{cs=''};
  }
	
	$.get('default.asp',{pid:pid,la:la,m:'checkitm',itm:itm,col:col,siz:siz,cs:cs,qnt:quant,x:now.toUTCString()},function(data) {
		if(result){
			if(md==1){
				add2basketLbox(itm,la,pid);
			}else if(md==2){
//				addTobasket(itm,la,pid);
			}else if(md==3){
//				addTobasketFromWish(itm,la,pid,col,siz);
			}
		}else{
      $('#BoxOverlay').css('height', $(document).height());
			csscody.error(err_msg);	
		}
	},'script');
}

function showMiniBasket(la){
	var now = new Date();
  if($('li.cart .items').css('display') == 'none') {
    $.ajax({url: '/default.asp?pid=54&la='+la+'&x=' + now.toUTCString(),
      success: function(data) {
        $('li.cart .items').html(data);
        $('li.cart .items').fadeIn(300);
/*        if($('li.cart .items').not(':visible')) {
          var c=$('#actions-menu .basket a').offset();
          $('li.cart .items').css('left', c.left-186).css('top', c.top+31).show();
        }*/
      }
    });
  } else {
    $('li.cart .items').fadeOut(300);  
  };
}

function refreshMiniBasket(la){
	var now = new Date();
  if($('li.cart .items').css('display') == 'block') {
    $('li.cart .items').fadeOut(300);  
  };
  $.ajax({url: '/default.asp?pid=54&la='+la+'&x=' + now.toUTCString(),
    success: function(data) {
      $('li.cart .items').html(data);
    }
  });
}

function add2basket(itm,la,pid){
	var siz,col,quant,cs;
	var now = new Date();
	if($('#size')[0]!=undefined){siz=$('#size')[0].value}else{siz=0};
	if($('#color')[0]!=undefined){col=$('#color')[0].value}else{col=0};
	if($('#colorsize')[0]!=undefined){cs=$('#colorsize')[0].value}else{cs=''};
	if($('#product_quantity')[0]!=undefined){quant=$('#product_quantity')[0].value}else{quant=1};
	$.ajax({url: '/default.asp?pid='+pid+'&m=add&itm='+itm+'&la='+la+'&cs='+cs+'&col='+col+'&siz='+siz+'&qnt='+quant+'&x=' + now.toUTCString() ,
		success: function(data) {
			$('li.cart .items').html(data);
			$('#lgfrm fieldset').hide();
			$('li.cart .items').fadeIn(300);
/*			if($('li.cart .items').not(':visible')) {
				var c=$('#actions-menu .basket a').offset();
				$('li.cart .items').css('left', c.left-186).css('top', c.top+31).show();
			} 
*/      
			var offset = $("li.cart .items").offset();
			if (offset.top - 10 < $(window).scrollTop()) {
				$.scrollTo('li.cart .items',{duration:300});
			}
		}
	});
}

function add2basketLbox(itm,la,pid){
	var siz,col,quant,cs;
	var now = new Date();
	if($('#colorsize-lbox')[0]!=undefined){cs=$('#colorsize-lbox')[0].value}else{cs=''};
	quant=1;
	$.ajax({url: '/default.asp?pid='+pid+'&m=add&itm='+itm+'&la='+la+'&cs='+cs+'&col='+col+'&siz='+siz+'&qnt='+quant+'&x=' + now.toUTCString() ,
		success: function(data) {
			$('li.cart .items').html(data);
			$('#lgfrm fieldset').hide();
			$('li.cart .items').fadeIn(300);
			var offset = $("li.cart .items").offset();
			if (offset.top - 10 < $(window).scrollTop()) {
				$.scrollTo('li.cart .items',{duration:300});
			}
		}
	});
}

function add2Mainbasket(itm,la,pid){
	var siz,col,quant;
	var now = new Date();
	if($('#size')[0]!=undefined){siz=$('#size')[0].value}else{siz=0};
	if($('#color')[0]!=undefined){col=$('#color')[0].value}else{col=0};
	if($('#product_quantity')[0]!=undefined){quant=$('#product_quantity')[0].value}else{quant=1};
	$.ajax({url: '/default.asp?pid='+pid+'&m=add&itm='+itm+'&la='+la+'&col='+col+'&siz='+siz+'&qnt='+quant+'&x=' + now.toUTCString() ,
		success: function(data) {
			$('.basket .main').html(data);
			$('.basket .main').fadeIn(300);
		}
	});
}

function addTobasket(itm,la,pid){
	var now = new Date();
	$.ajax({url: '/default.asp?pid='+pid+'&m=add&itm='+itm+'&la='+la+'&cs=&col=0&siz=0&qnt=1&x=' + now.toUTCString() ,
		success: function(data) {
			$('li.cart .items').html(data);
			$('#lgfrm fieldset').hide();
			$('li.cart .items').fadeIn(300);
			
/*			if($('li.cart .items').not(':visible')) {
				var c=$('#actions-menu .basket a').offset();
				$('li.cart .items').css('left', c.left-186).css('top', c.top+31).show();
			}
*/      
			var offset = $('li.cart .items').offset();
			if (offset.top - 10 < $(window).scrollTop()) {
				$.scrollTo('li.cart .items',{duration:300});
			}
		}
	});
}

function addTobasketFromWish(itm,la,pid,col,siz){
	var now = new Date();
	$.ajax({url: '/default.asp?pid='+pid+'&m=add&wish=1&itm='+itm+'&la='+la+'&col='+col+'&siz='+siz+'&qnt=1&x=' + now.toUTCString() ,
		success: function(data) {
			$('li.cart .items').html(data);
			$('#lgfrm fieldset').hide();
			$('li.cart .items').fadeIn(300);
			
/*			if($('li.cart .items').not(':visible')) {
				var c=$('#actions-menu .basket a').offset();
				$('li.cart .items').css('left', c.left-186).css('top', c.top+31).show();
			} */
			var offset = $("li.cart .items").offset();
			if (offset.top - 10 < $(window).scrollTop()) {
				$.scrollTo('li.cart .items',{duration:300});
			}
		}
	});
}

function addTowishlist(itm,la,pid){
	if($('#size')[0]!=undefined){var siz=$('#size')[0].value}else{var siz=0};
	if($('#color')[0]!=undefined){var col=$('#color')[0].value}else{ var col=0};
	if($('#colorsize')[0]!=undefined){var cs=$('#colorsize')[0].value}else{cs=''};
	var qnt=1;
	var now = new Date();
	var postdata ={m:'addItemToWishList',c:'ItemsWishList',itm:itm,la:la,cs:cs,col:col,siz:siz,qnt:qnt,x:now.toUTCString()};
	$.get('ajax/ajaxProxy.asp',postdata,function(data){
		$('#lgfrm fieldset').hide();
		$('li.cart .items').hide();
		
    $('#BoxOverlay').css('height', $(document).height());
    if (wish_error) {
      csscody.error(wish_msg);
    } else {
      csscody.success(wish_msg);
    }
	},'script');
}

function addTowishlistLbox(itm,la,pid){
	if($('#size')[0]!=undefined){var siz=$('#size')[0].value}else{var siz=0};
	if($('#color')[0]!=undefined){var col=$('#color')[0].value}else{ var col=0};
	if($('#colorsize-lbox')[0]!=undefined){var cs=$('#colorsize-lbox')[0].value}else{cs=''};
	var qnt=1;
	var now = new Date();
	var postdata ={m:'addItemToWishList',c:'ItemsWishList',itm:itm,la:la,cs:cs,col:col,siz:siz,qnt:qnt,x:now.toUTCString()};
	$.get('ajax/ajaxProxy.asp',postdata,function(data){
		$('#lgfrm fieldset').hide();
		$('li.cart .items').hide();
		
    $('#BoxOverlay').css('height', $(document).height());
    if (wish_error) {
      csscody.error(wish_msg);
    } else {
      csscody.success(wish_msg);
    }
	},'script');
}

function addToWithLightbox(itm,la,pid, basket_or_wishlist){
	var type = (basket_or_wishlist === 'basket') ? 1 : 2;
	$.ajax({url: '/ajax/ajaxProxy.asp?pid='+ pid +'&la='+ la +'&c=cstm_GetProduct&m=htmlColorSizes&itm='+ itm +'&tp='+ type +'&ft=' + Math.floor(Math.random()*10000) ,
		success: function(data) {
			$('#add-item-wrapper').html(data);
      $('#lightbox-add-item').center();
      $('#popup-add-item').show();
      $('#lightbox-add-item').show();
      $('#lightbox-add-item').css('z-index','10000');      
		}
	});
}

function removeFromWishlist(wid){
	var now = new Date();
	var postdata ={m:'delItemfromWishList',c:'ItemsWishList',wid:wid,x:now.toUTCString()};
	$.get('ajax/ajaxProxy.asp',postdata,function(data){
		$('#lgfrm fieldset').hide();
		$('li.cart .items').hide();
    $('#BoxOverlay').css('height', $(document).height());    
		wish_error ? csscody.error(wish_msg) : window.location.href=window.location.href;
	},'script');
}

function FindProduct(la,pid){
	var code;
	var now = new Date();
	if($('#quick_code')[0]!=undefined){code=$('#quick_code')[0].value}else{code=0};

	$.ajax({url: '/default.asp?pid='+pid+'&m=find&code='+code+'&la='+la+'&x=' + now.toUTCString() ,
		success: function(data) {
			$('.basket .main').html(data);
			$('#color, #size').uniform();
		}
	});
}

function updateBasketQuantityWithCheck(key,la,pid){
	var quant;
	var now = new Date();
	$('li.cart .items').hide();
	if($('#quantity_'+key)[0]!=undefined){quant=$('#quantity_'+key)[0].value}else{quant=1};
	$.get('default.asp',{pid:pid,m:'check',key:key,la:la,qnt:quant,x:now.toUTCString()},function(data) {
		result ? updateBasketQuantity(key,la,pid) : csscody.error(err_msg);
	},'script');
}

function updateBasketQuantity(key,la,pid){
	var quant;
	var now = new Date();
	$('li.cart .items').hide();
	if($('#quantity_'+key)[0]!=undefined){quant=$('#quantity_'+key)[0].value}else{quant=1};
	$.ajax({url: '/default.asp?pid='+pid+'&m=set&key='+key+'&la='+la+'&qnt='+quant+'&x=' + now.toUTCString() ,
		success: function(data) {
			$('.basket .main').html(data);
		}
	});
}

function remfromMainbasket(key,la,pid){
	var now = new Date();
	$.ajax({url: '/default.asp?pid='+pid+'&m=remove&key='+key+'&la='+la+'&x=' + now.toUTCString() ,
		success: function(data) {
			$('.basket .main').html(data);
		}
	});
}

function gotoCheckoutWithCheck(url,la,pid){
	var quant;
	var now = new Date();
	$('li.cart .items').hide();
	$.get('default.asp',{pid:pid,la:la,m:'checkcart',x:now.toUTCString()},function(data) {
		result ? document.location.href=url : csscody.error(err_msg);
	},'script');
}

function gotoCheckout(la){
	var now = new Date();
	var postdata ={m:'getContent',c:'AltecService',ac:'gotoCheckout',la:la,x:now.toUTCString()};
	$.get('ajax/ajaxProxy.asp',postdata,function(data){
		//alert(data);
		if(as_result){
			window.location.href=loc;
		}else{
			$('#basketErrorMsg').html(as_error);
			$('#basketErrorMsg').fadeIn(200);
		}
	},'script');
}

function remAllfromMainbasket(la,pid){
	var now = new Date();
	$.ajax({url: '/default.asp?pid='+pid+'&m=removeAll&la='+la+'&x=' + now.toUTCString() ,
		success: function(data) {
			$('.basket .main').html(data);
		}
	});
}

function removefrombasket(key,la,pid){
	var now = new Date();
	$.ajax({url: '/default.asp?pid='+pid+'&m=remove&key='+key+'&la='+la+'&x=' + now.toUTCString() ,
		success: function(data) {
			$('li.cart .items').html(data);
/*			if($('li.cart .items').not(':visible')) {
				var c=$('#actions-menu .basket a').offset();
				$('li.cart .items').css('left', c.left-186).css('top', c.top+31).show();
			}
*/      
			if($('.basket .main')[0]!=undefined){
				$.ajax({url: '/default.asp?pid=55&la=1'  ,	success: function(data) {$('.basket .main').html(data);	}})
			}
		}
	});
}

$(document).ready(function() {
	if($('.recommendations .gallery li').length < 4) {
		$('.recommendations .next').addClass('disabled');
	} else {
		$('.recommendations .gallery').jCarouselLite({
			btnNext: '.recommendations .next',
			btnPrev: '.recommendations .previous',
			easing: 'easeInOutQuad',
			speed: 300,
			circular: false,
			visible: 3
		});
	}

	if($('.product_photos .gallery li').length < 4) {
		$('.product_photos .next').addClass('disabled');
	} else {
		$('.product_photos .gallery').jCarouselLite({
			btnNext: '.product_photos .next',
			btnPrev: '.product_photos .previous',
			easing: 'easeInOutQuad',
			speed: 300,
			circular: false,
			visible: 3
		});
	}
	
// 	$('a[rel=lightbox]').lightbox({ 
// 		fileLoadingImage: '/templates/js/loading.gif',
// 		fileBottomNavCloseImage: '/templates/js/close.gif',
// 		fitToScreen: true,
// 		strings: {
// 			prevLinkText:  '&lt;&lt;',
// 			nextLinkText:  '&gt;&gt;',
// 			image: '',
// 			of: ' / '
// 		}
// 	});

	$('#actions-menu .login a.button').click(function(e){
		e.preventDefault();
		if($('#lgfrm fieldset').not(':visible')) {
			var c=$(this).offset();
			$('#lgfrm fieldset').css('left', c.left-183).css('top', c.top+31).show();
		}
	});

	$('#lgfrm fieldset a.close').click(function(e){
		e.preventDefault();
		$('#lgfrm fieldset').hide();
	});
 
	$('#basket-box a.close').click(function(e){
		e.preventDefault();
		$('li.cart .items').hide();
	});

	$('div.product_details select, .product_grid_actions .sort select, .register select, .delivery select, .quick_order select, .search-box select, #ship_method, #pay_method, #department, #contact_category, #adrsCountry').uniform();

	if($('.banner_home').children().length<1) { $('.banner_home').css('display', 'none'); };
	
	if($('input[name=compare_check]').length>0){
		$('input[name=compare_check]').click(function(){
			var compare_max=4;
			if($('input[name=compare_check]:checked').length>compare_max){
				$(this).attr('checked', false);
				$('#lgfrm fieldset').hide();
				$('li.cart .items').hide();
        $('#BoxOverlay').css('height', $(document).height());        
				csscody.alert(compare_msg);
			}
		});
		$('#compareButton').click(function(){
			var compare_min=2;
			if($('input[name=compare_check]:checked').length<compare_min){
				$('#lgfrm fieldset').hide();
				$('li.cart .items').hide();
        $('#BoxOverlay').css('height', $(document).height());        
				csscody.error(compare_error);
			}else{
				$('form[name=compareForm]').submit();
				//document.compareForm.submit();	
			}
		})
	}
});

$(window).resize(function() {
	$('#lgfrm fieldset').hide();
	$('li.cart .items').hide();
});

