$(document).ready(function() {
	

	$('.tooltip').tooltip({
			delay: 0, 
		    showURL: false, 	
		    left: -45,
		    top: -35
	});

	//$(".user-icons img[title], .btn_holder img[title]").tipTip();

	$('.wysiwyg').wysiwyg({
		controls: {
			strikeThrough: { visible : true },
			underline: { visible : true },
			separator00: { visible : true },
			insertUnorderedList: { visible : true },
			insertHorizontalRule: { visible : true }
		}
	});
	
	//MENU HOVER
	$('.top-list li').hover(
		function() {
			$(this).children('div').show();
			//$(this).children('a').addClass('menu-links-hover');
		},
		function() {
			$(this).children('div').hide();
			//$(this).children('a').removeClass('menu-links-hover');
		}
	);
	
	//MENU HOVER
	$('.main-list li').hover(
		function() {
			$(this).children('div').show();
			$(this).children('a').addClass('menu-links-hover');
		},
		function() {
			$(this).children('div').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;
		var price = $(this).attr('alt');
		if(windowX < e.screenX) {
			yOffset = -600;
		}
		
		if(windowY < e.screenY) {
			xOffset = 350;
		}
		
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/><span class='stitle'>" + this.t+"</span>" : "";
		$("body").append("<p id='screenshot'><img src='"+ this.rel +"' alt='url preview' />"+ c + "</p>");
		var pHTML = $(".sprice").html();
		if(price && pHTML!=price) {
			if(pHTML)
				$(".sprice").html(price);
			else
				$("#screenshot").append("<div class='sprice'>"+price +"</div>");
		}
		else if(price && pHTML!=null)
			$(".sprice").html(price);
		else if(pHTML)
			$(".sprice").remove();
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.css("z-index", "99999")
			.css("display", 'block');						
    },
	function(){
		this.title = this.t;
		//$(".sprice").remove();
		$("#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() {
	$(".listing-holder").css({width: '740px', paddingLeft: '0px', 'overflow': 'visible'});
	$(".product-holder").css({width: '100px', marginLeft: '45px', float: 'left', 'background': 'none'});
	$(".product-holder .author").css({margin: '0px'});
	$(".product-holder .MCenter").attr("class", "center-holder");
	$(".product-holder .uploader").css({width: '80px','height': '28px','overflow':'hidden','text-align': 'center'});
	$(".product-holder .product-title").css({'font-size': '12px', 'line-height': '14px'});
	$(".product-holder .product-info").css({'display':'none'});
	$(".product-holder .price span").css({'float':'left', 'font-size':'11px','margin-top': '-8px', 'height': '11px'});
	$(".product-holder .price p").css({'float':'right', 'font-size':'11px'});
	$(".product-holder .rating-holder").css({'clear':'both'});

	$("#blist").removeClass('active');
	$("#bgrid").addClass('active');
	$.cookie("ShowItemsType", "grid", { path: '/' });
};

/* SHOW ITEMS IN LIST VIEW */
this.showList = function() {
	$(".listing-holder").css({paddingLeft: '15px'});
	$(".product-holder").css({width: '735px', marginLeft: '0px', 'background': 'url('+data_server+'images/product-separator.png) bottom repeat-x'});
	$(".product-holder .uploader").css({width: '230px','height': 'auto', 'text-align':'left'});
	$(".product-holder .product-title").css({'font-size': '15px', 'line-height': '18px'});
	$(".product-holder .product-info").css({'display':'block'});
	$(".product-holder .price span").css({'float':'none', 'font-size':'12px'});
	$(".product-holder .price p").css({'float':'none', 'font-size':'32px'});
	$(".product-holder .center-holder").attr("class", "MCenter");
	$(".product-holder .rating-holder").css({'clear':'both'});
	
	$("#bgrid").removeClass('active');
	$("#blist").addClass('active');

	$.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();
	}
};


function all_earnings_selected() {
    $("#all-earnings-notice").show();
    set_amount_field_enabling()
}
function custom_amount_selected() {
    $("#all-earnings-notice").hide();
    set_amount_field_enabling()
}
function set_amount_field_enabling() {
    document.getElementById("amount").disabled = document.getElementById("maximum_at_period_end_true").checked;
}
