var font_loaded = false;

function visualsButtonToggle(el, action) {
	var bkg_pos = $(el).css('background-position');
	if (action == 'over') {
		if (bkg_pos == '0px 0px') { bkg_pos = '-17px 0'; }
		else { bkg_pos = '0 0'; }
	}
	else {
		var html = $('div#bkg').html();
		if (action == 'click') {
			if (html == '') {
				$("<img>").appendTo('div#bkg');
				var img_url = www_path + 'img/bkgs/' + Math.ceil(Math.random()*4) + '.gif'
				$('div#bkg img').css('display', 'none');
				$('div#bkg img').attr('src', img_url);
				$('div#bkg img').load(function() {
					$('div#bkg img').css('display', 'block');
				});
				var bkg_pos = '-17px 0';
            	var cookie_val = 'on';
			}
			else {
				$('div#bkg').html('');
				var bkg_pos = '0 0';
                var cookie_val = 'off';
			}
            $.cookie("visuals", cookie_val, { expires: 364 });
    	}
		else {
			bkg_pos = html != '' ? '-17px 0' : '0 0';
		}
	}
	$(el).css('background-position', bkg_pos);
}

$(document).ready(function () {
	
	// load fonts
	Cufon.replace('h3', { fontSize: '28px', letterSpacing:'0.02em' } );
	Cufon.replace('div.paging a', { fontSize: '19px', letterSpacing:'1px' } );
	
	/* 
    check if cookie is set - change the button background position  
    (the actual visuals is disabled in layouts/header.php
    */
    var visuals = $.cookie("visuals");
    if (visuals && visuals == 'off') {
        $('a#visuals').css('background-position', '0 0');
    }
    
	// toggle visuals
	$('a#visuals').click(function(event) {
		event.preventDefault();
		visualsButtonToggle($(this), 'click');
	});
	
	// visual button rollovers
	$('a#visuals').mouseover(function(event) { visualsButtonToggle($(this), 'over'); });
	$('a#visuals').mouseout(function(event) { visualsButtonToggle($(this), 'out'); });
		
	// rollover text
	var default_rollover_text = false;
	$('a.rollover').mouseover(function() {
		if (!default_rollover_text) { default_rollover_text = $('#rollover_text').html(); }
		$('#rollover_text').html($(this).attr('title'));
	});
	$('a.rollover').mouseout(function() {
		$('#rollover_text').html(default_rollover_text);
	});
	
	// share
	$('div.share a.btn').click(function(event) {
		event.preventDefault();
		var href = $(this).attr('href');
		var title = $(this).attr('title');
		var div = $(this).next('div.bottom');
		
		var div_height = $(div).css('height').replace('px', '');
		var animation_time = 300;
		if (div_height == 1) {
			$(div).animate(
				{ height:'46px' }, 
				animation_time,
				function() {
					var share = '<a href="http://twitter.com/home?status=' + title + ' ' + href + '" title="share on Twitter" target="_blank">Twitter</a><br />';
				 	share += '<a href="http://www.facebook.com/sharer.php?u=' + href + '&t=' + title + '" title="share on Facebook" target="_blank">Facebook</a><br />';
					share += '<a href="mailto:?subject=MIDNIGHT JUGGERNAUTS - ' + title + '&body=' + href + '" title="email to a friend">Email</a>';
					$(div).html(share);
				}
			);
		}
		else {
			$(div).html('&nbsp;');
			$(div).animate({ height:'1px' }, animation_time);
		}
		
	});
	
	// shop toggle buttons
	$('a.box').click(function(event) {
		event.preventDefault();
		$('img#featured-image').attr('src', $(this).attr('href'));
	});
	
	// clear input fields
	$(':input.text').click(function() {
		$(this).attr('value', '');
	});
	
	// updates
	$.ajax({ url: www_path + 'update/facebook/' });
	$.ajax({ url: www_path + 'update/twitter/' });
	
});