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);
}

Cufon.replace('#navigation .active ul a', {fontFamily: 'Marydale'});
Cufon.replace('#heading_msg strong, .foot h4, .foot .d4 ul li a', {fontFamily: 'Helvetica'});
Cufon.replace('#navigation .a-books', {fontFamily: 'Marydale'});
Cufon.replace('#navigation .a-course', {fontFamily: 'Marydale'});

show_tip = function(){};
save_to_session = function(){};

function fix_side_shadow() {
	var $j = jQuery.noConflict();
	var height = $j('.content').height() - parseInt($j('.side-shadow').css('top')) - $j('.foot').height() + 11;
	$j('.side-shadow').height(height);
	var height = $j('.content').height() - parseInt($j('.side-shadow-right').css('top')) - $j('.foot').height() + 1;
	$j('.side-shadow-right').height(height);
}
(function($){
	fix_side_shadow();
	$(document).ready(function () {
		fix_side_shadow();
		//fix_floating_positions();
	});
	$(window).load(function () {
		fix_side_shadow();
	});
	
	save_to_session = function(name, value) {
		var true_name = 'ajax-' + name;
		var getdata = {};
		getdata[true_name] = value;
		$.get(home_url, getdata);
	}
	
	show_tip = function(tip, li){
		$(tip).show();
		var top = Math.floor($(li).position().top - ($(tip).height() / 2) + ($(li).find('a:first').height() / 2) + parseInt($(li).find('a:first').css('margin-top'))) + 3;
		if ($(li).find('.secondary-a').length > 0) {
			top -= 8;
		}
		var left = $(li).width();
		$(tip).css({
			'top': top.toString() + 'px',
			'left': left.toString() + 'px'
		});
		var arrow_top = Math.floor(($(tip).height() / 2) - ($(tip).find('.gtip-arrow').height() / 2));
		$(tip).find('.gtip-arrow').css('top', arrow_top.toString() + 'px');
		$(tip).show();
	}
	$('.gtip').hide();
	
	function fix_floating_positions() {
		var left1 = $('.main > .content:eq(0)').position().left
		var left2 = $('.main').position().left
		var left3 = $('.main').css('margin-left')
		var left = left1 + left2 + (left3=='auto'?0:parseInt(left3));
		
		$('#subscribe_foot').css({
			'left': (left + 120).toString() + 'px'
		});
		
		$('#interested_foot').css({
			'left': (left + 120 + $('#subscribe_foot').width() + 23).toString() + 'px'
		});
		
		$('.foot-cnt .wmarker').css({
			'left': (left + $('.main > .content').width() - 65 + 60 * $('#interested_foot').length).toString() + 'px'
		});
		
		$('#bg-arrow').css({
			'left': ($('.main').position().left - 75).toString() + 'px',
		}).show();
	}
	
	$(window).resize(function() {
		//fix_floating_positions();
	});
	
	$(document).ready(function () {
		$('.blink').focus(function() {
			if ($(this).val() == $(this).attr('title')) {
				$(this).val('');
			}
		});
		$('.blink').blur(function() {
			if ($(this).val() == '') {
				$(this).val($(this).attr('title'));
			}
		});
		
		//fix_floating_positions();
		
		$('#subscribe_foot').click(function(e){
			if ($(e.originalTarget).parent().is('a')) {
				return true;
			}
			if ($(this).find('a#subfoot1').is(':visible')) {
				return false;
			}
			$('#subscribe_foot').stop().animate({'height': '248px'});
			$('a#subfoot1').show();
			return false;
		});
		$('a#subfoot1').click(function(){
			$('#subscribe_foot').stop().animate({'height': '72px'});
			$(this).hide();
			return false;
		});
		$('#subscribe_foot').stop().css({'height': '72px'});
		$('a#subfoot1').hide();
		
		$('#interested_foot').click(function(){
			if ($(this).find('a#subfoot1').is(':visible')) {
				return false;
			}
			$('#interested_foot').stop().animate({'height': '213px'});
			$('a#intfoot1').show();
			return false;
		});
		$('#interested_foot .s2 a').click(function(){ //override the above behavior for anchors within the text
			window.location=$(this).attr('href');
			return false;
		});
		$('a#intfoot1').click(function(){
			$('#interested_foot').stop().animate({'height': '72px'});
			$(this).hide();
			return false;
		});
		$('#interested_foot').stop().css({'height': '72px'});
		$('a#intfoot1').hide();

		assess_foot_animating = false;
		$('#assess_foot').hover(function(e){
			if (!assess_foot_animating) {
				if ($(this).find('a#assessfoot1').is(':visible')) {
					return false;
				}
				assess_foot_animating = true;
				$('#assess_foot').stop().animate({'height': '148px'},{complete: function(){assess_foot_animating = false;}});
				$('a#assessfoot1').show();
			}
			return false;
		}, function(e) {
			if (!assess_foot_animating) {
				assess_foot_animating = true;
				$('#assess_foot').stop().animate({'height': '72px'},{complete: function(){assess_foot_animating = false;}});
				$('a#assessfoot1').hide();
			}
			return false;
		});
		$('a#assessfoot1').click(function(){
			assess_foot_animating = true;
			$('#assess_foot').stop().animate({'height': '72px'},{complete: function(){assess_foot_animating = false;}});
			$(this).hide();
			return false;
		});
		$('#assess_foot').stop().css({'height': '72px'});
		$('a#assessfoot1').hide();
		
		$('body, .heading_msg_x').click(function(e){
			if (!$('.heading_msg_x').is(':visible')) {
				return true;
			}
			
			if ($(this).is('.heading_msg_x')) {
				if ($(e.currentTarget).hasClass('heading_msg_x') || $(e.currentTarget).parents('#heading_msg').length == 0) {
					save_to_session('hide-notification', 'closed');
					$('#heading_msg').slideUp();
					$('#bg-arrow').hide();
				}
				return false;
			} else {
				save_to_session('hide-notification', 'to-be-closed');
			}
			return true;
		})
		
		$('#home-tabs .tabs-head a').live('click', function() {
			return true;
			var index = $('#home-tabs .tabs-head a').index(this);
			$('#home-tabs .tabs-head a').removeClass('active');
			$(this).addClass('active');
			$('#home-tabs .tab-content').hide();
			$('#home-tabs .tab-content:eq(' + index.toString() + ')').show();
			return true;
		});
		
		$('#navigation > li').not('#navigation > li.active').hover(function() {
			var n = $('#navigation > li').index(this) + 1;
			$(this).addClass('hover');
			show_tip($('.tip'+n), $(this));
		}, function() {
			var n = $('#navigation > li').index(this) + 1;
			$(this).removeClass('hover');
			$('.tip'+n).hide();
		});
		
		var showed_interested = false;
		if ($('.reply-box').length > 0) {
			$(window).scroll(function() {
				if (showed_interested == false && $(window).scrollTop() + $(window).height() >= $('.reply-box').position().top) {
					$('#interested_foot').click();
					showed_interested = true;
				}
			});
		}
		$('body').click(function(e) {
			if ($(e.currentTarget).parents('#subscribe_foot').length == 0) {
				$('#subfoot1').click();
				$('#intfoot1').click();
			}
			return true;
		});
		
		$('.taxonomy-list > a').click(function() {
			if ($(this).next().is(':visible')) {
				$(this).parent().find('ul').stop().slideUp();
				return false;
			}
			$(this).parent().find('ul').stop().slideUp();
			$(this).next().stop().slideDown();
			return false;
		});
		
		$('.show-product').live('click', function() {
			if ($(this).next().find('.product-entry-cnt').is(':visible')) {
				$(this).parent().find('.product-entry-cnt').stop().slideUp();
				window.location.hash = "close-all-a";
				return false;
			}
			$(this).parent().find('.product-entry-cnt').stop().slideUp();
			$('.selected').removeClass('selected');
			$(this).next().find('.product-entry-cnt').stop().slideDown();
			$(this).next().addClass('selected');
			window.location.hash = $('.selected').prev().text().toLowerCase().replace(/[ ,.'"?#!@$%^&*()]/g,'-');
			return false;
		});
		if (window.location.hash == "") {
			$('.show-product:first').click();
		} else {
			$('.show-product' + window.location.hash + '-a').click();
		}
		
		var top = $('.foot .d4d').height() + 10;
		$('#paperclip').css('top', top.toString() + 'px');
		
		/*if ($('#interested_foot').length > 0) {
			var difference = parseInt($('#interested_foot').css('left')) + $('#interested_foot').width() - $('.foot-cnt .wmarker').position().left;
			if (difference > 0) {
				$('.foot-cnt .wmarker').css('right', (-difference).toString() + 'px');
			}
		}*/
		
		$('.header-socials .stbutton img, .footsoc .stbutton img').attr('src', theme_url + '/images/soc4_share.png').css({'width': '25px', 'height': '25px'});
		$('.single-socials .stbutton').removeClass('vertical').addClass('horizontal');
	});
	
	$(window).load(function () {
		$('.header-socials .stbutton img, .footsoc .stbutton img').attr('src', theme_url + '/images/soc4_share.png').attr('style', 'width: 25px !important; height: 25px !important;');
		$('.footsoc .stbutton').append('<div class="to-cufon" style="padding-left: 30px;">Share</div>');
		Cufon.replace('.foot .to-cufon', {fontFamily: 'Helvetica'});
		$('.single-socials .stbutton').removeClass('vertical').addClass('horizontal');
		//fix_floating_positions
	});
	
})(jQuery)
