$(document).ready(function () {
	$('#add_to_cart,#add_to_cart_bottom').removeAttr('onclick');

	$('#add_to_cart, #add_to_cart_bottom').click(function () {
		var sizeSelect = $('#product select');
		var primaryLogo = $('#personalLogoContainer');
		if(sizeSelect.length > 0){
			var selectValue1 = $('#select_1').val();
		}else{
			var selectValue1 = '';
		}
				
		if(primaryLogo.length > 0){
			var plogo = $('#personalLogoContainer input[name=primaryLogoCheck]:checked').val();
			if(!plogo){
				plogo = '';
			}
		}else{
			var plogo = '';
		}
		var moveStatus = false;
		
		if(sizeSelect.length == 0){
			moveStatus = true;
		}else if(sizeSelect.length > 0){
			if(selectValue1 != ''){
				moveStatus = true;
			}else{
				moveStatus = false;
			}
		}
		var moveStatus1 = false;
		if(primaryLogo.length == 0){
			moveStatus1 = true;
		}else if(primaryLogo.length > 0){
			if(plogo != ''){
				moveStatus1 = true;
			}else{
				moveStatus1 = false;
			}
		}
		
		if( ( moveStatus == true ) && ( moveStatus1 == true ) ){
			$.ajax({
				type: 'post',
				url: 'index.php?route=module/cart/callback',
				dataType: 'html',
				data: $('#product').serializeArray(),
				success: function (html) {
					$('#module_cart .middle').html(html);
				},	
				complete: function () {
					var image = $('#image').offset();
					var cart  = $('#module_cart').offset();
		
					$('#image').before('<img src="' + $('#image').attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');
		
					params = {
						top : cart.top + 'px',
						left : cart.left + 'px',
						opacity : 0.0,
						width : $('#module_cart').width(),  
						height : $('#module_cart').height()
					};		
		
					$('#temp').animate(params, 'slow', false, function () {
						$('#temp').remove();
					});		
				}			
			});		
			//$('#product').submit();
		}else{
			if(sizeSelect.length > 0 && selectValue1 == ''){
				alert('Please select a size');
			}else if(primaryLogo.length > 0 && plogo == ''){
				alert('Please select a primary logo');
			}
		}
	});	
	var $floatingbox = $('#module_cart');
	if($('#content').length > 0){
		var $floatingbox1 = $('#content div.middle');
		var $middlebox1 = $('#module_cart div.middle');
		var widthBox = $floatingbox.width()+'px';
		var middleBox = (parseInt($floatingbox.width()) - 40) + 'px';
		var heightBox = (parseInt($floatingbox.height())) + 'px';
		var bodyY = parseInt($('#content').offset().top) - 20;
		var heightBox1 = (parseInt($middlebox1.height()));
		var originalX = $floatingbox.css('margin-left');
		var contentHeight = $floatingbox1.height();
		
		$(window).scroll(function () { 
			var scrollY = $(window).scrollTop();
			var isfixed = $floatingbox.css('position') == 'fixed';
			//alert($floatingbox.css('position'));
			
			if($floatingbox.length > 0){
				 var middleHtml = $floatingbox.html();
				$floatingbox.html(middleHtml);
				scrollY = parseInt(scrollY);
				bodyY = parseInt(bodyY);
				if(scrollY > 600 && heightBox1 > 600){
					$floatingbox.stop().css({
						position: 'fixed',
						width: widthBox,
						bottom: 0,
						top: -200
						//left: '50%',
						//top: 20,
						//marginLeft: -500
					});
				}else if ( scrollY > bodyY && !isfixed ) {
				
				$('#module_cart .menu').width(middleBox);
					$floatingbox.stop().css({
						position: 'fixed',
						width: widthBox,
						bottom: 20,
						top:5
						//left: '50%',
						//top: 20,
						//marginLeft: -500
					});
				}else if (scrollY < bodyY && isfixed ) {
					
					$('#module_cart .menu').width(middleBox);
					$floatingbox.css({
						position: 'relative',
						width: widthBox,
						bottom: 0,
						top: 0
						//left: 0,
						//top: 0,
						//marginLeft: originalX
					});
				}
		
			}
		});	
	}
	
	/*$().ready(function() {
		var $scrollingDiv = $("#module_cart");
 
		$(window).scroll(function(){			
			$scrollingDiv
				.stop()
				.animate({"marginTop": ($(window).scrollTop() + 10) + "px"}, "slow" );			
		});
	});*/
});
