function pageLoaded() {
	Cufon.replace('#navigation a', { 
		fontFamily: 'CenturyGothic', 
		hover: true, 
		textShadow: '1px 1px 2px #0081ae'
	});
	Cufon.replace('#slogan, #slider h2, .news .body h3, .bottom-box h2, .post h1, .post h2, .post h3, .post h4, .submenu a span, .pagetitle', { 
		fontFamily: 'CenturyGothic'
	});
	Cufon.now();
};

function isiOS(){
    return (
        (navigator.platform.indexOf("iPhone") != -1) ||
        (navigator.platform.indexOf("iPad") != -1) ||
        (navigator.platform.indexOf("iPod") != -1)
    );
}

jQuery(function($) {
	
	var email_regex = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";
	
	$('#navigation > ul > li:last').addClass('last');
	$('#footer .bottom-nav span:last').remove();
	
	function mycarousel_initCallback(carousel) {		
		$('#slider-controls a.next').click(function() {
			carousel.next();
			return false;
		});
		$('#slider-controls a.prev').click(function() {
			carousel.prev();
			return false;
		});
		$("#slider-navigation a").click(function(){
			$("#slider-navigation a").removeClass('active');
			$(this).addClass('active');
	    	var x = parseInt($(this).attr('rel'));
	    	carousel.scroll(x);
	    	return false;
	    });
	};
	function mycarousel_itemVisibleInCallback(carousel, li, pos, state) {
		$("#slider-navigation a").removeClass('active');
		$("#slider-navigation a").eq(pos-1).addClass('active');
		var background_color = $(li).find('h2:eq(0)').css('color');
		$('#slider-navigation').animate({'background-color': background_color}, 500);
	};
	function roundImages() {
	  $('img.rounded:visible, a.rounded-container img:visible').each(function() {
		  	if ($(this).parent().hasClass('rounded_wrapper')) {
		  		return;
		  	}
		  	
		  	var img = $(this);
			var img_width = img.width();
			var img_height = img.height();
			
			var wrapper = $('<span class="rounded_wrapper" />');
			wrapper.width(img_width);
			wrapper.height(img_height);
			
			// move CSS properties from img to wrapper
			wrapper.css('float', img.css('float'));
			img.css('float', 'none');
			
			wrapper.css('margin-right', img.css('margin-right'));
			img.css('margin-right', '0')
		 
			wrapper.css('margin-left', img.css('margin-left'));
			img.css('margin-left', '0')
		 
			wrapper.css('margin-bottom', img.css('margin-bottom'));
			img.css('margin-bottom', '0')
		 
			wrapper.css('margin-top', img.css('margin-top'));
			img.css('margin-top', '0')
		 
			wrapper.css('display', 'block');
			img.css('display', 'none');
			
			wrapper.css('background-image', 'url('+img.attr('src')+')');
			
			img.wrap(wrapper);
		});	
	};	

  $('.news .head li a').click(function() {
  	var href = $(this).attr('href');
  	if ($(href).is(':visible')) {
  		return false;
  	}
  	
  	$('.news .head li').removeClass('active');
  	$(this).parent().addClass('active');
  	$('.news .body .tab').hide();
  	$(href).fadeIn();
  	
  	roundImages();
  	
  	return false;
  });
  
  var rand_quote = 1+Math.floor(Math.random()*7); 

  $('#wrapper-3').removeClass().addClass('quote'+rand_quote);
  
  if ($('#slider').length) {
  	try {
	  	$("#slider ul").jcarousel({
			scroll: 1,
			wrap: 'both',
			initCallback: mycarousel_initCallback,
			itemVisibleInCallback: mycarousel_itemVisibleInCallback,
			buttonNextHTML: null,
			buttonPrevHTML: null
	    });	
  	} catch (e) {
  		// pass on IE
  	}
  	
  }
	function rps_initCallback(carousel) {		
		$('#recent-posts-controls a.next').click(function() {
			carousel.next();
			return false;
		});
		$('#recent-posts-controls a.prev').click(function() {
			carousel.prev();
			return false;
		});
	};

  if ($('#recent-posts-slider').length) {
  	if ($('#categorized-post-tabs').is(':visible')) {
	  	$("#recent-posts-slider").jcarousel({
			scroll: 1,
			wrap: 'both',
			initCallback: rps_initCallback,
			buttonNextHTML: null,
			buttonPrevHTML: null
	    });	
  	}
  };
  
  // FLIPPING BOXES
  $('#contact-us h4 a').click(function() {
  	if ($(this).parents('.panel').hasClass('active')) {
  		$(this).parents('.panel').css('z-index',0).removeClass('active').animate({top: '0'},300).next().css('z-index',2).addClass('active').animate({top: '32px'}, 300);
  	} else {
  		$(this).parents('.panel').css('z-index',2).addClass('active').animate({top: '32px'},300).next().css('z-index',0).removeClass('active').animate({top: '0'}, 300);
  	};
  	return false;
  });
  
  $('#follow-us h4 a').click(function() {
  	if ($(this).parents('.panel').hasClass('active')) {
  		$(this).parents('.panel').css('z-index',0).removeClass('active').animate({top: '0'},300).prev().css('z-index',2).addClass('active').animate({top: '32px'}, 300);
  	} else {
  		$(this).parents('.panel').css('z-index',2).addClass('active').animate({top: '32px'},300).prev().css('z-index',0).removeClass('active').animate({top: '0'}, 300);
  	};
  	return false;
  });
  
  $('.close').live('click',function() {
  	$(this).parent().fadeOut();
  	return false;
  });
  
  $('a[href="#link-to-us"]').click(function() {
  	$('#link-to-us').fadeIn();
  	return false;
  });
  
  if ($.browser.msie) {
  	DD_roundies.addRule('blockquote, .comment-content, .rounded_wrapper, #tab-comments .btns a', '15px');
  };
  if ($.browser.msie && $.browser.version.substr(0,1)<7) {
  	DD_belatedPNG.fix('#logo a, .twitter .head, .twitter .body, .facebook .head, .facebook .body, #sidebar .vcard, .post li, .news .head li a, .older-posts, .recent-posts .btn2, .rocket, #slider-holder, #slider-navigation a, a.button, #flip .panel .vcard, .breadcrumbs .sep, #wrapper-2, .rocket-shadow, .sponsor-this-post, #sponsor-post-popup');
  };
  
  
  $('.blink').focus(function () {
  	if ($(this).val() == $(this).attr('title')) {
  		$(this).val('');
  	}
  });
  $('.blink').blur(function () {
  	if ($(this).val() == '') {
  		$(this).val($(this).attr('title'));
  	}
  });
  
  $('.pretty-tabs .head a').click(function() {
  	var href = $(this).attr('href');
  	$('.pretty-tabs .head a').removeClass('active');
  	$(this).addClass('active');
  	$('.pretty-tabs .tab').hide();
  	$(href).show();
  	if ($('body').hasClass('fullscreen-mode')) {
  		equalize_fullscreen_items();
  	} else {
  		equalize_items();
  	}
  	return false;
  });
  
  roundImages();
  
  $('.contact-details p:last').css('padding-bottom', 0);
	$('.link-to-this').click(function() {
		$('#link-to-this').fadeIn();
		return false;
	});
	
	$('.email-to-friend').click(function() {
		$('#email-to-friend').fadeIn();
		$.ajax({
			type: "GET",
			url: $('#email-to-friend form:eq(0)').attr('action').replace('send_mail=yes', 'add_session_var=yes'),
			success: function() {}
		});
		return false;
	});

	$('#tab-comments .btns a').click(function() {
		if ($(this).hasClass('active')) {
			return false;
		}
		$('#tab-comments .btns a.active').removeClass('active');
		$(this).addClass('active');
		$('.comment-inner-tab').hide();
		var href = $(this).attr('href');
		$(href).show();
	  	if ($('body').hasClass('fullscreen-mode')) {
	  		equalize_fullscreen_items();
	  	} else {
	  		equalize_items();
	  	}
		return false;
	});
	
	$('.get-another-tweet').click(function() {
		var speed = "medium";
		if ($('.tweets .tweet:animated').length) {
			return false;
		}
		var total = $('.tweets .tweet').length;
		var current = $('.tweets .tweet').index($('.tweets .tweet:visible'));
		var next = (current == total - 1) ? 0 : current+1;
		$('.tweets .tweet').fadeOut(speed);
		$('.tweets .tweet:eq(' + next + ')').fadeIn(speed);
		return false;
	});
	
	var hash = window.location.hash;
	//when replying to a comment
	if (hash.indexOf('respond') > 0) {
		$('#comments').hide();
		$('#respond').show();
		$('.pretty-tabs ul li a[href="#tab-comments"]').click();
	}
	
	
	//equalize content and sidebar height
	function equalize_items() {
		var additional = 0;
		
		var ph = $('.pretty-tabs').height();
		$('#content-inner').css('padding-bottom', ph);
		
		$('#content-inner').height($('#content-inner-cnt').height());
		$('#sidebar-t').height($('#sidebar-inner').height());
		
		if ($('.submenu').length) {
			additional += $('.submenu').height();
		}
		
		var ch = $('#content').height() + additional;
		var sh = $('#sidebar').height();
		if (ch == sh) {
			return false;
		}
		
		if (ch > sh) {
			$('#sidebar-t').height(ch);
		} else {
			var target = sh - additional - ph - 18;
			$('#content-inner').height(target);
		}
	}
	
	function equalize_fullscreen_items() {
		var ph = $('.pretty-tabs').height();
		$('#fullscreen-page-inner').css('padding-bottom', ph + 20);
	}
	
	$(window).load(function() {
	  	if ($('body').hasClass('fullscreen-mode')) {
	  		equalize_fullscreen_items();
	  	} else {
	  		equalize_items();
	  	}
	});
	
	if (!$.browser.msie) {
		$("#slider li").touchwipe({
		     wipeLeft: function() { 
		     	$('#slider-controls a.prev').click();
		     },
		     wipeRight: function() {
		     	$('#slider-controls a.next').click();
		     },
		     min_move_x: 20,
		     preventDefaultEvents: true
		});	
		
		$("#recent-posts-slider li").touchwipe({
		     wipeLeft: function() { 
		     	$('#recent-posts-controls a.prev').click();
		     },
		     wipeRight: function() {
		     	$('#recent-posts-controls a.next').click();
		     },
		     min_move_x: 20,
		     preventDefaultEvents: true
		});	
	}

	$('.send-email-button').live('click', function() {
		if (!$('#your_email').val().match(email_regex) || !$('#friend_email').val().match(email_regex)) {
			alert('Please input the correct email addresses!');
			return false;
		}
		if (!$('#email_text').val()) {
			alert('Please input a message to your friend.');
		}
		var dataString = 'your_email='+ $('#your_email').val() + '&friend_email=' + $('#friend_email').val() + '&email_text=' + $('#email_text').val();
		$.ajax({
			type: "POST",
			url: $(this).parent('form').attr('action'),
			data: dataString,
			success: function() {
				$('#email_form').html("<p style='color: #fff; padding: 0 11px !important;'>Message has been sent!</p>");
				setTimeout(function() {
					$('#email-to-friend').fadeOut();
				}, 1500);
			}
		});
		return false;
	});
	
	//If reading a comment, the comments tab will be open
	var hash = window.location.hash;
	if (hash.indexOf('comment-') > -1) {
		$('.pretty-tabs a[href="#tab-comments"]').click();
	}
	
	$('#sponsor-post-popup .submit-button').live('click', function() {
		if (!$('#sponsor-your-name').val()) {
			alert('Please input your name.');
			return false;
		}
		if (!$('#sponsor-your-email').val().match(email_regex)) {
			alert('Please input the correct email address!');
			return false;
		}
		var dataString = $('#sponsor-post-popup form:eq(0)').serialize();
		$.ajax({
			type: "POST",
			url: $(this).parent('form').attr('action'),
			data: dataString,
			success: function() {
				$('#sponsor-post-popup form').html("<p>Thank you for your interest in sponsoring this post!<br />We will contact you as soon as possible.</p>");
				setTimeout(function() {
					$('.te_close_link').click();
				}, 1500);
			}
		});
		return false;
	});
	
	$('#subscribe-form').submit(function() {
		if(!$('#subscribe-fname').val() || $('#subscribe-fname').val() == 'first') {
			alert('Please input your correct first name.');
			$('#subscribe-fname').focus();
			return false;
		} else if(!$('#subscribe-lname').val() || $('#subscribe-lname').val() == 'last') {
			alert('Please input your correct last name.');
			$('#subscribe-lname').focus();
			return false;
		} else if (!$('#subscribe-form input[name=EMAIL]').val().match(email_regex)) {
			alert('Please input your correct email address.');
			$('#subscribe-email').focus();
			return false;
		}
	});
	
	$('a.colorbox-iframe').colorbox({
		inline: true
	});
	
}); 
  
