/*
 * Site Common Javascript
 *
 * May 10, 2010
 *
 */

// init the local namespace. All functions should be part of this namespace
var $gwx = window.$gwx || {};  



$(document).ready(function() {

    $clxcommon.initExternal();
    $clxcommon.initWarning();
    $clxcommon.attachClickAnalytics('clorox-promo');
    $clxcommon.attachClickAnalytics('clorox-video');
    $clxcommon.attachClickAnalytics('clorox-download');
    $clxcommon.attachClickAnalytics('clorox-virtual');

    	
 	//LINKED DIVS
 	//HOME
 		//in bubbles
 	$clxcommon.createLink('#homeBubbleGIY .heartButton', '/get-involved/giy-green-it-yourself/');
	$clxcommon.createLink('#homeBubbleDishReview .heartButton', '/products/dishwashing-liquid/');
	$clxcommon.createLink('#homeBubbleGoingNatural .heartButton', '/why-natural/');
	$clxcommon.createLink('#homeBubbleChlorineFreeBleach .heartButton', '/products/chlorine-free-bleach/');
	$clxcommon.createLink('#homeBubbleFlower .heartButton', '/products/');
	$clxcommon.createLink('#homeBubbleSpecialOffer .heartButton', '/user/register/');
	$clxcommon.createLink('#homeBubbleAllPurposeReview .heartButton', 'http://reviews.greenworkscleaners.com/2796/00001/natural-all-purpose-cleaner-reviews/reviews.htm');
	$clxcommon.createLink('#homeBubbleTopRated .heartButton', '/products/top-rated/');
	$clxcommon.createLink('#homeBubbleLaundryDetergent .heartButton', '/products/laundry-detergent/');
	$clxcommon.createLink('#homeBubbleToiletBowlCleaner .heartButton', '/products/toilet-bowl-cleaner/');
	$clxcommon.createLink('#homeBubbleMoneyBackGuarantee .heartButton', '/products/guarantee/');
	$clxcommon.createLink('#homeBubbleGreenItYourself .heartButton', 'http://www.facebook.com/greenworks?sk=app_199786930034675&amp;app_data=tag_gw-website');
 		//in carousel
 	$clxcommon.createLink('#carouselGIY', '/get-involved/giy-green-it-yourself/');
    $clxcommon.createLink('#carouselDishReview', '/products/dishwashing-liquid/');
    $clxcommon.createLink('#carouselGoingNatural', '/why-natural/');
    $clxcommon.createLink('#carouselChlorineFreeBleach', '/products/chlorine-free-bleach/');
	$clxcommon.createLink('#carouselFlower', '/about-us/');
	$clxcommon.createLink('#carouselSpecialOffer', '/user/register/');
	$clxcommon.createLink('#carouselAllPurposeReview', '/products/all-purpose-cleaner/');
	$clxcommon.createLink('#carouselTopRated', '/products/top-rated/');
	$clxcommon.createLink('#carouselLaundryDetergent', '/products/laundry-detergent/');
	$clxcommon.createLink('#carouselToiletBowlCleaner', '/products/toilet-bowl-cleaner/');
	$clxcommon.createLink('#carouselMoneyBackGuarantee', '/products/guarantee/');
	$clxcommon.createLink('#carouselGreenItYourself', 'http://www.facebook.com/greenworks?sk=app_199786930034675&amp;app_data=tag_gw-website');
	
	//BUTTONS
	$clxcommon.createLink('#dfeButton', 'http://www.epa.gov/dfe/');
	$clxcommon.createLink('#regButton', '/user/register/');
		
 

	//validate form field in footer
    $("#signUp").validate({
	    errorPlacement: function(error, element){
			error.insertAfter(".inlineSubmit");
		}
	});

	
	//on page load, if there is already an anchor in the url 
	//and a value after "_" (for anchoring to product reviews) go to that	
	
	theUrl = window.location.href;
	
	if(document.getElementById('reviewAnchor')!=-1){
		theDiv = document.getElementById('reviewAnchor');
	}
	
	if(theDiv!=null && theUrl.indexOf('#')!=-1){
	

		beforeHash = theUrl.split('#')[0];
		afterHash = theUrl.split('#')[1];
		
		if(afterHash.indexOf('_')!=-1){
		
			//create the anchor with proper "name" attribute value
			theAnchor = createElement(afterHash);
			theDiv.appendChild(theAnchor);
			
			window.location = theUrl;
			
			$('#ratingsLink').attr('href', beforeHash+'#'+afterHash);
			
		}else{
		
			$('#ratingsLink').attr('href', beforeHash+'#'+afterHash+"_productTab");
			
			//create the anchor with proper "name" attribute value
			theAnchor = createElement(afterHash+'_productTab');
			theDiv.appendChild(theAnchor);
		}
		
			
	//if there's no hash mark in the url
	}else if(theDiv!=null && theUrl.indexOf('#')==-1){
	

		//set the anchor "name" attribute value
		theAnchor = createElement('_productTab');
		theDiv.appendChild(theAnchor);
	
	}
	
	
	

});


//function for anchoring "read all reviews" button in products
function reviewAnchor(anchor,pageSegment){

	var theUrl = window.location.href;
	var theDiv = document.getElementById('reviewAnchor');
	
	//if there is a hash mark in the url
	if(theUrl.indexOf('#')!=-1){
		afterHash = theUrl.split('#')[1];
		
		//if there is no underscore in the url
		if(afterHash.indexOf('_')==-1){
			//create the anchor with proper "name" attribute value
			theAnchor = createElement(afterHash+'_'+anchor);
			theDiv.appendChild(theAnchor);
			
			//and go to a url with the underscore and value passed to this function included
			window.location = theUrl+'_'+anchor;
			
		//otherwise this has already been updated and we don't want to append again	
		}else{
			//so just load the current url again and anchor down
			window.location = theUrl;
		}
		
	//if there's no hash mark in the url
	}else{
		//set the anchor "name" attribute value
		theAnchor = createElement('_'+anchor);
		theDiv.appendChild(theAnchor);
		
		//if there's an page segment passed in
		if(pageSegment!=''){
			//strip it out, append anchor, and go there
			window.location = theUrl.replace(pageSegment+'/','')+'#_'+anchor;
		}else{
			window.location = theUrl+'#_'+anchor;
		}
		
	}
}
//need to dynamically create the anchor elements
//since we can't update the name attribute in ie
function createElement( name )
{
   var element = null;
try
   {
       element = document.createElement( "<a name='"+name+"'>" );
   } catch( e ) {};

   if( !element || element.name.toLowerCase() != name.toLowerCase() )
   {
       element = document.createElement( 'a' );
       element.name = name;
   };  

   return element;
}

// the overlay function for the newsletter popup
$("a[rel=#newsletter]").live('click', function (e) {
    e.preventDefault(); //prevent default link action

    $(this).overlay({
        api: true,
        load: true
    });
});

//basic cookie functions
function createCookie(name, value, minutes)
{
  if (minutes) {
    var date = new Date();
    date.setTime(date.getTime()+(minutes*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
  return null;
}

function eraseCookie(name)
{
  createCookie(name, "", -1);
}


