$(document).ready(function(){
	$("li:first-child").addClass("first"); //add a "first" class to all list items
	$("p:last-child").addClass("last"); //add a "first" class to all list items
	
	if ($('div.sub-nav').size() != 0) { $("div.sub-nav ul li a").corner(); };
	
	if ($('.outstand').size() != 0) { 
		$('.outstand').each(function () {
			$(this).corner();							
		});
	};
	
	$('.corner').each(function() { $(this).corner(); });
	
	//check if we need to supersize the background
	if ($('#background-container').size() != 0) { initializeBackground(); }
	
	//check if we need to start the slider
	if ($('#slideshow').size() != 0) { initializeSlideshow(); }
	
	//check if we need to resize videos
	if ($('div.video').size() != 0) { resizeVideos(); }
	
	//check we have products
	if ($('div.product').size() > 0 ) { switchPaypalButtons(); }
	
	//if ($('body').hasClass('popup')) { initPopupDom(); }

	if ($('#newsletter-signup').size() != 0) { initNewsletterSignup(); }

});

function initNewsletterSignup() {
	$('div#newsletter-signup').corner('bottom');
	$('a#bt-newsletter').click(function() {
		$('div#newsletter-signup').toggle();
		$(this).toggleClass('selected');
		return false;
	});
}

function initPopupDom(){
	if(!!window.parent.Shadowbox && $('.overlay-resize').length > 0) {
		shadowBoxResize.init({
	    	padding : 30,
	     	container: 'body.overlay-resize',
	     	titleHeight: 26
	    });
	}
	
}


function switchPaypalButtons() {
	$('input:[src=https://www.paypalobjects.com/WEBSCR-640-20110401-1/en_US/i/btn/btn_buynowCC_LG.gif]').each( function() {
		$(this).attr('src', '/theme/images/bt-buy-through-paypal.jpg');
	});
	
	$('input:[src=https://www.paypalobjects.com/WEBSCR-640-20110401-1/en_US/i/btn/btn_subscribeCC_LG.gif]').each( function() {
		$(this).attr('src', '/theme/images/bt-subscribe-through-paypal.jpg');
	});
	
}

// Resizes the iframes (videos) to its parent width
function resizeVideos() {
	$('div.video iframe').each(function() {
		sizeRatio = $(this).attr('width') / $(this).attr('height');
		newWidth = $(this).parent().width();
		newHeight = Math.round(newWidth / sizeRatio);
		$(this)
			.attr('width', newWidth)
			.attr('height', newHeight);
	});
}

function initializeScrollpane() {
	$('#content').jScrollPane({scrollbarWidth:13, showArrows:true, reinitialiseOnImageLoad: true});
}

function initializeBackground() {
	$.fn.supersized.options = {  
		startwidth: 1024,  
		startheight: 768,
		minsize: .50,
		slideshow: 0	
	};
	$('#background').supersized();
}

function highlightNavigation() {
	if (highlightElement != "") {
		$("li#" + highlightElement).addClass("selected");
		$("li#" + highlightElement + " div.sub-nav").show(); //shows the selected sub-nav
	}
}

function initializeSlideshow() {
    $('#slideshow').nivoSlider({
        effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
        animSpeed:500, //Slide transition speed
        pauseTime:5000,
        startSlide:0, //Set starting Slide (0 index)
        directionNav:false, //Next & Prev
        keyboardNav:false, //Use left & right arrows
        pauseOnHover:false //Stop animation while hovering
    });

}
