$(document).ready(function(){

	$("#PrimaryMenu > ul > li:last-child").addClass("last");
	
	$(".SubCategoryListGrid ul li.RowDivider").remove();
	$(".SubCategoryListGrid ul li:last-child").remove();
	
	// $("#ProductReviews > a > img").replaceWith("write a review");
	// $("#ProductReviews > a").addClass("button");
	
	/*
		This is a short and simple script that will create an Active class for your custom pages menu links(if you have to use stuff like Category pages for your main navigation).
		Keep in mind that this will also work for any other list (like custom Side Vendors, Side Categories, or Side Brands) by just changing the ID Selector. **Credit to Krystin Rice
	*/
	var loc_href = window.location.pathname;
		$('#PrimaryMenu a').each(function () {
		if (loc_href == $(this).attr('href')) {
			$(this).parent("li").addClass('ActivePage');
		}
	});
	
	var url = window.location.pathname;
	url = url.toLowerCase();
	
	var fkImage = $("#Footer .span-8.last");
	
	if (url == '/labels/') {
		fkImage.html("<img src='/template/img/fkLabel.jpg' alt='Wingman Labels' /><p>GET THE GIRL BLOG</p>");
	} else if (url == '/element/') {
		fkImage.html("<img src='/template/img/fkElement.jpg' alt='Wingman Elements' /><p><a href='/blog/'>GET THE GIRL BLOG</a></p>");
	} else if (url == '/lab-series-oils/') {
		fkImage.html("<img src='/template/img/fkLab.jpg' alt='Wingman labels' /><p><a href='/blog/'>GET THE GIRL BLOG</a></p>");
	} else if (url == '/xtreme-x/') {
		fkImage.html("<img src='/template/img/fkX.jpg' alt='Wingman X' /><p><a href='/blog/'>GET THE GIRL BLOG</a></p>");
	} else {
		jwplayer("videoContainer").setup({ 
			flashplayer: "/template/js/player.swf", 
			file: "http://www.youtube.com/watch?v=12CyZxYNc7c", 
			volume: 100,
			height: 186, 
			width: 308,
			'skin': '/template/js/snel.zip',
			image: "/product_images/uploaded_images/wingmanstill.jpg"
			
		}); 	
	}
	
	
	function textReplacement(input) {
		var originalvalue = input.val();
		input.focus(function() {
			if($.trim(input.val()) == originalvalue){ input.val(""); }
		});
		input.blur(function() {
			if($.trim(input.val()) == ""){ input.val(originalvalue); }
		});
	}

	textReplacement($("#search_query"));
	textReplacement($("#nl_first_name"));
	textReplacement($("#nl_email"));
	

	$("#CategoryImage img").wrap("<a href='#skipTo'>");
	
	$("#SideNewProducts li:nth-child(2n)").remove();
	$(".shop #HomeFeaturedProducts").css("margin-left", "-200px");
	$("<h2>Featured Products</h2>").prependTo(".shop #HomeFeaturedProducts");
	
	
	// // loads the cycle or whatever on specific url in this case the home page
	// //GET URL	
	// var url = window.location.pathname;
	// url = url.toLowerCase();
	// //READ URL
	// // uncomment alert to see what the url is in browser if needed
	// //alert(url);
	// if (url == '/' || url == "/index.php") {
	// //RUN CYCLE
		// $("#cycle").cycle({
			// fx: "fade"
		// });
	// };
	
	// // loads only if the cycle selector exists
	// if ($("#cycle").length > 0){	
		// $("#cycle").cycle({
			// fx: "fade"
		// });
	// };


	//	jCarouselLite example -- you will need to add a <div class="ProductListContainer"> around the <ul>
	//	in the HomeFeaturedProducts Panel

	$("#HomeFeaturedProducts .ProductListContainer").jCarouselLite({
        btnNext: "#featured-next",
        btnPrev: "#featured-prev",
	      visible: 5,
	 	  scroll: 5,
		  speed: 800
   });
   
// REWRITE THE OUTPUT OF THE "(X ITEMS)" FIELD TO MATCH THEME
	var cartValue = $("#crt").html();
	var cartDigit;
	
	cartValue = jQuery.trim(cartValue); /Trim whitespace for IE/
	switch (cartValue)
	{
	case "":
	$("#crt").html("0");
	number = 0;
	break;
	case "(1 item)":
	$("#crt").html("1");
	number = 1 ;
	break;
	default:
	//cartDigit = 3;
	
	
	if(cartValue != ""){
	splitup=cartValue.split(" ");
	number = splitup[0].replace("(", "");
	$("#crt").html(number);
	}
	break;
	}


});

// -------------------------------------------------------------------
// gAjax RSS Feeds Displayer- By Dynamic Drive, available at: http://www.dynamicdrive.com
// Created: July 17th, 2007
// Updated June 14th, 10': Fixed issue in IE where labels would sometimes be associated with the incorrect feed items
// -------------------------------------------------------------------

var gfeedfetcher_loading_image="indicator.gif" //Full URL to "loading" image. No need to config after this line!!

google.load("feeds", "1") //Load Google Ajax Feed API (version 1)

function gfeedfetcher(divid, divClass, linktarget){
	this.linktarget=linktarget || "" //link target of RSS entries
	this.feedlabels=[] //array holding lables for each RSS feed
	this.feedurls=[]
	this.feeds=[] //array holding combined RSS feeds' entries from Feed API (result.feed.entries)
	this.feedsfetched=0 //number of feeds fetched
	this.feedlimit=5
	this.showoptions="" //Optional components of RSS entry to show (none by default)
	this.sortstring="date" //sort by "date" by default
	document.write('<div id="'+divid+'" class="'+divClass+'"></div>') //output div to contain RSS entries
	this.feedcontainer=document.getElementById(divid)
	this.itemcontainer="<li>" //default element wrapping around each RSS entry item
}

gfeedfetcher.prototype.addFeed=function(label, url){
	this.feedlabels[this.feedlabels.length]=label
	this.feedurls[this.feedurls.length]=url
}

gfeedfetcher.prototype.filterfeed=function(feedlimit, sortstr){
	this.feedlimit=feedlimit
	if (typeof sortstr!="undefined")
	this.sortstring=sortstr
}

gfeedfetcher.prototype.displayoptions=function(parts){
	this.showoptions=parts //set RSS entry options to show ("date, datetime, time, snippet, label, description")
}

gfeedfetcher.prototype.setentrycontainer=function(containerstr){  //set element that should wrap around each RSS entry item
this.itemcontainer="<"+containerstr.toLowerCase()+">"
}

gfeedfetcher.prototype.init=function(){
	this.feedsfetched=0 //reset number of feeds fetched to 0 (in case init() is called more than once)
	this.feeds=[] //reset feeds[] array to empty (in case init() is called more than once)
	this.feedcontainer.innerHTML='<img src="'+gfeedfetcher_loading_image+'" /> Retrieving RSS feed(s)'
	var displayer=this
	for (var i=0; i<this.feedurls.length; i++){ //loop through the specified RSS feeds' URLs
		var feedpointer=new google.feeds.Feed(this.feedurls[i]) //create new instance of Google Ajax Feed API
		var items_to_show=(this.feedlimit<=this.feedurls.length)? 1 : Math.floor(this.feedlimit/this.feedurls.length) //Calculate # of entries to show for each RSS feed
		if (this.feedlimit%this.feedurls.length>0 && this.feedlimit>this.feedurls.length && i==this.feedurls.length-1) //If this is the last RSS feed, and feedlimit/feedurls.length yields a remainder
			items_to_show+=(this.feedlimit%this.feedurls.length) //Add that remainder to the number of entries to show for last RSS feed
		feedpointer.setNumEntries(items_to_show) //set number of items to display
		feedpointer.load(function(label){
			return function(r){
				displayer._fetch_data_as_array(r, label)
			}
		}(this.feedlabels[i])) //call Feed.load() to retrieve and output RSS feed.
	}
}


gfeedfetcher._formatdate=function(datestr, showoptions){
	var itemdate=new Date(datestr)
	var parseddate=(showoptions.indexOf("datetime")!=-1)? itemdate.toLocaleString() : (showoptions.indexOf("date")!=-1)? itemdate.toLocaleDateString() : (showoptions.indexOf("time")!=-1)? itemdate.toLocaleTimeString() : ""
	return "<span class='datefield'>"+parseddate+"</span>"
}

gfeedfetcher._sortarray=function(arr, sortstr){
	var sortstr=(sortstr=="label")? "ddlabel" : sortstr //change "label" string (if entered) to "ddlabel" instead, for internal use
	if (sortstr=="title" || sortstr=="ddlabel"){ //sort array by "title" or "ddlabel" property of RSS feed entries[]
		arr.sort(function(a,b){
		var fielda=a[sortstr].toLowerCase()
		var fieldb=b[sortstr].toLowerCase()
		return (fielda<fieldb)? -1 : (fielda>fieldb)? 1 : 0
		})
	}
	else{ //else, sort by "publishedDate" property (using error handling, as "publishedDate" may not be a valid date str if an error has occured while getting feed
		try{
			arr.sort(function(a,b){return new Date(b.publishedDate)-new Date(a.publishedDate)})
		}
		catch(err){}
	}
}

gfeedfetcher.prototype._fetch_data_as_array=function(result, ddlabel){	
	var thisfeed=(!result.error)? result.feed.entries : "" //get all feed entries as a JSON array or "" if failed
	if (thisfeed==""){ //if error has occured fetching feed
		alert("Google Feed API Error: "+result.error.message)
	}
	for (var i=0; i<thisfeed.length; i++){ //For each entry within feed
		result.feed.entries[i].ddlabel=ddlabel //extend it with a "ddlabel" property
	}
	this.feeds=this.feeds.concat(thisfeed) //add entry to array holding all feed entries
	this._signaldownloadcomplete() //signal the retrieval of this feed as complete (and move on to next one if defined)
}

gfeedfetcher.prototype._signaldownloadcomplete=function(){
	this.feedsfetched+=1
	if (this.feedsfetched==this.feedurls.length) //if all feeds fetched
		this._displayresult(this.feeds) //display results
}


gfeedfetcher.prototype._displayresult=function(feeds){
	var rssoutput=(this.itemcontainer=="<li>")? "<ul>\n" : ""
	gfeedfetcher._sortarray(feeds, this.sortstring)
	for (var i=0; i<feeds.length; i++){
		var itemtitle="<a href=\"" + feeds[i].link + "\" target=\"" + this.linktarget + "\" class=\"titlefield\">" + feeds[i].title + "</a>"
		var itemlabel=/label/i.test(this.showoptions)? '<span class="labelfield">['+this.feeds[i].ddlabel+']</span>' : " "
		var itemdate=gfeedfetcher._formatdate(feeds[i].publishedDate, this.showoptions)
		var itemdescription=/description/i.test(this.showoptions)? "<br />"+feeds[i].content : /snippet/i.test(this.showoptions)? "<br />"+feeds[i].contentSnippet  : ""
		rssoutput+=this.itemcontainer + itemtitle + " " + itemlabel + " " + itemdate + "\n" + itemdescription + this.itemcontainer.replace("<", "</") + "\n\n"
	}
	rssoutput+=(this.itemcontainer=="<li>")? "</ul>" : ""
	this.feedcontainer.innerHTML=rssoutput
}

// -------------------------------------------------------------------
// gAjax RSS Pausing Scroller- By Dynamic Drive, available at: http://www.dynamicdrive.com
// Created: Aug 2nd, 2007 Updated: n/a
// REQUIRES: gfeedfetcher.js class, available at http://dynamicdrive.com/dynamicindex18/gajaxrssdisplayer.htm
// -------------------------------------------------------------------

var gfeedfetcher_loading_image="indicator.gif" //Specify full URL to "loading" image. Overwrites same var from gfeedfetcher.js


function gfeedpausescroller(divid, divClass, delay, linktarget){
	this.tickerid=divid //ID of outermost scroller div
	this.delay=parseInt(delay) //Default delay between msg change, in miliseconds.
	this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over scroller (and pause it if it is)
	this.hiddendivpointer=1 //index of RSS feed array's entry for hidden div
	this.itemsperpage=1 //Entries to show per page
	document.write('<div id="'+divid+'" class="'+divClass+'" style="position: relative; overflow: hidden">')
	document.write('<div class="innerDiv" style="position: absolute; width: 100%" id="'+divid+'1"><span style="position: absolute">Initializing RSS scroller...</span></div>')
	document.write('<div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" id="'+divid+'2"></div>')
	gfeedfetcher.call(this, divid+"unneeded", divClass+"unneeded", linktarget) //inherit properties from "gfeedfetcher" class, but feed it bogus ID/Class, as we won't be using the DIV generated by "gfeedfetcher"
	document.write('</div>')
	this.itemcontainer="<div>" //default element wrapping around each RSS entry
	this.feedcontainer.style.display="none" //Hide unneeded div generated after inheriting properties from "gfeedfetcher" class
	this.feedcontainer=document.getElementById(divid+'1') //overwrite inherited "feedcontainer" to reference the first DIV within scroller container
	this.tickerdiv=document.getElementById(divid)
	this.visiblediv=document.getElementById(divid+"1")
	this.hiddendiv=document.getElementById(divid+"2")
	this.visibledivtop=parseInt(this.tickerdiv.currentStyle? this.tickerdiv.currentStyle["paddingTop"] : window.getComputedStyle? window.getComputedStyle(this.tickerdiv, "").getPropertyValue("padding-top") : 0) //Determine the "top" boundary of the visible div, factoring in any CSS padding
	this.tickerdivwidth=this.tickerdiv.currentStyle? parseInt(this.tickerdiv.currentStyle["width"]) : this.tickerdiv.offsetWidth //IE has trouble getting offsetWidth while page is loading, so use global CSS value instead
	this.tickerdivheight=this.tickerdiv.currentStyle? parseInt(this.tickerdiv.currentStyle["height"])+50 : 0 //IE has trouble getting offsetWidth while page is loading, so use global CSS value instead. "50" accounts for any possible padding skewing things. Variable used by any other browser.
}

gfeedpausescroller.prototype=new gfeedfetcher //inherit methods from gfeedfetcher class
gfeedpausescroller.prototype.constructor=gfeedpausescroller
gfeedpausescroller.prototype._displayresult=null //Remove inherited method "_displayresult()"


// -------------------------------------------------------------------
// entries_per_page()- Sets the number of RSS entries to display per page (at once)
// -------------------------------------------------------------------

gfeedpausescroller.prototype.entries_per_page=function(num){
	this.itemsperpage=num
}

// -------------------------------------------------------------------
// _signaldownloadcomplete()- Signals to the rest of the script when the fetching of all RSS feeds is complete
// -------------------------------------------------------------------

gfeedpausescroller.prototype._signaldownloadcomplete=function(){ //overwrite inherited method "_signaldownloadcomplete()"
	this.feedsfetched+=1
	if (this.feedsfetched==this.feedurls.length) //if all feeds fetched
		this._populateticker(this.feeds) //Populate the two DIVs within scroller with the fetched data
}

// -------------------------------------------------------------------
// _populateticker()- Pre-populates the two DIVs of the scroller with the 1st and 2nd RSS entries.
// -------------------------------------------------------------------

gfeedpausescroller.prototype._populateticker=function(feeds){
	gfeedfetcher._sortarray(feeds, this.sortstring)
	//set width of inner DIVs to outer DIV's width minus padding (padding assumed to be top padding x 2)
	this.visiblediv.style.width=this.hiddendiv.style.width=parseInt(this.tickerdivwidth)-(this.visibledivtop*2)+"px"
	this.itemsperpage=(this.itemsperpage>=feeds.length)? 1 : this.itemsperpage //Adjust "itemsperpage" if needed (based on total # of avail entries)
	var feedslice1=feeds.slice(0, this.itemsperpage) //Get subsection of feed array based on how many entries to show at once
	this.hiddendivpointer=this.itemsperpage //Set the feed array index of the hidden div accordingly
	var feedslice2=feeds.slice(this.hiddendivpointer, this.hiddendivpointer+this.itemsperpage)
	this.visiblediv.innerHTML=formatrssmessage(feedslice1, this.showoptions, this.itemcontainer, this.linktarget)
	this.hiddendiv.innerHTML=formatrssmessage(feedslice2, this.showoptions, this.itemcontainer, this.linktarget)
	this._initscroller()
}


// -------------------------------------------------------------------
// _initscroller()- Initialize the scroller by setting initial positions of the two dinner DIVs, start up down animation
// -------------------------------------------------------------------

gfeedpausescroller.prototype._initscroller=function(){
	var scrollerinstance=this
	this.getinline(this.visiblediv, this.hiddendiv)
	this.hiddendiv.style.visibility="visible"
	//set width of inner DIVs to outer DIV's width minus padding (padding assumed to be top padding x 2)
	this.visiblediv.style.width=this.hiddendiv.style.width=parseInt(this.tickerdivwidth)-(this.visibledivtop*2)+"px"
	this.tickerdiv.onmouseover=function(){scrollerinstance.mouseoverBol=1}
	this.tickerdiv.onmouseout=function(){scrollerinstance.mouseoverBol=0}
	if (window.attachEvent) //Clean up loose references in IE
		window.attachEvent("onunload", function(){scrollerinstance.tickerdiv.onmouseover=scrollerinstance.tickerdiv.onmouseout=null})
	setTimeout(function(){scrollerinstance._animateup()}, this.delay)
}


// -------------------------------------------------------------------
// formatrssmessage()- Global function that formats a RSS entry(s) to the desired components (title, date, description etc)
// -------------------------------------------------------------------

function formatrssmessage(feedslice, showoptions, itemcontainer, linktarget){
	var rssoutput=(itemcontainer=="<li>")? "<ul>\n" : "" //if "itemcontainer" is set to "<li>", define a "<ul>" tag to wrap around the result
	for (var i=0; i<feedslice.length; i++){ //Loop through the entered slice of a RSS feed (1 or more entries)
		var itemtitle="<a href=\"" + feedslice[i].link + "\" target=\"" + linktarget + "\" class=\"titlefield\">" + feedslice[i].title + "</a>"
		var itemlabel=/label/i.test(showoptions)? '<span class="labelfield">['+feedslice[i].ddlabel+']</span>' : " "
		var itemdate=gfeedfetcher._formatdate(feedslice[i].publishedDate, showoptions)
		var itemdescription=/description/i.test(showoptions)? "<br />"+feedslice[i].content : /snippet/i.test(showoptions)? "<br />"+feedslice[i].contentSnippet  : ""
		rssoutput+=itemcontainer + itemtitle + " " + itemlabel + " " + itemdate + "\n" + itemdescription + itemcontainer.replace("<", "</") + "\n\n"
	}
	rssoutput+=(itemcontainer=="<li>")? "</ul>\n" : ""
	return rssoutput
}


// -------------------------------------------------------------------
// _animateup()- Move the two inner divs of the scroller up and in sync
// -------------------------------------------------------------------

gfeedpausescroller.prototype._animateup=function(){
	var scrollerinstance=this
	if (parseInt(this.hiddendiv.style.top)>(this.visibledivtop+5)){
		this.visiblediv.style.top=parseInt(this.visiblediv.style.top)-5+"px"
		this.hiddendiv.style.top=parseInt(this.hiddendiv.style.top)-5+"px"
		setTimeout(function(){scrollerinstance._animateup()}, 40)
	}
	else{
		this.getinline(this.hiddendiv, this.visiblediv)
		this._swapdivs()
		setTimeout(function(){scrollerinstance._rotatemessage()}, this.delay)
	}
}


// -------------------------------------------------------------------
// _swapdivs()- Swap between which is the visible and which is the hidden div
// -------------------------------------------------------------------

gfeedpausescroller.prototype._swapdivs=function(){
	var tempcontainer=this.visiblediv
	this.visiblediv=this.hiddendiv
	this.hiddendiv=tempcontainer
}

gfeedpausescroller.prototype.getinline=function(div1, div2){
	div1.style.top=this.visibledivtop+"px"
	div2.style.top=Math.max(div1.parentNode.offsetHeight || this.tickerdivheight, div1.offsetHeight)+"px"
}


// -------------------------------------------------------------------
// _rotatemessage()- Populate the hidden div with the next message before it's visible
// -------------------------------------------------------------------

gfeedpausescroller.prototype._rotatemessage=function(){
	var scrollerinstance=this
	if (this.mouseoverBol==1) //if mouse is currently over scoller, do nothing (pause it)
		setTimeout(function(){scrollerinstance._rotatemessage()}, 100)
	else{
		var i=this.hiddendivpointer
		var ceiling=this.feeds.length
		this.hiddendivpointer=(i+this.itemsperpage>ceiling-1)? 0 : i+this.itemsperpage
		var feedslice=this.feeds.slice(this.hiddendivpointer, this.hiddendivpointer+this.itemsperpage)
		this.hiddendiv.innerHTML=formatrssmessage(feedslice, this.showoptions, this.itemcontainer, this.linktarget)
		this._animateup()
	}
}

