/*=============================================================
Type:				Natures Way Foods - Main Javascript file
Build by:			VGroup
Document Author:	Mark Perkins
Author Email:		mark.perkins@vgroup.com
Copyright: 			(c) Vgroup 2008 	
---------------------------------------------------------------
Creation Date:		02-05-2008
Last edited:		N/A
============================================================ */

// Dependencies: jquery version 1.3.1 + cookie plugin ( http://www.stilbuero.de/2006/09/17/cookie-plugin-for-jquery/ )

nwf = function()
{
	return {
		
		init : function()
		{
			$("img").each(function(){
				if ( $(this).attr("href") == "" )
				{
					$(this).hide();
				}
			});
			
			nwf.url.init();
		
			nwf.tagselector.init();
			nwf.styleswitcher.init();
		}
		
	}
	
}();

nwf.tagselector = function()
{

	var initPageTagPicker = function()
	{
		$("#tag_selector input[name=go]").hide();
		
		$("#tag_selector select[name=show_tagged_pages]").change(function(){
			
			if ( $("body.editing").attr("class") == "editing" )
			{
				// editing the site, run through content checker...
				parent.vcms.editpage.confirmExit( nwf.url.base + "search/"+ $(this).find( "option:selected" ).attr("rel") );
			}
			else
			{
				location.href = nwf.url.base + "search/"+ $(this).find( "option:selected" ).attr("rel");					
			}
			
		});
	}
	
	return {
		
		init : function()
		{
			initPageTagPicker();
		}
		
	}
	
}();


nwf.styleswitcher = function()
{
	
	var switchSeason = function( season )
	{
		$("#switch_seasons").attr( "href", nwf.url.base+"styles/"+season+".css" );
		$.cookie( 'nwf_season_memory', '', { expires: -1 }); // delete cookie first, just to be sure
		$.cookie( 'nwf_season_memory', season, { path : "/" } ); // set cookie
	}
	
	var getFlash = function( movieName )
	{
        return ( $.browser.msie ) ? window[movieName] : document.getElementById(movieName);
	}
	
	var changeFlashSeason = function( season )
	{
		getFlash("banner_movie").changeMySeason( season.ucfirst() );
		return false;
	}
	
	return {
		
		init : function()
		{
			if ( $("body.editing").size() )
			{
				$("#season_switcher").hide();
				// $("#season_switcher a").click(function(){
				// 	return false;
				// });
			}
			else
			{
				$("#season_switcher a").click(function(){
					var season = $(this).attr("id");
					season = season.replace( "change_", "" );
					changeFlashSeason( season );
					switchSeason( season );
					return false;
				});				
			}
		}
		
	}
	
}();

nwf.url = function()
{
	
	var segments = new Array();
	
    return {
	
		base : "",
		
		string : "",
		
		hash : "",
		
		segment : function( segmentNumber )
		{
			if ( segments[segmentNumber] == "" || segments[segmentNumber] == undefined ) return false;
			else return segments[segmentNumber]; 
		},
		
		init : function()
		{
			var baseurl_parts = location.href.split("/");
			
			this.hash = location.hash;
			
			this.base = baseurl_parts[0]+"//"+baseurl_parts[2]+"/";
			
			for ( var i = 3; i < baseurl_parts.length; i++ )
			{
				segments[i-3] = baseurl_parts[i];
			}
			
			if ( segments[segments.length-1][0] == "#" ) delete segments[segments.length-1]; 
	    }

    }

}();

$(function(){ nwf.init() });

String.prototype.ucfirst = function()
{
   var x = this.split(/\s+/g);
   for(var i = 0; i < x.length; i++)
   {
      var parts = x[i].match(/(\w)(\w*)/);
      x[i] = parts[1].toUpperCase() + parts[2].toLowerCase();
   }
   return x.join(' ');
};

function flashGoTo( url ) {

	if ( parent.frames['mainframe'] && parent.frames['mainframe'].name == "mainframe" ) {
		url = url.replace( "..", "" );
		url = url.replace( "..", "" );
		url = url.replace( "..", "" );
		return parent.vcms.editpage.confirmExit( window.location.protocol +"//"+ window.location.host + url );
	} else {
		url = url.replace( "..", "" );
		url = url.replace( "..", "" );
		url = url.replace( "..", "" );
		location.href= window.location.protocol +"//"+ window.location.host + url;

	}
}
