
var trace = function(msg) { try { console.log(msg) } catch(e) {} }

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

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

  function eraseCookie(name) {
  	createCookie(name,"",-1);
  }
  /**
    Cart Totals
  **/
  function updateCartItemTotal(){
    if(readCookie("gtcart")){
      Cart = {};
      Cart = JSON.parse(readCookie("gtcart"));
      var count = 0;
      for (var i in Cart) {
        if(Cart[i].quantity) {
          count++;
        }
      }
      createCookie("itemTotal",count,1);
    }
    if(readCookie("itemTotal")){
      var itemTotal = readCookie("itemTotal");
      if(itemTotal == 0){
        if($(".headerViewCartLink")){
          $("li.headerViewCartLink").remove();
          if($('.cartheader span.numOfItems')){
            $('span.numOfItems').html("( 0 items )");
          }
        }
      }
      else if(itemTotal == 1){
        // View Cart (1 item)
        $('li.headerViewCartLink').remove();
        $("#topNav a:last").css("border-right","1px solid #745C36");
        $('<li class="headerViewCartLink"><a href="/take-action/cart" style="border: none"><span id="headerNumCount">View Cart ( 1 Item )</span></a></li>').insertAfter("li.searchLinkTopNav");
        if($('.cartheader span.numOfItems')){
          $('span.numOfItems').html("( 1 item )");
        }
      }
      else{
        // View Cart (2 items)
        $('li.headerViewCartLink').remove();
        $("#topNav a:last").css("border-right","1px solid #745C36");
        $('<li class="headerViewCartLink"><a href="/take-action/cart" style="border: none"><span id="headerNumCount">View Cart ( ' + itemTotal + ' Items )</span></a></li>').insertAfter("li.searchLinkTopNav");
        if($('.cartheader span.numOfItems')){
          $('span.numOfItems').html("( " + itemTotal +" items )");
        }
      }
      
    }
  }
  /*
    Destroys Cart and View Totals
  */
  function destroyCart(){
    
  }  
$(document).ready(function(){

	
    $('a[href=#submitForm]').click(function(){
       $(this).closest('form').submit(); 
       return false;
    });
    
    
    $('input.defaultText').each(function(){
        $(this).val($(this).attr('alt'));  
    }).focus(function(){
        if($(this).val() == $(this).attr('alt')) {
            $(this).val('');
        }
    }).blur(function(){
        if($(this).val() == '') {
            $(this).val($(this).attr('alt'));
        }
    });
    
	jQuery.fn.centerScreen = function(loaded) {
		var obj = this;
		var addTop = $(document).scrollTop();
		if(!loaded) {
			obj.css('top', ($(window).height()/2-this.height()/2)+addTop);
			obj.css('left', $(window).width()/2-this.width()/2);
			$(window).resize(function() { obj.centerScreen(!loaded); });
			$(window).scroll(function() { obj.centerScreen(!loaded); });
		} else {
			obj.stop();
			obj.animate({ top: ($(window).height()/2-this.height()/2)+addTop, left: $(window).width()/2-this.width()/2}, 200, 'linear');
		}
	}
	
	$('.closeTop').click(function(){
		$(this).parent().parent().slideUp();
	});
	
	$('.faqList h4').hover(function(){
		$(this).css('color', '#5e4a2a');
	}, function() {
		$(this).css('color', '#796038');
	})
	
	$('.faqList h4').click(function(){
		var open = $(this).parent().find("p:visible").length;

		$('.faqList h4 span').removeClass('active');
		$('.faqList p').hide();
		if(open == 0) {
		    $(this).find('span').addClass('active');
		    $(this).parent().find('p').show();
		}
		
		// if(!$.browser.msie) {
		//            $('.faqList h4 span').css('backgroundImage','url(/images/faqArrowRight.gif)');
		//            $('.faqList p').hide();
		//            if(open == 0) {
		//                $(this).find('span').css('backgroundImage','url(/images/faqArrowDown.gif)');
		//                $(this).parent().find('p').show();
		//            }
		//        } else {
		//            $('.faqList h4 img').attr('src','/images/faqArrowRight.gif');
		//            $('.faqList p').hide();
		//            if(open == 0) {
		//                $(this).parent().find('img').attr('src','/images/faqArrowDown.gif');
		//                $(this).parent().find('p').show();
		//            }
		//        }
	});
	
	$('.mainNavLink').parent().hover(function(){
		var current=$(this).find('.mainNavLink').css('backgroundPosition').match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		$(this).find('.mainNavLink').animate({ backgroundPosition: "("+current[1]+current[2]+" -21px)" }, 200);
		$(this).find('.subMenu').show();
		if($(this).find('.subMenu').length && $(this).attr('setSubMenu') != 1) {
			$(this).find('li:last').addClass('nopadding');
			var thisCenter = $(this).position()['left'] + ($(this).width()/2);
			var subMenuWidth = $(this).find('ul').width();
			var subMenuLeft = thisCenter - (subMenuWidth/2);
			if(subMenuLeft + subMenuWidth > 940) subMenuLeft = 940 - subMenuWidth;
			if(subMenuLeft < 0) subMenuLeft = 0;
			$(this).find('.subMenu ul').css('left', subMenuLeft);
			$(this).attr('setSubMenu',1); // only do all of this math once...
		}
	},function() {
		var current=$(this).find('.mainNavLink').css('backgroundPosition').match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		$(this).find('.mainNavLink').animate({ backgroundPosition: "("+current[1]+current[2]+" 0px)" }, 200);
		$(this).find('.subMenu').hide();
	});
	
	$('#topNavSearchLink').click(function(){
		$('#topLogin').hide();
		$('#topSearch').slideToggle(400);
		return false;
	});
	
	$('.feedQuestion').click(function(){
		$(this).parent().find('p').toggle();
		if($(this).parent().find('p:first').css('display') == 'block') {
			$(this).find('img').attr('src','/wordpress/wp-content/themes/greaterthan/images/circleArrowDown.gif');
		} else {
			$(this).find('img').attr('src','/wordpress/wp-content/themes/greaterthan/images/circleArrowRight.gif');
		}
		return false;
	});
	
	$('.videoToggle').click(function(){
		$('.videoToggle').css('color','');
		$(this).css('color','#ffffff');
		if($(this).text() == 'REAL TALK') {
			$('.videoOther').hide();
		} else {
			$('.videoOther').show();
		}
		updateVideoNavigation();
		return false;
	});
	
	$('img[hover_src]').each(function(){
		var image = new Image();
		image.src = $(this).attr('hover_src');
	});
	
	$('img[hover_src]').hover(function() {
		$(this).attr('orig_src', $(this).attr('src'));
		$(this).attr('src', $(this).attr('hover_src'));
	}, function(){
		$(this).attr('src', $(this).attr('orig_src'));
	});
	
	$(document).pngFix(); 
	
	$('#contentBody a, .tweets li p a').each(function(){$(this).attr('target','_blank');});
		
	$('.closeLink').click(function(){
		if($(this).hasClass('cform')) {
			$(this).parent().parent().parent().slideToggle();
		} else {
			$(this).parent().parent().slideToggle();
		}
	});
	
	if($.browser.msie) {
		/** IE Fixes... **/
		$('.featuredItem:gt(3)').hide();
		$('.faqList h4 span').addClass('msie');
		$('.faqList h4 span').after('<img src="/wordpress/wp-content/themes/greaterthan/images/faqArrowRight.gif" style="border: none; vertical-align: bottom; margin-left: 10px" />')
	}
	
	$('#sidebar select').uniform();

    
  //overides styling for the top nav li so that the view cart
  // border can work with out looking wierd  
  $("#topNav a:last").css("border","none");
  updateCartItemTotal();
		
});








