/**
 * @author alexlindsay
 */

function addHover(target) {
	target.addClass('hover');
}

function removeHover(target) {
	target.removeClass('hover');
}

function login() {
	jQuery.ajax({
		url:'/module_functions.php',
		type:'POST',
		data:{
			is_ajax: true,
			method_key: '6772a8896049e9bb37ac74fd5c81d8a8',
			method_name: 'login',
			email: jQuery('#email').val(),
			password: jQuery('#password').val()
		},
		success:function(msg) {
			document.location = document.location;
		}
	});
}

function logout() {
	if (!confirm('Are you sure you wish to log out?')) {
		return false;
	}
	jQuery.ajax({
		url:'/module_functions.php',
		data:{
			is_ajax: true,
			method_key: 'bfa2f814e2c3d6c9307123b152da5824',
			method_name: 'logout'
		},
		success:function(data) {
			document.location = '/';
		}
	});
}

function setupTopMenu() {
	jQuery('#top_menu li').click(function() {
		document.location = jQuery(this).find("a").attr('href');
	}).hover(function(){
		addHover(jQuery(this));
	}, function(){
		removeHover(jQuery(this));
	});
}

function setupBottomMenu() {
	jQuery('#bottom_menu li').click(function() {
		document.location = jQuery(this).find("a").attr('href');
	}).hover(function(){
		addHover(jQuery(this));
	}, function(){
		removeHover(jQuery(this));
	});
}

function setupCategories() {
	jQuery('.category').click(function() {
		var catName = jQuery(this).find("a:first").text();
		var catURL = jQuery(this).find("a:first").attr('href');
		setCookie('catName',catName,1);
		setCookie('catURL',catURL,1);
		document.location = catURL;
	}).hover(function(){
		addHover(jQuery(this));
		highlightCategory(jQuery(this));
	}, function(){
		removeHover(jQuery(this));
	});
	jQuery('.category:first').mouseover();
}

function highlightCategory(target) {
	lastCategory = target;
	var catName = jQuery(target).find('.category_name');
	var catImage = jQuery(target).find('.category_image');
	jQuery('#container_1_1_2_1').fadeTo('500','0.005',function() {
		jQuery(this).html(
		"<div class=\"category_detail\">\n"
		+ "\t<div class=\"category_inner\">\n"
		+ "\t\t<div class=\"category_name\">" + catName.html() + "</div>\n"
		+ "\t\t<div class=\"category_image\">" + catImage.html() + "</div>\n"
		+ "\t</div>\n"
		+ "</div>"
		).fadeTo('500','0.005',function(){
			jQuery(this).fadeTo('1000','1');
		});
	});
	if (typeof(timer) != 'undefined') {
		clearTimeout(timer);	
	}
	timer = setTimeout(rotateCategories,10000);
}

function setupButtonHovers() {
	jQuery("a img").live("mouseover", function() {
		if (!jQuery(this).parents('td:first').hasClass('productListing-data')) {
			var src = jQuery(this).attr("src");
			var ext = src.slice(src.lastIndexOf('.'));
			src = src.slice(0,-ext.length) + '_over' + ext;
			jQuery(this).attr("src",src);
		}
	}).live("mouseout", function() {
		if (!jQuery(this).parents('td:first').hasClass('productListing-data')) {
			var src = jQuery(this).attr("src");
			var ext = src.slice(src.lastIndexOf('.'));
			src = src.slice(0, -(ext.length + 5)) + ext; // +5 to pick up "_over"
			jQuery(this).attr("src", src);
		}
	});
}

function rotateCategories() {
	jQuery('.category').mouseout();
	var nextCategory = 
		((typeof(lastCategory) != 'undefined') 
		&& (lastCategory.next('.category').length != 0)) ? 
			lastCategory.next('.category') : jQuery('.category:first');
    var divOffset = jQuery('#container_1_1_2_2').offset().top;
    var pOffset = nextCategory.offset().top;
    var pScroll = pOffset - divOffset;
    jQuery('#container_1_1_2_2').animate({scrollTop: '+=' + pScroll + 'px'}, 1000);
	nextCategory.mouseover();
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());

}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function addCategoryBackButton() {
	var catName = getCookie('catName');
	var catURL = getCookie('catURL');
	if ((typeof(catName) != 'undefined') && (typeof(catURL) != 'undefined')) {
		jQuery('#container_1_1_2_2')
			.append('<div class="product_back"></div>')
			.find('.product_back')
				.append('<a></a>')
				.find('a')
					.attr('href', catURL)
					.attr('title', ' Back to: ' + catName + ' ')
					.text('Back to: ' + catName);
			
	}
}

jQuery(function() {
	setupTopMenu();
	setupBottomMenu();
	jQuery('#login_btn').click(login);
	jQuery('#logout_btn').click(logout);
	jQuery('#email').click(function() {
		jQuery(this).val('');
	}).blur(function() {
		if (jQuery.trim(jQuery(this).val()) == '') {
			jQuery(this).val('Email Address');
		}
	});
	setupCategories();
	setupButtonHovers();
	var url = new String(document.location);
	if (url.indexOf('products_id') != -1) {
		addCategoryBackButton();
	}
	timer = setTimeout(rotateCategories,10000);
});

