/*
	Copyright: 	Hybrid Forge 2011. All Rights Reserved.
	Author: 	Rod Miles (www.HybridForge.com)
	Date:		Sept 2011
	
	Info:		Base Customizations & Styling of all themes & sites
				on DiscoverWorship.com
=============================================================================*/
 
 
/*	jQuery Customizations
=============================================================================*/
jQuery.noConflict();	// required to use jQuery when Prototype is active
jQuery(document).ready(function($) {

	/*--------------------------------------------------------------
		Misc. EASING Functions
	--------------------------------------------------------------*/
		$.easing.custom = function (x, t, b, c, d) { 
			var s = 1.70158;  
			if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b; 
			return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b; 
		}	
		$.easing.easeInOutQuint = function (x, t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
			return c/2*((t-=2)*t*t*t*t + 2) + b;
		}


	/*--------------------------------------------------------------
		MISC. Tweaks
	--------------------------------------------------------------*/
		// Product reviews inside Product Tabs when it's empty
		if ( $.trim($("#product_tabs_product_review_contents #customer-reviews").html()) == "") {
			$("#product_tabs_product_review_contents #customer-reviews").html( "No reviews yet. " + $("#product_addtocart_form .no-rating").html() );
		}
		
		// Hilite product cell if on sale and in grid-view
		$(".products-grid .special-price").parents("li.item").addClass("special");
		
		// Reviews page
		if ( $("body").hasClass("review-product-list") || $("body").hasClass("catalog-product-view") ) {
			if ( $.trim( $(".product-collateral.cms-content .cms-content-inner").text()) == "") { $(".product-collateral.cms-content").remove(); }
		}
		
		// Pop external links in new window (in JS, so we can stay HTML Strict)
		$("a[rel=external]").attr("target","_blank");
		
		// Add inner div to main value prop block
		$(".sidebar .block-valueprop").wrapInner("<div class='block-inner'></div>");
		
		// Logged in HF-Stats drop /* this is not working as expected because the body class <action> had to be removed from local.xml
		//$(".loggedin #header-stats-inner").stop().animate({ top: "0px" }, 1000, "easeInOutQuint", function(){});


	/*--------------------------------------------------------------
		MainMenu Tweaks
	--------------------------------------------------------------*/
		$("#nav li").filter(".level0").find("a:first").addClass("first-level");
		if ( $("#nav li#mm02 li.active").length ) { $("#nav li#mm02 a.mmTop").addClass("active"); }


	/*--------------------------------------------------------------
		Products Lists
	--------------------------------------------------------------*/
		// Musicals categories: showing thumbnails on musicals categories
		if ( $("body[class*='musicals']").length ) { $("body").addClass("show-thumbs"); }
		
		// Product lists
		if ( $("#products-list").length ) {
			$("#products-list li.item").each(function(){
				// Move price and credits into same div
				$(this).find(".price-box").prependTo( $(this).find(".priceCreditBox") );
				$(this).find(".credit-cost .credit").prependTo( $(this).find(".priceCreditBox") );
				if ( $(this).find(".price-box").length ) {
					$(this).find(".credit").prepend("<span class='or'>or</span>");
				}
				
				// Move message into details box
				$(this).find(".messageBox").appendTo( $(this).find(".messageBoxWrap") );
			});
		}
		
	/*--------------------------------------------------------------
		Product Details Pages
	--------------------------------------------------------------*/
		if ( $(".catalog-product-view, .review-product-list").length ) {
			
			// Premium Content Badge
			if ( $("#premium-badge").length ) {
				var extraPadding = $("#premium-badge").height() + 10;
				$("#prodOverviewLeft .inner").css("padding-top", extraPadding + "px");
				$("#prodOverviewRight .inner").css("padding-bottom", extraPadding + "px");
			}
			
			// Move Credit Count & price-box inside #priceCreditBox
			$(".product-action-items").prepend("<div id='priceCreditBox'></div>");
			$("#credit-cost .credit, .price-box:eq(0)").appendTo( $("#priceCreditBox") );
			if ( $("#prodOverviewLeft .price-box").length ) {
				$("#priceCreditBox .credit").prepend("<span class='or'>or</span>");
			}
			
			// Creating MP3 iFrame on the fly
			// This technique is REQUIRED to avoid a "double back button" click in order to go back 1 page previous
			$("#circlePlayerWrap").prepend("<iframe src='' id='circlePlayer' width='204' height='208' frameBorder='0' style='display: none; border: none; outline: none;' />");
			$("#circlePlayer").attr("src", "http://www.discoverworship.com/jplayer/index.php?sample=");
			
			// Associate the MP3 Sample
			$("#circlePlayer").attr("src", $("#circlePlayer").attr("src") + $(".product-samples dd.sample-mp3:first a").attr("href") ).fadeIn();
			
			// Ensuring height correctness for details box
			$(".catalog-product-view #prodOverview .inner").height( $(".product-essential .product-essential-inner #prodDetails").height() - 40 );
			$(".review-product-list #prodOverviewRight .inner").height( $(".review-product-list #prodOverviewLeft .inner").height() );
			
			// Move PDF samples to sample PDF Wrapper & center the buttons
			$(".product-samples dd.sample-pdf a").each(function(){
				$(this).addClass("button btn-learn-more").attr("target","_blank").wrapInner("<span><span></span></span>").appendTo("#sample-pdf-wrapper");
			});
			if ( $("#sample-pdf-wrapper .button").length ) {
				$("#sample-pdf-wrapper").fadeIn();
				$("#sample-pdf-wrapper .button").each(function(){
					$(this).find("span:first").css("margin-left", Math.ceil( ($("#sample-pdf-wrapper").width() - $(this).find("span:first").width() - 10)/2) );
				});	
			}
			
			// Align Action button to center
			var $actionButton = $("#prodOverviewLeft .inner .product-action-items .button span:first");
			$actionButton.css("margin-left", Math.ceil( ($("#prodOverviewLeft .product-action-items").width() - $actionButton.width() - 10 )/2) ); // 10 comes from padding on element
			
			// Animation for the details panel
			var animSpeed = 1500;
			var animationEasing = "easeInOutQuint";
			var animOptions = { width: '700px' }
			$(".product-essential .product-essential-inner").stop().animate(animOptions, animSpeed, animationEasing, function(){});
			
			// Fade In of Player
			var fadeInTimeout = setTimeout("displaySongPlayer()", 1200);
		}


	/*--------------------------------------------------------------
		HOME PAGE
	--------------------------------------------------------------*/
		if ( $("body.cms-home").length || $("body.customer-account-index").length ) { 
			
			// Video Widget
			// -------------------------------------------------
				/* add controls */
				if ( $("#videosBox .videoItem").length ) {
					$("#videosBox").prepend("<div id='videoNav'></div><a href='#' title='Previous'><span id='videoPrev'>Prev</span></a><a href='#' title='Next'><span id='videoNext'>Next</span></a>")
				}
			
				/* adding launch button, video link, etc. */
				$("#videosBox .videoItem").each(function() {
					videoTitle = $(this).find(".videoDescription").text().replace("'","");
					videoLink = "http://www.youtube.com/embed/" + $.trim( $(this).find(".videoEmbed").text() ) + "?rel=0&amp;wmode=transparent&amp;autoplay=1";
					$(this).find(".videoImg").wrapInner("<a rel='dw-videos' class='videoLink' href='" + videoLink + "' title='" + videoTitle + "'></a>");
					$(this).find(".videoImg a").prepend("<div class='videoLaunch'></div>");
				});
			
				/* adding colorbox to the mix */
				$("#videosBox .videoItem a[rel='dw-videos']").colorbox({ iframe:true, innerWidth:800, innerHeight:500, current:"Video {current} of {total}" });
			
				/* restructure content into rows for cycling thru */
				var totalVideoCount = $("#videosBox .videoItem").length;
				var totalRowCount = Math.ceil(totalVideoCount/3);
				var videosInRowCounter = 0;
				var rowCounter = totalRowCount;
			
				/* append the correct # of videorows */
				for (i=totalRowCount;i>=1;i--) {
					$("#videosBox #videosBoxInner").append("<div id='videoRow-" + i + "' class='videoRow'></div>");
					if (i != 1) { $("#videosBox #videosBoxInner #videoRow-" + i).css("display","none"); }
				}
			
				/* add videos to individual videorows */
				for (i=totalVideoCount;i>=1;i--) {
					videoItemCount = i;
					$("#videosBox .videoItem").eq(i-1).addClass("video-" + videoItemCount).appendTo("#videoRow-" + rowCounter);
					if (videosInRowCounter == 2) {
						rowCounter--;
						videosInRowCounter = 0;
					} else { videosInRowCounter++; }
				}
				
				/* remove margin-right from 3rd video in list */
				$(".videoRow").each(function() {
					$(this).find(".videoItem:eq(2)").css("margin-right","0");
				});
			
				/* initiate the cycle */
				$('#videosBoxInner').cycle({
					fx:     	'scrollHorz', 
					easing: 	'easeInOutQuint',
					speed:  	1000,
					timeout:	4500,
					delay:  	0,
					pause:   	true,
					pauseOnPagerHover: true,
					pagerEvent:	'click', 
					pager:  	'#videoNav',
					prev:   	'#videoPrev',
					next:   	'#videoNext',
					sync:   	true,
					cleartypeNoBg: 	true	// needed for IE
				});
				$('#videosBoxInner').cycle('pause');
			
			
			// Lists Widget
			// -------------------------------------------------
				/* Adding necessary classes */
				$("#homeListsRow .songListWrap:first").addClass("songListWrap-1");
				$("#homeListsRow .songListWrap:eq(1)").addClass("songListWrap-2");
				$("#homeListsRow .songListWrap:eq(2)").addClass("songListWrap-3");
				$("#homeListsRow .songListSongs li:odd").addClass("odd");
				$("#homeListsRow .songListSongs li:even").addClass("even");
				$(".songListSongs").each(function(){
					$(this).find("li:first").addClass("first");
					$(this).find("li:last").addClass("last");
				});
				
				/* Creating a Wrapper */
				$(".songListWrap").appendTo("#songListWrapper");
				
				/* animation setup */
				var animSpeed = 500;
				var animCounter = 0;
				var animationEasing = "easeInOutQuint";
				var animNonActiveOptions = { width: '216px', 'top': '0' }
				var animNonActiveExtras = { height: '0px' }
				var animNonActiveDetails = { width: '170px' }
				var animNonActiveFontsize = { 'font-size': '16px' }
				var animActiveOptions = { width: '270px', 'top': '-35px' }
				var animActiveExtras = { height: '15px' }
				var animActiveDetails = { width: '220px' }
				var animActiveFontsize = { 'font-size': '14px' }
				
				/* initial animation */
				$(".songListWrap .songTitle").css("font-size","16px");
				$(".songListWrap:first").addClass("active").stop().animate(animActiveOptions, animSpeed, animationEasing, function(){});
				$(".songListWrap:first").find(".songDetails").stop().animate(animActiveDetails, animSpeed, animationEasing, function(){});
				$(".songListWrap:first").find(".songExtras").stop().animate(animActiveExtras, animSpeed, animationEasing, function(){});
				$(".songListWrap:first").find(".songTitle").stop().animate(animActiveFontsize, animSpeed, animationEasing, function(){});
			
				/* interactions */
				$(".songListWrap").mouseenter(function(){
					animCounter++;
					$(".songListWrap").each(function(){
						$(this).find(".songDetails").stop().animate(animNonActiveDetails, animSpeed, animationEasing, function(){});
						if ($(this).find(".songExtras").height() != 15 || $(this).hasClass("active") ) {
							$(this).find(".songExtras").stop().animate(animNonActiveExtras, animSpeed, animationEasing, function(){});
						}
						$(this).removeClass("active").stop().animate(animNonActiveOptions, animSpeed, animationEasing, function(){});
					})
					$(this).addClass("active");
					$(this).stop().animate(animActiveOptions, animSpeed, animationEasing, function(){});
					$(this).find(".songDetails").stop().animate(animActiveDetails, animSpeed, animationEasing, function(){});
					$(this).find(".songExtras").stop().animate(animActiveExtras, animSpeed, animationEasing, function(){});
					$(this).find(".songTitle").stop().animate(animActiveFontsize, animSpeed, animationEasing, function(){});
				}).mouseleave(function(){
					$(this).removeClass("active").stop().animate(animNonActiveOptions, animSpeed, animationEasing, function(){});
					$(this).find(".songDetails").animate(animNonActiveDetails, animSpeed, animationEasing, function(){});
					$(this).find(".songExtras").animate(animNonActiveExtras, animSpeed, animationEasing, function(){});
					$(this).find(".songTitle").animate(animNonActiveFontsize, animSpeed, animationEasing, function(){});
				});
				
				/* back to initial */
				$("#songListWrapper").mouseleave(function(){
					$(".songListWrap:first").addClass("active").stop().animate(animActiveOptions, animSpeed, animationEasing, function(){});
					$(".songListWrap:first").find(".songDetails").stop().animate(animActiveDetails, animSpeed, animationEasing, function(){});
					$(".songListWrap:first").find(".songExtras").stop().animate(animActiveExtras, animSpeed, animationEasing, function(){});
					$(".songListWrap:first").find(".songTitle").stop().animate(animActiveFontsize, animSpeed, animationEasing, function(){});
				});
		}


	/*--------------------------------------------------------------
		Browser Detection
	--------------------------------------------------------------*/
		var BrowserDetect = {
			init: function () {
				this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
				this.version = this.searchVersion(navigator.userAgent)
					|| this.searchVersion(navigator.appVersion)
					|| "an unknown version";
				this.OS = this.searchString(this.dataOS) || "an unknown OS";
			},
			searchString: function (data) {
				for (var i=0;i<data.length;i++)	{
					var dataString = data[i].string;
					var dataProp = data[i].prop;
					this.versionSearchString = data[i].versionSearch || data[i].identity;
					if (dataString) {
						if (dataString.indexOf(data[i].subString) != -1)
							return data[i].identity;
					}
					else if (dataProp)
						return data[i].identity;
				}
			},
			searchVersion: function (dataString) {
				var index = dataString.indexOf(this.versionSearchString);
				if (index == -1) return;
				return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
			},
			dataBrowser: [
				{
					string: navigator.userAgent,
					subString: "Chrome",
					identity: "Chrome"
				},
				{ 	string: navigator.userAgent,
					subString: "OmniWeb",
					versionSearch: "OmniWeb/",
					identity: "OmniWeb"
				},
				{
					string: navigator.vendor,
					subString: "Apple",
					identity: "Safari",
					versionSearch: "Version"
				},
				{
					prop: window.opera,
					identity: "Opera"
				},
				{
					string: navigator.vendor,
					subString: "iCab",
					identity: "iCab"
				},
				{
					string: navigator.vendor,
					subString: "KDE",
					identity: "Konqueror"
				},
				{
					string: navigator.userAgent,
					subString: "Firefox",
					identity: "Firefox"
				},
				{
					string: navigator.vendor,
					subString: "Camino",
					identity: "Camino"
				},
				{	// for newer Netscapes (6+)
					string: navigator.userAgent,
					subString: "Netscape",
					identity: "Netscape"
				},
				{
					string: navigator.userAgent,
					subString: "MSIE",
					identity: "internet-explorer",
					versionSearch: "MSIE"
				},
				{
					string: navigator.userAgent,
					subString: "Gecko",
					identity: "Mozilla",
					versionSearch: "rv"
				},
				{ 	// for older Netscapes (4-)
					string: navigator.userAgent,
					subString: "Mozilla",
					identity: "Netscape",
					versionSearch: "Mozilla"
				}
			],
			dataOS : [
				{
					string: navigator.platform,
					subString: "Win",
					identity: "Windows"
				},
				{
					string: navigator.platform,
					subString: "Mac",
					identity: "Mac"
				},
				{
					   string: navigator.userAgent,
					   subString: "iPhone",
					   identity: "iPhone/iPod"
				},
				{
					string: navigator.platform,
					subString: "Linux",
					identity: "Linux"
				}
			]			
		};
		BrowserDetect.init();
		
		//add browser and os to body classes
		$("body").addClass(BrowserDetect.browser).addClass(BrowserDetect.browser + "-" + BrowserDetect.version).addClass("os-" + BrowserDetect.OS);

});

function displaySongPlayer() {
	jQuery("#circlePlayer").fadeIn();
}

