/******************************
*Useful General Functons for Ecwid
*Makes adding mods really easy
*
*craig.cooper@ciedesign.co.uk
*http://www.ciedesign.co.uk
*http://www.ecwid.com
*
*****************************
Copyright (c) 2010 Craig Cooper,  All rights reserved.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
********************************************************************************
All you need to do is delcare a function then:

addMod(modName, pageName);

where modName is a function and pageName is an ecwid page type. If a function returns false it'll be run until it returns true

functions get passed these:
 	      page.mode
              page.category
	      page.product

PageName options are

category
product
orderConfirmation
cart
accountSettings
orders
addressBook
checkoutSA
checkoutPD
checkoutPO
orderConfirmation
allPages


****************************************************************************
/*Test Mod*/
/*Just pops an alert on category pages containing the category number
 
tester = function (page) {
      alert (page.category);
      return true;
}

addMod(tester, "category");

************************************************************************/

$j = jQuery.noConflict();

if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function (obj, fromIndex) {
    if (fromIndex == null) {
        fromIndex = 0;
    } else if (fromIndex < 0) {
        fromIndex = Math.max(0, this.length + fromIndex);
    }
    for (var i = fromIndex, j = this.length; i < j; i++) {
        if (this[i] === obj)
            return i;
    }
    return -1;
  };
}


pageFunctions = new Object();
pageFunctions.category = new Array();
pageFunctions.product = new Array();
pageFunctions.orderConfirmation = new Array()
pageFunctions.accountSettings = new Array();
pageFunctions.orders = new Array();
pageFunctions.addressBook = new Array();
pageFunctions.checkoutSA = new Array();
pageFunctions.checkoutPD = new Array();
pageFunctions.checkoutPO = new Array();
pageFunctions.cart = new Array();
pageFunctions.orderConfirmation = new Array();

$j(document).ready(function(){                    //Run when page is first loaded, not just on change
  	$j( window.location ).trigger(
				"change",
				{
					currentHash: window.location.hash.slice(1)
				}
			);
});

$j( window.location ).bind(
      "change",
      function(objEvent, objData){
	      hash = objData.currentHash.slice(6);  //trim the ecwid and the COLON leading.

              var page = {};
	      page.mode = getPageType(hash);
              page.category = getCategory(hash);
	      page.product = getProduct(hash);

	      ecwidMods(page);
      	}
);

function ecwidMods(page) {
 	   functionComplete = false;  //They all return true if successful. false if not.
           if (window.t1 !== undefined) { clearInterval(t1); }

	   t1 = setInterval (function() {

	      var pageName = pageFunctions[page.mode];
	      if (pageName) {
	      	            completeFlags = new Array(pageName.length); //Create array of same length for flags and set to 0
	      		    for ( var j in completeFlags ) { completeFlags[j] = 0; }
	      } else {  functionComplete = true; } //Nothing to do!

	      for ( var i in pageName )
		  {
		    if(completeFlags[i] !== true) {
		     completeFlags[i] = pageName[i](page); //Set Flag
		    }
              }
		  //Check if all flags are set
		  if ( completeFlags.indexOf(false) == -1) {
                      functionComplete = true;
		  }
	      if (functionComplete) { clearInterval(t1); }
          }, 100);
}

function addMod(modName, pageName)
{
  if ( pageName == "allPages" ) {
    for (var k in pageFunctions)
      {
      pageFunctions[k].push(modName);
      }
  } else {
    if(pageFunctions[pageName].indexOf(modName) == -1) {
  	pageFunctions[pageName].push(modName);
  	}
  }
}

function getPageType(hash) {
   var hashObj = $j.deparam.fragment(hash);
   if (hashObj.mode) { return hashObj.mode; } else { return false; }
}

function getCategory(hash) {
   var hashObj = $j.deparam.fragment(hash);
   if (hashObj.category) { return hashObj.category; } else { return false; }
}

function getProduct(hash) {
   var hashObj = $j.deparam.fragment(hash);
   if (hashObj.product) { return hashObj.product; } else { return false; }
}

if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function (obj, fromIndex) {
    if (fromIndex == null) {
        fromIndex = 0;
    } else if (fromIndex < 0) {
        fromIndex = Math.max(0, this.length + fromIndex);
    }
    for (var i = fromIndex, j = this.length; i < j; i++) {
        if (this[i] === obj)
            return i;
    }
    return -1;
  };
}

/************A few functions we use************************************/

/*http://www.bennadel.com/index.cfm?dax=blog:1520.view
Lets me look for changes to the hash easily.
*/
( function( $ ){
	// Default to the current location.
        var strLocation = window.location.href;
	var strHash = window.location.hash;
	var strPrevLocation = "";
	var strPrevHash = "";

	// This is how often we will be checkint for
	// changes on the location.
	var intIntervalTime = 100;

	// This method removes the pound from the hash.
	var fnCleanHash = function( strHash ){
		return(
		strHash.substring( 1, strHash.length )
		);
	}

	// This will be the method that we use to check
	// changes in the window location.
	var fnCheckLocation = function(){
		// Check to see if the location has changed.
		if (strLocation != window.location.href){

			// Store the new and previous locations.
			strPrevLocation = strLocation;
			strPrevHash = strHash;
			strLocation = window.location.href;
			strHash = window.location.hash;

			// The location has changed. Trigger a
			// change event on the location object,
			// passing in the current and previous
			// location values.
			$( window.location ).trigger(
				"change",
				{
					currentHref: strLocation,
					currentHash: fnCleanHash( strHash ),
					previousHref: strPrevLocation,
					previousHash: fnCleanHash( strPrevHash )
				}
				);

		}
	}

	// Set an interval to check the location changes.
	setInterval( fnCheckLocation, intIntervalTime );
	}
)( jQuery );

/*
 * jQuery BBQ: Back Button & Query Library - v1.0.3 - 12/2/2009
 * http://benalman.com/projects/jquery-bbq-plugin/
 *
 * Copyright (c) 2009 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,c){var g,m=c.location,i=Array.prototype.slice,F=decodeURIComponent,a=$.param,o,d,r,p=$.bbq=$.bbq||{},q,e,A,b="hashchange",w="querystring",z="fragment",y="elemUrlAttr",h="href",E="src",D=$.browser,n=D.msie&&D.version<8,l="on"+b in c&&!n,s=/^.*\?|#.*$/g,B=/^.*\#/,u={};function t(G){return typeof G==="string"}function x(H){var G=i.call(arguments,1);return function(){return H.apply(this,G.concat(i.call(arguments)))}}function j(G){return G.replace(/^[^#]*#?(.*)$/,"$1")}function k(G){return G.replace(/(?:^[^?#]*\?([^#]*).*$)?.*/,"$1")}function f(H,O,G,I,L){var N,M,K,P,J;if(I!==g){K=G.match(H?/^([^#]*)\#?(.*)$/:/^([^#?]*)\??([^#]*)(#?.*)/);J=K[3]||"";if(L===2&&t(I)){M=I.replace(H?B:s,"")}else{P=d(K[2]);I=t(I)?d[H?z:w](I):I;M=L===2?I:L===1?$.extend({},I,P):$.extend({},P,I);M=a(M)}N=K[1]+(H?"#":M||!K[1]?"?":"")+M+J}else{N=O(G!==g?G:m[h])}return N}a[w]=x(f,0,k);a[z]=o=x(f,1,j);$.deparam=d=function(J,H){var I={},G={"true":!0,"false":!1,"null":null};$.each(J.replace(/\+/g," ").split("&"),function(N,P){var M=P.split("="),Q=F(M[0]),L,R=I,O=0,S=Q.split("]["),K=S.length-1;if(/\[/.test(S[0])&&/\]$/.test(S[K])){S[K]=S[K].replace(/\]$/,"");S=S.shift().split("[").concat(S);K=S.length-1}else{K=0}if(M.length===2){L=F(M[1]);if(H){L=L&&!isNaN(L)?+L:L==="undefined"?g:G[L]!==g?G[L]:L}if(K){for(;O<=K;O++){Q=S[O]===""?R.length:S[O];R=R[Q]=O<K?R[Q]||(S[O+1]&&isNaN(S[O+1])?{}:[]):L}}else{if($.isArray(I[Q])){I[Q].push(L)}else{if(I[Q]!==g){I[Q]=[I[Q],L]}else{I[Q]=L}}}}else{if(Q){I[Q]=H?g:""}}});return I};function v(I,H,G){if(H===g||typeof H==="boolean"){G=H;H=a[I?z:w]()}else{H=t(H)?H.replace(I?B:s,""):H}return d(H,G)}d[w]=x(v,0);d[z]=r=x(v,1);$[y]||($[y]=function(G){return $.extend(u,G)})({a:h,base:h,iframe:E,img:E,input:E,form:"action",link:h,script:E});e=$[y];function C(J,G,I,H){if(!t(I)&&typeof I!=="object"){H=I;I=G;G=g}return this.each(function(){var M=$(this),K=G||e()[(this.nodeName||"").toLowerCase()]||"",L=K&&M.attr(K)||"";M.attr(K,a[J](L,I,H))})}$.fn[w]=x(C,w);$.fn[z]=x(C,z);p.pushState=q=function(J,I){if(t(J)&&/^#/.test(J)&&I===g){I=2}var H=J!==g,G=o(m[h],H?J:{},H?I:2);m[h]=G+(/#/.test(G)?"":"#")};p.getState=function(H,G){return H===g||typeof H==="boolean"?r(H):r(G)[H]};p.pollDelay=100;$.event.special[b]={setup:function(){if(l){return false}A.start()},teardown:function(){if(l){return false}A.stop()},add:function(G,I,H){return function(K){var J=K[z]=o();K.getState=function(M,L){return M===g||typeof M==="boolean"?d(J,M):d(J,L)[M]};G.apply(this,arguments)}}};A=(function(){var H={},L,G,I,K;function J(){I=K=function(M){return M};if(n){G=$('<iframe src="javascript:0"/>').hide().appendTo("body")[0].contentWindow;K=function(){return j(G.document.location[h])};I=function(O,M){if(O!==M){var N=G.document;N.open().close();N.location.hash="#"+O}};I(o())}}H.start=function(){if(L){return}var N=o();I||J();(function M(){var P=o(),O=K(N);if(P!==N){I(N=P,O);$(c).trigger(b)}else{if(O!==N){q("#"+O)}}L=setTimeout(M,p.pollDelay)})()};H.stop=function(){if(!G){L&&clearTimeout(L);L=0}};return H})()})(jQuery,this);



