$(document).ready(function() {
    
    frontpage_image_resize();
    $(window).resize(frontpage_image_resize);
    
    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();
	
	updateCartHeader();
	
	// 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(); });

	$('.playVideoOne a').click(function(e) {
	    e.preventDefault();
	    youtubeId = $(this).attr('href');
	    $('body').prepend('<div class="popupOverlay">&nbsp;</div><div class="popupContainer"><div class="popupWrapper"><div class="popupWindow"><a href="#" id="popupClose">CLOSE</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();
	    data = '<iframe title="YouTube video player" width="600" height="420" src="http://www.youtube.com/embed/'+youtubeId+'" frameborder="0" allowfullscreen></iframe>';
	    $('.popupWindowContentOverflow').html(data);
	    showOverlay();
	});
	
	$('.playVideoTwo a').click(function(e) {
	    e.preventDefault();
	    youtubeId = $(this).attr('href');
	    $('body').prepend('<div class="popupOverlay">&nbsp;</div><div class="popupContainer"><div class="popupWrapper"><div class="popupWindow"><a href="#" id="popupClose">CLOSE</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();
	    data = '<iframe title="YouTube video player" width="600" height="420" src="http://www.youtube.com/embed/'+youtubeId+'" frameborder="0" allowfullscreen></iframe>';
	    $('.popupWindowContentOverflow').html(data);
	    showOverlay();
	});
});

function frontpage_image_resize() {

	var window_w = $(window).width();

	if (window_w < 960) {
		$('#wrapper').css({"overflow":"visible"});
		$('#content').css({"overflow":"hidden"});
	} else {
		$('#wrapper').css({"overflow":"hidden"});
		$('#content').css({"overflow":"visible"});
	}
} 

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

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();
            });
    	});
    });
}

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

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
	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 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;
	}
	
	if (totalCartProducts > 0) $('.cartProducts span').html('('+totalCartProducts+')');
}
