/* 
Title:		Main Javascript
Author: 	3 Crown Creative
*/

//----------------------------------------------------------------------
// FUNCTION: Housekeeping
//----------------------------------------------------------------------
function houseKeeping() {
	checkBrowser();

/*  VOLUSION NAVIGATION HACK  */
	var sPage;
	var text = "";
	
       window.addEvent('domready',function() {
           
          if ($("product_photo_zoom_url")) {
               $("product_photo_zoom_url").addEvent('click', function(event) {
	           event.preventDefault();
	       });
           }


           var sQuery = window.location.search.substring(1).toLowerCase();
           var result = dissectQuery();
           if (result)  {   /*  category page OR article ID page  */
              setBodyTag (result);	
	    }
	   else {
		
		var thisPath = location.pathname.toLowerCase();
		var onHomePage = (thisPath == "/") || (thisPath.indexOf("/default.asp") != -1);
		
		if (onHomePage) {   /*  home page  */
		     $(document.body).set('id', 'home');	
                     var allTables = $('home').getElements('table');                   
		     $('featuredproducts').innerHTML = "<table>"+allTables[2].innerHTML+"</table>";
                     allTables[2].innerHTML = "";
		     window.addEvent('domready', function() {
			 	var slideshow = new BarackSlideshow('menu', 'pictures', 'loading', { transition: 'fade', auto: true, autostart: true});
		     });
		}
                else {
                     var sPage = thisPath.substring(thisPath.lastIndexOf('/') + 1);
                     switch (sPage) {
                           case 'terms_privacy.asp': 
		               $(document.body).set('id', 'privacy');	
  	                   break;
                          case 'shoppingcart.asp': 
			        $(document.body).set('id', 'cart');	
  		           break;
                           case 'myaccount.asp': 
			         $(document.body).set('id', 'myaccount');	
  		          break;
                          case 'giftoptions.asp': 
			         $(document.body).set('id', 'giftoptions');	
  		          break;
                          case 'help.asp': 
			         $(document.body).set('id', 'service');	
  		          break;

                          case 'one-page-checkout.asp': 
			         $(document.body).set('id', 'one-page-checkout');	
  		          break;                          
                         // case 'productdetails.asp':
                          default:
	                       if (document.forms['MainForm']) {
		                   var e1 = document.forms['MainForm'].getElement('table').getElement('td').getElement('a').getNext('a').get('href');
                                   var x = e1.substring(e1.lastIndexOf('/') + 1);
				   setBodyTag (x.substring(0,x.lastIndexOf('.htm')));
		              //     setBodyTag(e1.substring(e1.lastIndexOf('=') + 1));
	                       }
                         // break;
                     }
        }
	}

	});		

	
	window.addEvent('domready',function() {

		/*  (1) Make all links to external sites open in a new window  */
		$$('a[href^="http://"]').each(function(a) {   /* grab all complete linked anchors */
			var href = a.get('href');
			if(!href.contains(window.location.host)) {  /* if it's not this domain */
				a.setProperties({
					rel: 'nofollow',
					target: '_blank'
				});
			}
		});
		
		// Add pdf icons to pdf links
		$$("a[href$='.pdf']").each(function(a) {   
			var href = a.get('href');
				a.setProperties({
					rel: 'nofollow',
					target: '_blank'
				});
				a.addClass("pdf");
		});
		
		
		// Add txt icons to document links (doc, rtf, txt)
		$$("a[href$='.doc']","a[href$='.txt']", "a[href$='.rtf']").each(function(a) {   
			var href = a.get('href');
				a.setProperties({
					rel: 'nofollow',
					target: '_blank'
				});
				a.addClass("txt");
		});
	    
		// Add zip icons to Zip file links (zip, rar)
		$$("a[href$='.zip']","a[href$='.rar']").each(function(a) {   /* grab all complete linked anchors */
			var href = a.get('href');
				a.setProperties({
					rel: 'nofollow',
					target: '_blank'
				});
				a.addClass("zip");
		});

		

	});									 

}

//----------------------------------------------------------------------
// FUNCTION: return the query value for CAT or ID
//----------------------------------------------------------------------
function dissectQuery() {
    var sPath = window.location.search.substring(1).toLowerCase();
	var parms =  sPath.split("&");
	for (i=0; i < parms.length; i++) {
		parmPair = parms[i].split("=");
		if ( (parmPair[0] == "cat") || (parmPair[0]=="id") ) {
			sPage = parmPair[1];
			return sPage;
		}
	}
}
//----------------------------------------------------------------------
// FUNCTION: set id on body tag based on category
//----------------------------------------------------------------------
function setBodyTag(sPage) { 
      
      switch (sPage) {	
	    	    case '1':
			     $(document.body).set('id', 'vins');	
  		    break;
	            case '2': 
                           $(document.body).set('id', 'jellies');	
  		    break;
	            case '3': 
			     $(document.body).set('id', 'jams');	
  		    break;
	            case '4': 
 			     $(document.body).set('id', 'sauces');	
  		    break;
	            case '5': 
				 $(document.body).set('id', 'gifts');	
  		    break;
                    case '142':
                                $(document.body).set('id', 'wholesale');	
                    break;
                    case '143':
                                $(document.body).set('id', 'stores');	
                    break;
                    case '144':
                                $(document.body).set('id', 'recipes');	
                    break;
                    case '145':
                                $(document.body).set('id', 'contact');	
                    break;     }
}
//----------------------------------------------------------------------
// FUNCTION: Add Stylesheet when javascript is enabled
//----------------------------------------------------------------------
function linkCSS(title) { 
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     	if (a.getAttribute("rel").indexOf("style") != -1  && a.getAttribute("title")== title) {  
			a.disabled = true;
			a.disabled = false;
		}
    }
}

//---------------------------------------------------------------------------
//  FUNCTION: Check for browsers N4/IE4/IE5 Mac or older....
//---------------------------------------------------------------------------
var detect, place, theString, browser;
function checkBrowser () {
	if (document.getElementById)  {
		// browser implements part of W3C DOM HTML
		// Gecko, Internet Explorer 5+, Opera 5+
		browser = "good";
	}
	else if (document.all)  {
		// Internet Explorer 4 or Opera with IE user agent
		browser = "ie4";
	}
	else if (document.layers) {
		// Navigator 4
		browser = "net4";
		location.href = "oldbrowser.html";
	}
	
	detect = navigator.userAgent.toLowerCase();
	if (detect.indexOf("safari") != -1) 
		document.write('<link rel="stylesheet" type="text/css" href="styles/safari.css" />');


	if (checkIt('msie')) { // browser is IE
		var version = detect.charAt(place + theString.length);
		if (checkIt('mac'))
			location.href = "maciebrowser.html";
	}
}
function checkIt(string) {
	place = detect.indexOf(string) + 1;
	theString = string;
	return place;
}

//----------------------------------------------------------------------
// FUNCTION: Open a new window
//----------------------------------------------------------------------
function targetBlank (url) {
  blankWin = window.open(url,'_blank','menubar=yes,toolbar=yes,location=yes,directories=yes,fullscreen=no,titlebar=yes,hotkeys=yes,status=yes,scrollbars=yes,resizable=yes');
}
