var closetimer = null;

jQuery.fn.center = function() {
	this.css("position","absolute");
	this.css("top", (jQuery(window).height()-this.outerHeight()) / 2 + jQuery(window).scrollTop() + "px");
	this.css("left", (jQuery(window).width()-this.outerWidth()) / 2 + jQuery(window).scrollLeft() + "px");
	return this;
}

function contact_open() {
	jQuery("#contact_iframe").attr("src",jQuery("#contact_iframe").attr("alt"));
	jQuery("#contact_popup").fadeIn();
	jQuery("#contact_popup div.popup_panel").center();
}
function contact_kill() {
	jQuery("#contact_iframe").attr("src",jQuery("#contact_iframe").attr("alt")+"?null");
	jQuery("#contact_popup").fadeOut();
}

function load_itunes_link(ajax,jsondata,album,update) {	
	if (jsondata['results'][0] == undefined) {
		var general_url = "http://click.linksynergy.com/fs-bin/click?id=Aw4C9w2mZUw&offerid=146261.10000007&type=3&subid=0";
		var general_pxl = "http://ad.linksynergy.com/fs-bin/show?id=Aw4C9w2mZUw&bids=146261.10000007&type=3&subid=0";
		jQuery("#itunes_price_tag").attr("href",general_url);
		jQuery("#album_info").append("<img src='"+general_pxl+"' width='1' height='1' />");
	} else {
		var itunes_url = jsondata['results'][0]['collectionViewUrl'];
		jQuery.ajax({
			url: ajax,
			type: "POST",
			data: "id="+album+"&url="+itunes_url+"&update="+update,
			dataType: "json",
			success: function(data) {
				jQuery('#itunes_price_tag').attr('href',data.url);
				jQuery('#album_info').append('<img src="http://ad.linksynergy.com/fs-bin/show?id='+data.aff+'&bids='+data.off+'&type=2&subid=0" width="1" height="1" />');
			}
		});
	}
}

function load_camel_price(jsondata) {
	var default_price = "9.99";
	try {
		var price_new = jsondata['prices']['new']['current']['price'].substr(1);
	} catch(err) { var price_new = default_price; }
	try {
		var price_used = jsondata['prices']['used']['current']['price'].substr(1);
	} catch(err) { var price_used = default_price; }
	try {
		var price_amazon = jsondata['prices']['amazon']['current']['price'].substr(1);
	} catch(err) { var price_amazon = default_price; }
	var low_price = '$'+Math.min(price_new,price_used,price_amazon).toFixed(2);
	jQuery('#c3_price_tag').text(low_price);
}

function artists_open() {
	artists_canceltimer();
	artists_close();
	jQuery('#artists_menu').css('display', 'block');
	jQuery('#artists_link > a').css('color', '#FFFFFF');
}	

function artists_close() {
	jQuery('#artists_menu').css('display', 'none');
	jQuery('#artists_link > a').css('color', '#FFF3D7');
}

function artists_timer() {
	closetimer = window.setTimeout(artists_close, 500);
}

function artists_canceltimer() {
	if (closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

function show_html_tags() {
	jQuery('#allowed_tags span.link').css('display', 'none');
	jQuery('#allowed_tags span.info').css('display', 'block');
}

function format_number(n) {
	n += '';
	x = n.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

jQuery.preloadImages = function() {
	for (var i=0; i<arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	}
}

var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
jQuery(document).keydown(function(e) {
	kkeys.push(e.keyCode);
	if (kkeys.toString().indexOf(konami) >= 0) {
		jQuery(document).unbind('keydown',arguments.callee);
		jQuery.getScript('http://www.cornify.com/js/cornify.js', function() {
			cornify_add();
			jQuery(document).keydown(cornify_add);
		});          
	}
});