$(document).ready(function() {
	
	//MENU HOVER
	$('#menu_links li').hover(
		function() {
			$(this).children('ul').show();
			$(this).children('a').addClass('menu-links-hover');
		},
		function() {
			$(this).children('ul').hide();
			$(this).children('a').removeClass('menu-links-hover');
		}
	);
	
	//SEARCH ON FOCUS
	$('#menu form input[type=text]').focus(function() {
		$("#search-box").slideDown();
	});
	$('#menu form input[type=text]').blur(function() {
		closeSearchBox();
	});
	
	//SEARCH BOX HOVER
	$("#menu form input[type=text]").hover(
		function() { $.data(this, 'hover', true); },
		function() { $.data(this, 'hover', false); }
	).data('hover', false);
	$("#search-box").hover(
		function() { $.data(this, 'hover', true); },
		function() { 
			$.data(this, 'hover', false);
			setTimeout('closeSearchBox();', 500);
		}
	).data('hover', false);
	
	//SEARCH CATEGORIES INPUT
	$("#allcats").change(function() {
		if($("#allcats").is(":checked")) {
			$('.allcats').attr('disabled', 'disabled');
			$('.allcats').attr('checked', 'checked');
		}
		else {
			$('.allcats').attr('disabled', false);
			$('.allcats').attr('checked', false);
		}
	});
	
	//RUN SCREENSHOT PREVIEW
	screenshotPreview();
	
	
	//GRID VIEW BUTTON CLICK
	$("#bgrid").click(function() {
		showGrid();
		return false;
	});
	
	
	//LIST VIEW BUTTON CLICK
	$("#blist").click(function() {
		showList();
		return false;
	});
	
	//READ COOKIE TO SHOW ITEMS TYPE
	if(checkItemsType && $.cookie("ShowItemsType") != null) {		
		if($.cookie("ShowItemsType") == 'grid') {
			showGrid();
		}
		else {
			showList();
		}
	}
	
	//TAB LIST LI
	$('.tab-list li:first').css('border-left', '0px');
	$('.tab-list li:last').css('border-right', '0px');
	
	//JQTRANSFORM SELECT
	$('.order, .fancy-form').jqTransform({imgPath:'../images/'});
	
	//TABLE ROW HOVER
	tableRowHover();
	
	//SHOW FANCYBOX
	$(".fancy-comment").fancybox({
		padding: 12,
		frameWidth: '542px',
		frameHeight: '270px',
		width: '542px',
		height: '270px',
		hideOnContentClick: false,
		enableArrowButtons: false
	});
	
	
	//BUY ITEM
	$("#purchase_licences").show();
	$("#purchase_licences a.btn-small").click(function () {
	    $(this).parents("#purchase_licences").find("[name=licence]").val($(this).val());
	    $(this).parents("#purchase_licences").find(".licence-name").text($(this).attr("title"));
	    return false
	});
	
	
	$(".fancy_purchase input[type=submit]").remove();
	var animatePanel = function (c) {
	    var d = !c ? "show" : c;
	    $(".fancy_purchase").animate({
	        height: d,
	        opacity: d,
	        marginBottom: d
	    }, "slow")
	};
	$("#buy_form").submit(function () {
	    animatePanel();
	    return false
	});
	$("#buy_form > button").click(function (b) {
	    b.preventDefault();
	    animatePanel();
	    return false
	});
	$(".fancy_purchase a.close").click(function () {
	    animatePanel("hide");
	    return false
	});
	$("a.buynow-submit").click(function () {
	    $(this).parent().siblings("form").submit();
	    return false
	});
	$("a.prepaid-submit").click(function () {
	    if (confirm_purchase($("#stored-item-name").val(), $("#stored-item-category").val())) {
	        $(this).parent().siblings("form").submit()
	    }
	    return false
	});

	function confirm_purchase(c, d) {
	    return confirm("You are about to purchase " + c + " using your prepaid balance.\n\nPlease review the item attributes to ensure this item suits your needs. We can only issue a refund if the item has not been downloaded, is faulty, or does not work as described on the item page.\n\nBy clicking okay you will immediately purchase this item.")
	}
		
});

/* AJAX */
this.showLoading = function() {
	$('body').append('<img src="/data/images/common/loading.gif" id="loading_img" style="display: none;" />');
	$("#loading_img").css({ position: 'fixed', bottom: '10px', left: '10px' });
	$("#loading_img").fadeIn();
};

this.hideLoading = function() {
	$("#loading_img").fadeOut("normal", function() {
		$("#loading_img").remove();		
	});
};

/* CLOSE SEARCH BOX */
this.closeSearchBox = function() {
	if(!$("#menu form input[type=text]").data('hover') && !$("#search-box").data('hover')) {
		$("#search-box").slideUp();
	}
};

/* SCREENSHOT PREVIEW */
this.screenshotPreview = function() {	
		
	$("a.tooltip-preview").hover(function(e){
		
		var windowX = $(window).width() / 2;
		var windowY = $(window).height() / 2;
		var xOffset = 10;
		var yOffset = 30;
		
		if(windowX < e.screenX) {
			yOffset = -600;
		}
		
		if(windowY < e.screenY) {
			xOffset = 350;
		}
		
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='screenshot'><img src='"+ this.rel +"' alt='url preview' />"+ c +"</p>");
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.css("z-index", "99999")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#screenshot").remove();
    });	
	$("a.screenshot").mousemove(function(e){
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});	
		
};

/* SHOW ITEMS IN GRID VIEW */
this.showGrid = function() {
	$("#list-left .box:not(.no-change)").css({width: '140px', textAlign: 'center', marginRight: '10px', float: 'left'});
	$("#list-left .box:not(.no-change) .box_inner").css({height: '175px', overflow: 'hidden', padding: '5px'});
	$("#list-left .box:not(.no-change) .box_inner .item_info, #list-left .box:not(.no-change) .box_inner .item_name .author, #list-left .box:not(.no-change) .box_inner .item_name h1.large-title, #list-left .box:not(.no-change) .box_inner .item_price img").hide();
	$("#list-left .box:not(.no-change) img.m6").removeClass('left');
	$("#list-left .box:not(.no-change) .box_inner .item_name, #list-left .box:not(.no-change) .box_inner .item_price").css({width: '120px'});
	$("#list-left .box:not(.no-change) .box_inner .item_name h1.short-title").css({height: '40px', overflow: 'hidden'});
	$("#list-left .box:not(.no-change) .box_inner .item_name h1.short-title").show();
	$("#list-left .box:not(.no-change) .box_inner .item_price h2").css({width: '25px', overflow: 'hidden', fontSize: '11px', margin: '0 0 0 5px', float: 'right'});
	$("#list-left .box:not(.no-change) .box_inner .item_price p").css({width: '85px', overflow: 'hidden', fontSize: '11px', borderRight: '1px solid #d1d1d1', padding: '0', float: 'left'});
	$("#list-left .box:not(.no-change) .box_inner .number").css({background: 'none', color: '#ccc', fontStyle: 'italic', border: '0px', margin: '0 0 0 -10px'});
	
	$("#blist").removeClass('btn-small-hover');
	$("#bgrid").addClass('btn-small-hover');

	$.cookie("ShowItemsType", "grid", { path: '/' });
};

/* SHOW ITEMS IN LIST VIEW */
this.showList = function() {
	$("#list-left .box:not(.no-change)").css({width: '100%', textAlign: 'left', marginRight: '0', float: 'none'});
	$("#list-left .box:not(.no-change) .box_inner").css({height: 'auto', overflow: 'visible', padding: '10px'});
	$("#list-left .box:not(.no-change) .box_inner .item_info, #list-left .box:not(.no-change) .box_inner .item_name .author, #list-left .box:not(.no-change) .box_inner .item_name h1.large-title, #list-left .box:not(.no-change) .box_inner .item_price img").show();
	$("#list-left .box:not(.no-change) img.m6").addClass('left');
	$("#list-left .box:not(.no-change) .box_inner .item_name").css({width: '270px'});
	$("#list-left .box:not(.no-change) .box_inner .item_price").css({width: '90px'});
	$("#list-left .box:not(.no-change) .box_inner .item_name h1.short-title").hide();
	$("#list-left .box:not(.no-change) .box_inner .item_price h2").css({width: 'auto', overflow: 'visible', fontSize: '33px', margin: '10px 0', float: 'none'});
	$("#list-left .box:not(.no-change) .box_inner .item_price p").css({width: 'auto', overflow: 'visible', fontSize: '13px', textAlign: 'center', borderRight: '0px', margin: '0 0 5px 0', float: 'none'});
	$("#list-left .box:not(.no-change) .box_inner .number").css({background: '#69472a', color: '#fff', fontStyle: 'normal', border: '1px solid #423021', margin: '0 0 0 -24px'});
	
	$("#bgrid").removeClass('btn-small-hover');
	$("#blist").addClass('btn-small-hover');

	$.cookie("ShowItemsType", "list", { path: '/' });
};

/* SUBMIT ORDER */
this.submitOrder = function(el) {
	if($("#order-type").val() == 'asc') {
		$("#order-type").val('desc');
	}
	else {
		$("#order-type").val('asc');
	}
	$(el).parent().submit();
};

/* SHOW TAGS */
this.showTags = function(id) {
	if(id == 'all') {
		$("#features, #usage, #style").show();
	}
	else {
		$("#features, #usage, #style").hide();
		$("#"+id).show();
	}
	$("#tags-all, #tags-features, #tags-usage, #tags-style").removeClass('btn-small-hover');
	$("#tags-"+id).addClass('btn-small-hover');
};

/* TABLE ROW HOVER */
var oldRowColor = '';
this.tableRowHover = function() {
	$('.general_table tbody tr').hover(
		function() {
			oldRowColor = $(this).children('td').css('backgroundColor');
			$(this).children('td').css('backgroundColor', '#fff');
		},
		function() {
			$(this).children('td').css('backgroundColor', oldRowColor);			
		}
	);
};

/* WITHDRAWAL PAY METHOD */
this.showAppropriateFields = function(id) {
	$('#paypal_instructions, #moneybookers_instructions, #swift_instructions, #payment_email_address_field, #instructions_field').hide();
	
	$('#'+id+'_instructions').show();
	
	if(id == 'paypal' || id == 'moneybookers') {
		$("#payment_email_address_field").show();
	}
	else {
		$("#instructions_field").show();
	}
};

/* WITHDRAWAL AUSTRALIAN RESIDENT */
this.showResidentType = function(ok) {
	if(ok) {
		$("#taxation-details, #resident_type").show();
	}
	else {
		$("#taxation-details, #resident_type").hide();
	}
};

/* WITHDRAWAL AUSTRALIAN TAX NUMBER */
this.showTaxNumber = function(ok) {
	if(ok) {
		$("#tax_number_fields").show();
	}
	else {
		$("#tax_number_fields").hide();
	}
};
