// print and manage the cart status information
var alert_out = false;
var CartStatus = function(p_loggedIn, p_loginEnabled, p_numItems)
{
	this.init();
	// set if passed in
	if (typeof p_loggedIn !='undefined'){ this.setIsLoggedIn(p_loggedIn); }
	if (typeof p_loginEnabled !='undefined'){ this.setLoginEnabled(p_loginEnabled); }
	if (typeof p_numItems !='undefined'){ this.setNumberOfItems(p_numItems); }
};

CartStatus.prototype = {
	init:function() {
		this.itemsInCart = 0;
		this.isLoggedIn = false;
		this.loginEnabled = true;
		
		// init from coookies or create the cookie
		var ckUtil = new CJL_CookieUtil("cartStatus");
		if (!ckUtil.cookieExists()){
			ckUtil.setSubValue("loggedIn", false);
			ckUtil.setSubValue("itemsInCart", 0);
		        ckUtil.setSubValue("loginEnabled", true);
		        ckUtil.setSubValue("alertNumber", -1);
		}
		// init from cookie
		if (ckUtil.cookieExists()){
			this.isLoggedIn = ckUtil.getSubValue("loggedIn");
			this.itemsInCart = parseInt(ckUtil.getSubValue("itemsInCart"));
			this.loginEnabled = ckUtil.getSubValue("loginEnabled");
			this.alertID = parseInt(ckUtil.getSubValue("alertNumber"));
			if (this.itemsInCart.toString() == "NaN"){ this.itemsInCart = 0; }
			if (typeof this.loginEnabled == 'undefined') { ckUtil.setSubValue("loginEnabled", true); }
			try{if( this.alertID >= alert_num ){ 
			}
			else{ 
				newAlert();
			}}catch(e){}
		}
		
		/**
		set the number of items in the cart 
		*/
		this.setNumberOfItems = function(p_num){
			if (isFinite(p_num)){
				ckUtil.setSubValue("itemsInCart", p_num);
				alert( ckUtil.getSubValue("itemsInCart"));
			}
		}

		/**
		set login status
		*/
		this.setIsLoggedIn = function(p_val){
			ckUtil.setSubValue("loggedIn", p_val);
		}

		/**
		set login enabled
		*/
		this.setLoginEnabled = function(p_val){
			ckUtil.setSubValue("loginEnabled", p_val);
		}
		
		this.printStatus = function(p_divId){
			if (document.getElementById){
				var output = "<div id='account_content'>";
			    var items = "item";
			    if (this.itemsInCart != 1)
			    {
			        items = items + "s";
			    }
				//output += "You have " + this.itemsInCart + " " + items + " in your ";
				//output += '<a title="View Cart" href="/cart-view.aspx" style="border-style:None;"><img title="View Cart" src="/images/buttons/cart_btn.jpg" alt="" border="0" /></a><hr />';
				document.getElementById( "cart_btn_placer" ).innerHTML = '<a title="View Cart" id="cart_btn" href="javascript:var NW = window.open(\'cart-view.aspx\',\'\',\'scrollbars=yes,resizable=yes,width=560,height=650\');" >' + this.itemsInCart + '</a>';
				/*if (this.loginEnabled) {
					if (this.isLoggedIn){
						output += '<a title="Edit Profile" href="/account-addedit.aspx" style="border-style:None;"><img title="Edit Profile" src="/images/editprofile.png" alt="" border="0" /></a></p><p><a title="Logout" href="/logout.aspx" style="border-style:None;"><img title="Logout" src="/images/logout.png" alt="" border="0" /></a>';
					} else {
						output += '<form target="login_transfer_frame" action="login.aspx" method="post" onSubmit="showLogin();">';
						output += '<input type="text" value="email" id="Login_2_01_txtUsername" name="Login_2_01_txtUsername"><br>';
						output += '<input type="password" id="Login_2_01_txtPassword" name="Login_2_01_txtPassword" value="email">';
						output += '<input type="submit" value="log in"></form><br><a href="#">register</a><br><a href="#">forgot password?</a>';						
					}
				}*/
				output += '<iframe id="login_transfer_frame" name="login_transfer_frame" src="login.aspx" scrolling="no" frameborder="0" />';
				document.getElementById(p_divId).innerHTML = "<a href='javascript:void(0)' onclick='AccountSlide()' id='account_close'>x</a>" + output + "</div>";
				var self = this;
				this.int = setInterval( function(){ self.doUpdate(); } , 1500 );
			}
		}
		
		this.doUpdate = function(){
			//var ckUtil = new CJL_CookieUtil("cartStatus");
			this.itemsInCart = parseInt(ckUtil.getSubValue("itemsInCart"));
			if (this.itemsInCart.toString() == "NaN"){ this.itemsInCart = 0; }
			if( document.getElementById( "cart_btn" ).innerHTML != this.itemsInCart ){
				document.getElementById( "cart_btn_placer" ).innerHTML = '<a title="View Cart" id="cart_btn" href="javascript:var NW = window.open(\'cart-view.aspx\',\'\',\'scrollbars=yes,resizable=yes,width=560,height=650\');" >' + this.itemsInCart + '</a>';
			}
		}
	}	
};

/**
 * Copyright (C) 2002-2003, CodeHouse.com. All rights reserved.
 * CodeHouse(TM) is a registered trademark.
 *
 * THIS SOURCE CODE MAY BE USED FREELY PROVIDED THAT
 * IT IS NOT MODIFIED OR DISTRIBUTED, AND IT IS USED
 * ON A PUBLICLY ACCESSIBLE INTERNET WEB SITE.
 * 
 * CodeHouse.com JavaScript Library Module: Cookie Utility Class
 *
 * You can obtain this script at http://www.codehouse.com
 */
var CJL_CookieUtil = function(name, duration, path, domain, secure)
{
   this.affix = "";
   
   if( duration )
   {   	  
      var date = new Date();
	  var curTime = new Date().getTime();

	  date.setTime(curTime + (1000 * 60 * duration));
	  this.affix = "; expires=" + date.toGMTString();
   }
   
   if( path )
   {
      this.affix += "; path=" + path;
   }
   
   if( domain )
   {
      this.affix += "; domain=" + domain;
   }

   if( secure )
   {
      this.affix += "; secure=" + secure;
   }
   
      
   function getValue()
   {
      var m = document.cookie.match(new RegExp("(" + name + "=[^;]*)(;|$)"));

      return m ? m[1] : null;   
   }   
   
   function setAccountName( txt )
   {
	   txt = txt.substring( txt.indexOf( "Welcome" ) , txt.length - 1 );
	   txt = txt.substring( 0 , txt.indexOf( "</span" ) );
	   ckUtil.setSubValue("accountName", txt);

      return txt;   
   }
   
   this.cookieExists = function()
   {
      return getValue() ? true : false;
   }
      
   this.expire = function()
   {
      var date = new Date();
	  date.setFullYear(date.getYear() - 1);
	  document.cookie=name + "=noop; expires=" + date.toGMTString(); 
   }
        
   this.setSubValue = function(key, value)
   {
	   
	  //alert( "setting " + key + " to " + value );
      var ck = getValue();
	 // alert( "ck: " + ck );

      if( /[;, ]/.test(value) )
      {
         //Mac IE doesn't support encodeURI
		 value = window.encodeURI ? encodeURI(value) : escape(value);
      }

      
      if( value != null )
      {
		  //alert( "yes, has value " + value );
         var attrPair = "@" + key + value;

         if( ck )
         {
	    	  // alert( "cookie exists" );
	    	   //alert( new RegExp("@" + key) );
             if( new RegExp("@" + key).test(ck) )
	         {
		        document.cookie =
				   ck.replace(new RegExp("@" + key + "[^@;]*"), attrPair) + this.affix;
	         }
	         else
	         {
				 //alert( document.cookie );
		        document.cookie =
				   ck.replace(new RegExp("(" + name + "=[^;]*)(;|$)"), "$1" + attrPair) + this.affix;
	         }
         }
         else
         {
	        document.cookie = name + "=" + attrPair + this.affix;
         }
      }
      else
      {      
		  //alert( "no value " + value );
	     if( new RegExp("@" + key).test(ck) )
	     {
	        document.cookie = ck.replace(new RegExp("@" + key + "[^@;]*"), "") + this.affix;
	     }
      }
   }

      
   this.getSubValue = function(key)
   {
      var ck = getValue();

      if( ck )
      {
         var m = ck.match(new RegExp("@" + key + "([^@;]*)"));

	     if( m )
	     {
	        var value = m[1];

	        if( value )
	        { 
	           //Mac IE doesn't support decodeURI
			   return window.decodeURI ? decodeURI(value) : unescape(value);
	        }
	     }
      }
   }
}

function showLogin(){
	document.getElementById( 'login_frame_container' ).style.visibility = 'visible';
}
function newAlert(){
	/*
	var alert_btn = document.getElementById( "alerts_btn" );
	transX = new XTransformation( alerts_btn , alerts_btn.offsetTop , 1 , 2 , .015 , 80 , 20 , "y");
	transX.onEnd = function(){
		var alert_btn = document.getElementById( "alerts_btn" );
		transX = new XTransformation( alert_btn , alert_btn.offsetTop , 1 , 2 , .015 , 80 , 20 , "y");
		transX.onEnd = function(){
			var alert_btn = document.getElementById( "alerts_btn" );
			transX = new XTransformation( alert_btn , alert_btn.offsetTop , 1 , 2 , .015 , 80 , 20 , "y");
			transX.onEnd = function(){};
		}
	}*/
	if( document.getElementById( "alerts_btn" ) != null ){
		//MM_swapImage("alerts_btn","","images/buttons/alert_new.gif",1);
		showAlerts();
	}
}
function showAlerts(){
	/*var abx = document.getElementById( "alerts_box" );
	abx.style.visibility = ( abx.style.visibility != "visible" ) ? "visible" : "hidden";
	var ckUtil = new CJL_CookieUtil("cartStatus");
	ckUtil.setSubValue("alertNumber", alert_num);*/
	var div = document.getElementById( "alerts_box" );
	if( alert_out ){
		div.style.visibility = "hidden";
		document.getElementById( "wrapper" ).style.top = "0px";
		document.getElementById( "wrapper" ).style.position = "absolute";
		
		//var ckUtil = new CJL_CookieUtil("cartStatus");
		//ckUtil.setSubValue("alertNumber", alert_num);
		
		
		//transX = new XTransformation( div , -div.offsetHeight , 0 , 20 , 1.6 , 80 , 20 , "y");
	} else {
		div.style.visibility = "visible";
		document.getElementById( "wrapper" ).style.top = "22px";
		document.getElementById( "wrapper" ).style.position = "absolute";
		//transX = new XTransformation( div , 23 , 0 , 20 , 1.6 , 80 , 20 , "y" );
	}
	alert_out = !alert_out;
}
function updateLastURL(){
	var ckUtil = new CJL_CookieUtil("cartStatus");
	ckUtil.setSubValue( "lastPage", location.href );
}
