var totalProducts;
var productIndex;
var totalSpotted;
var spottedIndex;

$(document).ready(function() {

	updateCartHeader();
	
    var agent = navigator.userAgent.toLowerCase();
	var is_iphone = ((agent.indexOf('iphone') != -1));
	var is_ipad = ((agent.indexOf('ipad') != -1));
	
	if (is_iphone) { 
		
	}	
	
	if (is_ipad) { 

	}
	
	var ie_version = getInternetExplorerVersion();
	
	$(window).resize(lightbox_resize);
	
	$('#content .boxes').each(function (i) {
	    if ((i+1) % 4 == 0) $(this).css("clear","both");
	});
	
	productTotal = $('.gallery-list li').length;
	if(productTotal == 1) {
		$('.gallery-list').css("padding-left","264px")
	}
	if(productTotal == 2) {
		$('.gallery-list').css("padding-left","176px")
	}
	if(productTotal == 3) {
		$('.gallery-list').css("padding-left","88px")
	}


	
	// Setup a default popup link binding
    $('a.popupLink').bind('click', function(e) { popupWindow($(this).attr('href'), $(this).attr('title').replace(/[^A-z]/ig, ''),'width=600, height=800, scrollbars'); e.preventDefault(); });

	$('.productZoom').hover(
		function () {
	    	imageTwo = $(this).children('img').eq(1);
	    	if(imageTwo.length != 0) {
	    		$(this).children('img').eq(0).hide();
	    		$(this).children('img').eq(1).show();
	    	}
	  	}, 
	  	function () {
	  		if(imageTwo.length != 0) {
	    		$(this).children('img').eq(1).hide();
	    		$(this).children('img').eq(0).show();
	    	}
	  	}
	);

	
	$('.productZoom').click(function(e) {
	    e.preventDefault();
	    //totalProducts = $('.gallery-holder ul li').size();
	    //productLinkLi = $(this).parent().parent();
	    //productIndex = $('.gallery-holder ul li').index(productLinkLi);
	    productImageTwo = $(this).parent().parent().children('.productLightbox').children('image-block').children('img').eq(1);
	    $('body').prepend('<div class="popupOverlay">&nbsp;</div><div class="popupContainer"><div class="popupWrapper"><div class="popupWindow"><a href="#" style="color: #002d6a;" id="popupClose"></a><a href="#" style="color: #002d6a; display:none;" id="popupPrev"></a><a href="#" style="color: #002d6a; display:none;" id="popupNext"></a><div class="popupWindowContent" style="background-color:#ffffff;"><div class="popupWindowContentInner"><div class="popupWindowContentOverflow" style="overflow:hidden; text-align:center;"></div></div></div></div></div><div class="overlayClosePane"></div></div>');
	    data = $(this).parent().siblings('.productLightbox').html();
	    $('.popupWindowContentOverflow').html(data);
	    if(imageTwo.length != 0) {
	    	$('#popupNext').show();
	    }
	    showOverlay();
	});
	
	$('.spottedZoom').click(function(e) {
	    e.preventDefault();
	    totalSpotted = $('#content .boxes').size();
	    spottedLinkLi = $(this).parent().parent().parent();
	    spottedIndex = $('#content .boxes').index(spottedLinkLi);
	    $('body').prepend('<div class="popupOverlay">&nbsp;</div><div class="popupContainer"><div class="popupWrapper"><div class="popupWindow"><a href="#" style="color: #002d6a;" id="popupClose"></a><a href="#" style="color: #002d6a;" id="spottedPrev"></a><a href="#" style="color: #002d6a;" id="spottedNext"></a><div class="popupWindowContent" style="background-color:#ffffff;"><div class="popupWindowContentInner"><div class="popupWindowContentOverflow" style="overflow:hidden; text-align:center; padding-top:0px; left:-1px; top:-1px;"></div></div></div></div></div><div class="overlayClosePane"></div></div>');
	    data = $(this).parent().parent().parent().next('.spottedLightbox').html();
	    $('.popupWindowContentOverflow').html(data);
	    if(spottedIndex == 0) {
	    	$('#spottedPrev').hide();
	    }
	    if(spottedIndex == (totalSpotted-1)) {
	    	$('#spottedNext').hide();
	    }
	    showOverlay();
	});
	
});

function lightbox_resize() {
	windowHeight = $(window).height();
	totalHeight = $('#wrapper').height();
	if(windowHeight < totalHeight) {
		totalHeight = totalHeight + 44;
	}
	$('.popupOverlay, .popupContainer').css({height: totalHeight+'px'});
}

function getInternetExplorerVersion() {
	var rv = -1;
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
		rv = parseFloat( RegExp.$1 );
	}
	return rv;
}

function popupWindow(url, name, attributes){
	// Append popup to the URL if it does not already exist
        if(url.indexOf('http://', 0) == -1) {
            var queryStringExists = false;
            if(url.match(/\?/)) {
                    queryStringExists = true;
            }

            if(queryStringExists) {
                    // Only append if we haven't already got the parameter popup defined
                    if(!url.match(/(\?|&)popup=/)) {
                            url = url + '&popup=true';
                    }
            } else {
                    url = url + '?popup=true';
            }
	
            var baseTags = document.getElementsByTagName('base');
            if(baseTags != undefined && baseTags.length > 0) {
                    url = baseTags[0].href + url;
            }
        }
	
	var newWindow = window.open(url, name.replace(/[^A-z]/ig, ''), attributes);
	newWindow.focus();
}


function showOverlay() {
    $('.popupOverlay').css({display: 'block'});
    $('.popupContainer').css({display: 'block'});
    windowHeight = $(window).height();
	totalHeight = $('#wrapper').height();
	if(windowHeight < totalHeight) {
		totalHeight = totalHeight + 44;
	}
    $('.popupOverlay, .popupContainer').css({height: totalHeight+'px'});
    //$(window).scrollTo(0, 800);
    $('.popupOverlay').animate({opacity: 0.5}, 300, "swing", function() {
    	$('.popupContainer').animate({opacity: 1.0}, 300, "swing", function() {
            $('#popupClose, .overlayClosePane').click(function(e) {
                e.preventDefault();
                closeOverlay();
            });
    	});
    });
    $('#popupNext').click(function(e) {
		e.preventDefault();
		productIndex++;
		//newProductInfo = $('.gallery-holder ul li').eq(productIndex).children('.productLightbox').html();
		productImageOne = $('.popupWindowContentOverflow').children('.image-block').children('img').eq(0);
		productImageTwo = $('.popupWindowContentOverflow').children('.image-block').children('img').eq(1);
		productImageOne.hide();
		productImageTwo.show();
		//$('.popupWindowContentOverflow').empty();
		//$('.popupWindowContentOverflow').append(productImageTwo);
    	$('#popupPrev').show();
    	$('#popupNext').hide();
        //if(productIndex == (totalProducts-1)) {
			//$('#popupNext').hide();
		//}
	});
	$('#popupPrev').click(function(e) {
		e.preventDefault();
		productIndex--;
		//newProductInfo = $('.gallery-holder ul li').eq(productIndex).children('.productLightbox').html();
		productImageOne = $('.popupWindowContentOverflow').children('.image-block').children('img').eq(0);
		productImageTwo = $('.popupWindowContentOverflow').children('.image-block').children('img').eq(1);
		productImageOne.show();
		productImageTwo.hide();
		//$('.popupWindowContentOverflow').empty();
		//$('.popupWindowContentOverflow').append(newProductInfo);
		/*if(productIndex == 0) {
	    	$('#popupPrev').hide();
	    }*/
    	$('#popupNext').show();
	    $('#popupPrev').hide();
	});
	$('#spottedNext').click(function(e) {
		e.preventDefault();
		spottedIndex++;
		newSpottedInfo = $('#content .boxes').eq(spottedIndex).next('.spottedLightbox').html();
		$('.popupWindowContentOverflow').empty();
		$('.popupWindowContentOverflow').append(newSpottedInfo);
		if(spottedIndex == 0) {
	    	$('#spottedPrev').hide();
	    } else {
	    	$('#spottedPrev').show();
	    }
	    if(spottedIndex == (totalSpotted-1)) {
	    	$('#spottedNext').hide();
	    }
	});
	$('#spottedPrev').click(function(e) {
		e.preventDefault();
		spottedIndex--;
		newSpottedInfo = $('#content .boxes').eq(spottedIndex).next('.spottedLightbox').html();
		$('.popupWindowContentOverflow').empty();
		$('.popupWindowContentOverflow').append(newSpottedInfo);
		if(spottedIndex == 0) {
	    	$('#spottedPrev').hide();
	    }
	    if(productIndex == (totalSpotted-1)) {
	    	$('#spottedNext').hide();
	    } else {
	    	$('#spottedNext').show();
	    }
	});
}

function closeOverlay() {
    $('.popupContainer').animate({opacity: 0.0}, 100, "swing", function() {
    	$('.popupOverlay').animate({opacity: 0.0}, 100, "swing", function() {
        	$('.popupOverlay').remove();
            $('.popupContainer').remove();
    	});
    });		    
}


function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
    	var c = ca[i];
    	while (c.charAt(0)==' ') c = c.substring(1,c.length);
    	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function updateCartHeader() {
	var totalCartProducts = ""+parseInt(readCookie('totalCartProducts'));
	if(totalCartProducts == '' || totalCartProducts == 'null') {
		totalCartProducts = 0;
	}
	
	$('#cartCount').html(totalCartProducts);
}
