/*
	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();
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
	--------------------------------------------------------------*/
		// Breadcrumbs
		if ( $(".main .breadcrumbs").length ) { $("body").addClass("withBreadcrumbs"); }
		
		// Pop external links in new window (in JS, so we can stay HTML Strict)
		$("a[rel=external]").attr("target","_blank");
		
		// .catalog-callout Tweaks
		$(".catalog-callout").wrapInner("<div class='inner' />");
		$(".catalog-callout .callout-title, .catalog-callout .callout-text").wrapAll("<div class='callout-content' />");
		
		// Newsletter input text replacement
		var $newsletterInput = $("#newsletter-validate-detail #newsletter");
		var inputOrig = $newsletterInput.val();
		$newsletterInput.focus( function(){
			if( $.trim($(this).val()) == inputOrig ){ $(this).val(''); }
		});
		$newsletterInput.blur( function(){
			if( $.trim($(this).val()) == '' ){ $(this).val(inputOrig); }
		});
		
		// Uniform JS Inputs...
		//$("input, textarea, select, button").uniform();
		$("select").uniform();


	/*--------------------------------------------------------------
	 	Sidemenu Tweaks
	--------------------------------------------------------------*/
		if ( $("body.cms-about").length ) {
			$(".sidemenu .sm-about").addClass("selected");
		} else if ( $("body.cms-about-store").length ) {
			$(".sidemenu .sm-store").addClass("selected");
		} else if ( $("body.cms-about-locations").length ) {
			$(".sidemenu .sm-locations").addClass("selected");
		} else if ( $("body.is-blog").length ) {
			$(".sidemenu .sm-events").addClass("selected");
			$("#menuright-links li.about").addClass("active");
		} else if ( $("body.contacts-index-index").length ) {
			$(".sidemenu .sm-contact").addClass("selected");
		} else if ( $("body.cms-customer-service").length ) {
			$(".sidemenu .sm-customer").addClass("selected");
		}


	/*--------------------------------------------------------------
		MainMenu Tweaks
	--------------------------------------------------------------*/
		if ( $("body.cms-about").length ) {
			$("#mainMenuRight li.first").addClass("active");
		} else if ( $("body.contacts-index-index").length ) {
			$("#mainMenuRight li.last").addClass("active");
		} else if ( $("body.cms-wholesale").length ) {
			$("#mainMenuLeft li.last").addClass("active");
		} else if ( $("body.catalog-category-view").length || $("body.catalog-product-view").length || $("body.review-product-list").length ) {
			$("#mainMenuLeft li.first").addClass("active");
		}


	/*--------------------------------------------------------------
		Categories & Products Grids
	--------------------------------------------------------------*/
		if ( $(".products-grid").length ) {
			
			// Add "special" class to sale items
			$(".products-grid .price-box .special-price").parents(".item").addClass("special").each(function(){
				$(this).prepend("<a class='special-ribbon' title='" + $(this).find("a.product-image").attr('title') + "' href='" + $(this).find("a.product-image").attr('href') + "'>Special Pricing</a>");
			});
			
			// Add "new" class to new items
			$(".products-grid .item.new").each(function(){
				$(this).prepend("<a class='new-ribbon' title='" + $(this).find("a.product-image").attr('title') + "' href='" + $(this).find("a.product-image").attr('href') + "'>New Product</a>");
			});
			
			// Toggle price and Rating display
			var topPx = "30px";
			if ( $("body.wholesaler").length ) { topPx = "50px"; }
			$(".products-grid .item").mouseenter(function(){
				$(this).find(".product-extras-inner").stop().animate({ top: '0'}, 400, "easeInOutQuint", function() { });
			}).mouseleave(function(){
				$(this).find(".product-extras-inner").stop().animate({ top: topPx}, 400, "easeInOutQuint", function() { });
			});
		}
		
	/*--------------------------------------------------------------
		Product Details Pages
	--------------------------------------------------------------*/
		
		if ( $(".catalog-product-view, .review-product-list").length ) {
			
			// Add "special" class to sale items
			$("#product_addtocart_form .price-box .special-price").parents(".product-essential").addClass("special").find(".product-col-left").prepend("<div class='special-ribbon'>Special Pricing</div>");
			
			// Add "new" class to new items
			$(".product-essential.new").find(".product-col-left").prepend("<div class='new-ribbon'>New Product</div>");
			
			// Product Details Toggle & Display
			$(".product-details a.product-details-toggle").append("<div class='toggle-button'></div>");
			$(".product-details .toggle-box").wrapInner("<div class='toggle-box-inner' />");
			$(".product-details .toggle-box").toggle(200, "easeInOutQuint", function() {});
			$(".product-details a.product-details-toggle").click(function() {
				$(this).parents(".product-details:eq(0)").toggleClass("active").find(".toggle-box").animate({ height: 'toggle'}, 400, "easeInOutQuint", function() { });
			});
			
			// Product Reviews
			$("#customer-reviews").toggle(200, "easeInOutQuint", function() {});
			$("#reviewsListToggle").click(function() {
				$(this).parents(".product-reviews").toggleClass("active").find("#customer-reviews").animate({ height: 'toggle'}, 400, "easeInOutQuint", function() { });
			});
			
			// Review Form
			$("a#reviewFormLink, .no-rating a").colorbox({
				inline: true,
				title: false,
				opacity: 0.6,
				innerWidth: "600",
				innerHeight: "620",
				overlayClose: true
			});
			
			// Review input text replacement
			$(document).bind('cbox_complete', function(){
				// Review Nickname field
				var $reviewNickname = $("#nickname_field");
				var nicknameInputOrig = $reviewNickname.val();
				if ( $.trim(nicknameInputOrig) == "" || nicknameInputOrig == null) {
					nicknameInputOrig = "My Nickname";
					$reviewNickname.val(nicknameInputOrig);
					$reviewNickname.focus( function(){
						if( $.trim($(this).val()) == nicknameInputOrig ){ $(this).val(''); }
					});
					$reviewNickname.blur( function(){
						if( $.trim($(this).val()) == '' ){ $(this).val(nicknameInputOrig); }
					});
				}
				
				// Review Title field
				var $reviewTitle = $("#summary_field");
				var titleInputOrig = $reviewTitle.val();
				if ( $.trim(titleInputOrig) == "" || titleInputOrig == null) {
					titleInputOrig = "Title of Review";
					$reviewTitle.val(titleInputOrig);
					$reviewTitle.focus( function(){
						if( $.trim($(this).val()) == titleInputOrig ){ $(this).val(''); }
					});
					$reviewTitle.blur( function(){
						if( $.trim($(this).val()) == '' ){ $(this).val(titleInputOrig); }
					});
				}
			});
		}


	/*--------------------------------------------------------------
		HOME PAGE
	--------------------------------------------------------------*/
		if ( $("body.cms-home").length ) {
			
			// HOME Cycle Galleries
			var galleryCounter = 0;
			var leftAdjust, slideCount, majorWidth, widthBetweenDots, galleryNavWidth;
			$(".cycleGalleryWrap").each(function() {
				leftAdjust = 0;
				galleryCounter++;
				
				// Add wappers & controls 
				$(this).prepend("<a href='javascript:void(0);' title='Previous' class='prev '><span id='galleryPrev-" + galleryCounter + "' class='galleryDir galleryPrev'>Prev</span></a><a href='javascript:void(0);' title='Next' class='next'><span id='galleryNext-" + galleryCounter + "' class='galleryDir galleryNext'>Next</span></a>");
				$(this).append("<ul class='galleryNav' id='galleryNav-" + galleryCounter + "'></ul>");
				
				// BANNER Cycle
				if ( $(this).find(".homeBanner.cycleGallery").length ) {
					$(this).find(".cycleGallery").cycle({
						fx:     	'scrollHorz',
						easing: 	'easeInOutQuint',
						speed:  	1000,
						timeout:	4500,
						delay:  	0,
						pause:   	true,
						pauseOnPagerHover: true,
						pagerEvent:	'click', 
						pagerAnchorBuilder: function(idx, slide) { return '<li><a href="#"><span></span></a></li>'; },
						pager:  	'#galleryNav-' + galleryCounter,
						prev:   	'#galleryPrev-' + galleryCounter,
						next:   	'#galleryNext-' + galleryCounter,
						sync:   	true,
						cleartypeNoBg: 	true	// needed for IE
					});
				}
				
				// PRODUCT Cycle
				if ( $(this).find(".products-grid.cycleGallery").length ) {
					$(this).find(".cycleGallery").cycle({
						fx:     	'scrollHorz', 
						easing: 	'easeInOutQuint',
						speed:  	1000,
						timeout:	4500,
						delay:  	0,
						pause:   	true,
						pauseOnPagerHover: true,
						pagerEvent:	'click', 
						pagerAnchorBuilder: function(idx, slide) { return '<li><a href="#"><span></span></a></li>'; },
						pager:  	'#galleryNav-' + galleryCounter,
						prev:   	'#galleryPrev-' + galleryCounter,
						next:   	'#galleryNext-' + galleryCounter,
						sync:   	true,
						cleartypeNoBg: 	true	// needed for IE
					});
				}
				
				// Adjust the nav dots accordingly
				majorWidth = $(this).width();
				slideCount = $(this).find(".galleryNav li").length;
				widthBetweenDots = $(this).find(".galleryNav li:first").css("margin-right");
				widthBetweenDots = parseInt(widthBetweenDots.substring(0,widthBetweenDots.length-2));
				galleryNavWidth = $(this).find(".galleryNav li:eq(0)").width()*slideCount + (slideCount -1)*widthBetweenDots;
				leftAdjust = Math.floor((majorWidth - galleryNavWidth) / 2);
				$(this).find(".galleryNav").css("left",leftAdjust + "px");
				
				// Pause the gallery
				$(this).find(".cycleGallery").cycle('pause');
				
				// Add Classes to the Gallery Nav
				$(this).find(".galleryNav li:first").addClass("first");
				$(this).find(".galleryNav li:last").addClass("last");
			});
		}


	/*--------------------------------------------------------------
		Account Login Page
	--------------------------------------------------------------*/
		if ( $("body.customer-account-login").length ) {
			// Input box text replacement
			// Email field
			var $email = $("#email");
			$email.blur();
			var emailInputOrig = $email.val();
			if ( $.trim(emailInputOrig) == "" || emailInputOrig == null) {
				emailInputOrig = "Email Address";
				$email.val(emailInputOrig);
				$email.focus( function(){
					if( $.trim($(this).val()) == emailInputOrig ){ $(this).val(''); }
				});
				$email.click( function(){
					if( $.trim($(this).val()) == emailInputOrig ){ $(this).val(''); }
				});
				$email.blur( function(){
					if( $.trim($(this).val()) == '' ){ $(this).val(emailInputOrig); }
				});
			}
			// Password field
			var $password = $("#pass");
			var passInputOrig = $password.val();
			var $passwordLabel = $("#passwordLabel");
			if ( passInputOrig != "" ) {
				$passwordLabel.hide();
			}
			$("#pass, #passwordLabel").focus(function(){
				$passwordLabel.hide();
			});
			$("#pass, #passwordLabel").blur(function(){
				if ( $password.val() == "" ) { $passwordLabel.show(); }
			});
		}


	/*--------------------------------------------------------------
		One Page Checkout
	--------------------------------------------------------------*/
		if ( $("body.checkout-onepage-index").length ) {
			// Input box text replacement
			// Email field
			var $email = $("#login-email");
			$email.blur();
			var emailInputOrig = $email.val();
			if ( $.trim(emailInputOrig) == "" || emailInputOrig == null) {
				emailInputOrig = "Email Address";
				$email.val(emailInputOrig);
				$email.focus( function(){
					if( $.trim($(this).val()) == emailInputOrig ){ $(this).val(''); }
				});
				$email.click( function(){
					if( $.trim($(this).val()) == emailInputOrig ){ $(this).val(''); }
				});
				$email.blur( function(){
					if( $.trim($(this).val()) == '' ){ $(this).val(emailInputOrig); }
				});
			}
			// Password field
			var $password = $("#login-password");
			var passInputOrig = $password.val();
			var $passwordLabel = $("#passwordLabel");
			if ( passInputOrig != "" ) {
				$passwordLabel.hide();
			}
			$("#login-password, #passwordLabel").focus(function(){
				$passwordLabel.hide();
			});
			$("#login-password, #passwordLabel").blur(function(){
				if ( $password.val() == "" ) { $passwordLabel.show(); }
			});
		}


	/*--------------------------------------------------------------
		Account Creation Page
	--------------------------------------------------------------*/
		if ( $("body.customer-account-create").length ) {
			// Input box text replacement
			// First Name
			var $firstname = $("#firstname");
			$firstname.blur();
			var fnInputOrig = $firstname.val();
			if ( $.trim(fnInputOrig) == "" || fnInputOrig == null) {
				fnInputOrig = "First Name";
				$firstname.val(fnInputOrig);
				$firstname.focus( function(){
					if( $.trim($(this).val()) == fnInputOrig ){ $(this).val(''); }
				});
				$firstname.click( function(){
					if( $.trim($(this).val()) == fnInputOrig ){ $(this).val(''); }
				});
				$firstname.blur( function(){
					if( $.trim($(this).val()) == '' ){ $(this).val(fnInputOrig); }
				});
			}
			// Last Name
			var $lastname = $("#lastname");
			var lnInputOrig = $lastname.val();
			if ( $.trim(lnInputOrig) == "" || lnInputOrig == null) {
				lnInputOrig = "Last Name";
				$lastname.val(lnInputOrig);
				$lastname.focus( function(){
					if( $.trim($(this).val()) == lnInputOrig ){ $(this).val(''); }
				});
				$lastname.click( function(){
					if( $.trim($(this).val()) == lnInputOrig ){ $(this).val(''); }
				});
				$lastname.blur( function(){
					if( $.trim($(this).val()) == '' ){ $(this).val(lnInputOrig); }
				});
			}
			// Email
			var $email = $("#email_address");
			var emailInputOrig = $email.val();
			if ( $.trim(emailInputOrig) == "" || emailInputOrig == null) {
				emailInputOrig = "Email Address";
				$email.val(emailInputOrig);
				$email.focus( function(){
					if( $.trim($(this).val()) == emailInputOrig ){ $(this).val(''); }
				});
				$email.click( function(){
					if( $.trim($(this).val()) == emailInputOrig ){ $(this).val(''); }
				});
				$email.blur( function(){
					if( $.trim($(this).val()) == '' ){ $(this).val(emailInputOrig); }
				});
			}
			// Password field
			var $password = $("#password");
			var passInputOrig = $password.val();
			var $passwordLabel = $("#passwordLabel");
			if ( passInputOrig != "" ) {
				$passwordLabel.hide();
			}
			$("#password, #passwordLabel").focus(function(){
				$passwordLabel.hide();
			});
			$("#password, #passwordLabel").blur(function(){
				if ( $password.val() == "" ) { $passwordLabel.show(); }
			});
			// Password Conf. field
			var $passwordConf = $("#confirmation");
			var passInputOrig = $passwordConf.val();
			var $passwordConfLabel = $("#passwordConfLabel");
			if ( passInputOrig != "" ) {
				$passwordConfLabel.hide();
			}
			$("#confirmation, #passwordConfLabel").focus(function(){
				$passwordConfLabel.hide();
			});
			$("#confirmation, #passwordConfLabel").blur(function(){
				if ( $passwordConf.val() == "" ) { $passwordConfLabel.show(); }
			});
			
		}


	/*--------------------------------------------------------------
		Contact Us Page
	--------------------------------------------------------------*/
		if ( $("body.contacts-index-index").length ) {
			// Input box text replacement
			// First Name
			var $firstname = $("#name");
			$firstname.blur();
			var fnInputOrig = $firstname.val();
			if ( $.trim(fnInputOrig) == "" || fnInputOrig == null) {
				fnInputOrig = "First Name";
				$firstname.val(fnInputOrig);
				$firstname.focus( function(){
					if( $.trim($(this).val()) == fnInputOrig ){ $(this).val(''); }
				});
				$firstname.click( function(){
					if( $.trim($(this).val()) == fnInputOrig ){ $(this).val(''); }
				});
				$firstname.blur( function(){
					if( $.trim($(this).val()) == '' ){ $(this).val(fnInputOrig); }
				});
			}
			// Email field
			var $email = $("#email");
			var emailInputOrig = $email.val();
			if ( $.trim(emailInputOrig) == "" || emailInputOrig == null) {
				emailInputOrig = "Email Address";
				$email.val(emailInputOrig);
				$email.focus( function(){
					if( $.trim($(this).val()) == emailInputOrig ){ $(this).val(''); }
				});
				$email.click( function(){
					if( $.trim($(this).val()) == emailInputOrig ){ $(this).val(''); }
				});
				$email.blur( function(){
					if( $.trim($(this).val()) == '' ){ $(this).val(emailInputOrig); }
				});
			}
			// Phone field
			var $phone = $("#telephone");
			var phoneInputOrig = $phone.val();
			if ( $.trim(phoneInputOrig) == "" || phoneInputOrig == null) {
				phoneInputOrig = "Phone Number";
				$phone.val(phoneInputOrig);
				$phone.focus( function(){
					if( $.trim($(this).val()) == phoneInputOrig ){ $(this).val(''); }
				});
				$phone.click( function(){
					if( $.trim($(this).val()) == phoneInputOrig ){ $(this).val(''); }
				});
				$phone.blur( function(){
					if( $.trim($(this).val()) == '' ){ $(this).val(phoneInputOrig); }
				});
			}
		}


	/*--------------------------------------------------------------
	 	Locations Page
	--------------------------------------------------------------*/
		if ( $("body.cms-about-locations").length ) {
			// toggle of location regions
			$(".region-header a").click(function() {
				$(this).parents(".location-region:eq(0)").toggleClass("active").find(".company-list:eq(0)").slideToggle("easeInOutQuint",function(){
					//completed
				});
			});
		}


	/*--------------------------------------------------------------
		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.toLowerCase()).addClass(BrowserDetect.browser.toLowerCase() + "-" + BrowserDetect.version.toString().toLowerCase()).addClass("os-" + BrowserDetect.OS.toLowerCase());

});

