// IMAGE PRELOADER

(function ($) {
	$.preLoadImages = function(imageList,callback) {
		var pic = [], i, total, loaded = 0;
		if (typeof imageList != 'undefined') {
			if ($.isArray(imageList)) {
				total = imageList.length;
					for (i=0; i < total; i++) {
						pic[i] = new Image();
						pic[i].onload = function() {
							loaded++;
							if (loaded == total) {
								if ($.isFunction(callback)) {
									callback();
								}
							}
						};
						pic[i].src = imageList[i];
					}
			}
			else {
				pic[0] = new Image();
				pic[0].onload = function() {
					if ($.isFunction(callback)) {
						callback();
					}
				}
				pic[0].src = imageList;
			}
		}
		pic = undefined;
	};
})(jQuery);


// TRANSITION IN UPON PAGELOAD

function transitionIn() {

	if ($.browser.msie) {
		$("#twitter_btn").show(); // You'll see this a lot -- IE still can't handle semi-opaque 24-bit PNGs.
	} else {
		$("#twitter_btn").delay(2000).fadeIn(250);
	};

	if ($.browser.msie) {
		$("#content").show();
	} else {
		$("#content").fadeIn(500);
	};
	
	if (!($.browser.msie && $.browser.version < "8.0")) {
	
		$(".left")
			.css("margin-left", "-700px")
			.animate( { marginLeft: "0px" }, 1500, "easeOutQuad" );
			
		$(".right")
			.css("margin-right", "-700px")
			.animate( { marginRight: "0px" }, 1500, "easeOutQuad" );
			
		$("aside li")
			.css("margin-top", "80px")
			.animate( { marginTop: "8px" }, 1550, "easeOutQuad");
			
	};
	
	$("#loading").hide();
	
}


// ROTATION OF CERTAIN ELEMENTS

function rotateRelated(maxRotation) {

	var randomNum = new Array();
	
	for (i=0; i<=10; i++) {
	
		randomNum[i] = Math.floor(Math.random()*((maxRotation*100)+1))/100;
		
		if (i%2) {
			randomNum[i] = randomNum[i]-maxRotation; // Alternates rotation direction every other row.
		}
		
		$("#related"+i)
			.css("transform", "rotate("+randomNum[i]+"deg)") // Doesn't exist yet, but someday, hopefully!
			.css("-webkit-transform", "rotate("+randomNum[i]+"deg)")
			.css("-moz-transform", "rotate("+randomNum[i]+"deg)") // Makes text look like shit, but maybe Mozilla will fix it someday, hopefully!
			.css("-o-transform", "rotate("+randomNum[i]+"deg)") // This probably doesn't exist.
			.css("-ie-transform", "rotate("+randomNum[i]+"deg)"); // You're joking, right?
			
	}
	
}



// ----------------------



$(document).ready(function() {


	// PROJECT SLIDESHOW (NEW)

	$('#gallery_content').cycle({
		prev: '#gallery_prev',
		next: '#gallery_next',
		timeout: 0,
		speed: 200
	});


	// HOMEPAGE SLIDESHOW
	
	$(".home #gallery").cycle({
		timeout: 6000,
		speed: 350,
		pause: 1
	});


	// WRITE CORRECT PAGE TITLE
	
	if ( $('body').hasClass('single') || $('body').hasClass('archive') || $('body').hasClass('home') ) {
	
		if ($('body').hasClass('home')) { var titleStr = 'Feed Your Brand\u2122'
		} else if ($('body').hasClass('postid-700')) { var titleStr = 'About'
		} else if ($('body').hasClass('postid-706')) { var titleStr = 'Clients'
		} else if ($('body').hasClass('postid-704')) { var titleStr = 'Contact'
		} else if ($('body').hasClass('postid-708')) { var titleStr = 'Map'
		} else var titleStr = $('h1').text();
		
		if ( ($.browser.msie) || ($.browser.mozilla && $.browser.version < 3.5) ) {
			document.title = 'Ham in the Fridge - ' + titleStr; // Some browsers can't handle our encoded characters.
		} else {
	    	$('title').html('Ham in the Fridge &nbsp;&middot;&nbsp;&nbsp;');
			var thru=0;
			var spelling = setInterval(function(){
				if (thru < titleStr.length) {
					if (titleStr[thru]==" ") {
						document.title += ' '+titleStr[++thru];
						thru++;
					}
					else document.title += titleStr[thru++];
				} else clearInterval(spelling);
			}, 40);
		};
    };
	
	
	// FIREFOX IS A P.O.S.
	
	if ($.browser.mozilla) $(".label").css("line-height", "20px"); // Firefox doesn't apply correct line-height to Cufoned elements...
	if ($.browser.mozilla && $.browser.version >= 3.0) $("#news .label").css("line-height", "21px" ); // ...and inexplicably does it even worse with these, but only in v3+!
	if ($.browser.mozilla) $(".connection_top").css("padding-bottom", "2px"); // It also doesn't correctly measure line-height here, causing vertical misalignment...
	if ($.browser.mozilla) $(".cta_top").css("padding-bottom", "1px"); // ...and, again.
	
	
	// HOVER EFFECTS FOR RELATED ITEMS & SINGLE-ITEM GALLERY LINK
	
	if ( !( (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) ) ) {
	
		$(".left > ul > li").hover( 
			function() {
			
				$(".related_inner", this).stop(true, true).animate( { marginLeft: "-=6px" }, 200 );
				
				if ($.browser.msie) {
					$(".connection", this).stop(true, true).animate( { marginLeft: "-=5px" }, { queue: false, duration: 350 } ).show();
				} else {
					$(".connection", this).stop(true, true).animate( { marginLeft: "-=5px" }, { queue: false, duration: 350 } ).fadeIn(350);
				};
				
			}, 
			function() {
			
				$(".related_inner", this).stop(true, true).animate( { marginLeft: "-2px" }, 200 );
				
				if ($.browser.msie) {
					$(".connection", this).stop(true, true).animate( { marginLeft: "+=5px" }, { queue: false, duration: 250 } ).hide();
				} else {
					$(".connection", this).stop(true, true).animate( { marginLeft: "+=5px" }, { queue: false, duration: 250 } ).fadeOut(150);
				};
				
			}
		);
		
		$(".right > ul > li").hover( 
			function() {
			
				$(".related_inner", this).stop(true, true).animate( { marginLeft: "+=6px" }, 200 );
				
				if ($.browser.msie) {
					$(".connection", this).stop(true, true).animate( { marginLeft: "+=5px" }, { queue: false, duration: 350 } ).show();
				} else {
					$(".connection", this).stop(true, true).animate( { marginLeft: "+=5px" }, { queue: false, duration: 350 } ).fadeIn(350);
				};
				
			}, 
			function() {
			
				$(".related_inner", this).stop(true, true).animate( { marginLeft: "-2px" }, 200 );
				
				if ($.browser.msie) {
					$(".connection", this).stop(true, true).animate( { marginLeft: "-=5px" }, { queue: false, duration: 250 } ).hide();
				} else {
					$(".connection", this).stop(true, true).animate( { marginLeft: "-=5px" }, { queue: false, duration: 250 } ).fadeOut(150);
				};
				
			}
		);
		
		$(".gallery_container").hover( 
			function() {
				
				if ($.browser.msie) {
					$(".gallery_overlay", this).stop(true, true).show();
				} else {
					$(".gallery_overlay", this).stop(true, true).fadeIn(250);
				};
				
			}, 
			function() {
				
				if ($.browser.msie) {
					$(".gallery_overlay", this).stop(true, true).hide();
				} else {
					$(".gallery_overlay", this).stop(true, true).fadeOut(250);
				};
				
			}
		);
	
	}
	
	
	// SLIDESHOW
	
	/*
	var currentPosition = 0;
	var slideWidth = 480;
	var slides = $('.slide');
	var numberOfSlides = slides.length;
	var currentGalleryDiv = '';

	slides.wrapAll('<div id="slide_inner"></div>');

	$('#slide_inner').css('width', slideWidth * numberOfSlides); // Set width equal to total width of all slides.

	manageControls(currentPosition); // Hide left arrow control on first load.

	$('.gallery_control')
		.bind('click', function(event){
			event.preventDefault();
			currentGalleryDiv = document.getElementById('gallery_media'+currentPosition);
			
			// Reset current video.
			if (!$('img#gallery_media'+currentPosition).length) {
				currentGalleryDiv.api_pause();
				currentGalleryDiv.api_seekTo(0);
			};
			
			currentPosition = ($(this).attr('id')=='gallery_next') ? currentPosition+1 : currentPosition-1;
			manageControls(currentPosition);
			$('#slide_inner').animate({
				'marginLeft' : slideWidth*(-currentPosition)
			});
		});

	function manageControls(position){
	
		if (position==0) {
			$('#gallery_prev').hide()
		} else {
			$('#gallery_prev').fadeIn()
		}
		
		if (position==numberOfSlides-1) {
			$('#gallery_next').hide()
		} else {
			$('#gallery_next').fadeIn()
		}
	};*/
	
	
	// TWITTER
	
	$('#twitter_btn').toggle(
		function() {
			$('#twitter').stop(true, true).slideDown(250);
			$(this).css('cursor', 'n-resize');
		},
		function() {
			$('#twitter').stop(true, true).slideUp(250);
			$(this).css('cursor', 's-resize');
		}
	);
	
	
	// ENDMARK
	
	$('<img class="endmark" src="/img/endmark.png" width="16" height="12" alt="" />').appendTo('.single #article #description > *:last'); // Adds a little flourish to the end of blog posts.
	
	
	// LOADING INDICATORS
	
	if ( !($.browser.msie && $.browser.version == '7.0') ) {
		$('aside li.large .related_inner > a img').wrap('<div class="loading" />');
		$('.archive #content li.large .related_inner > a img').wrap('<div class="loading" />');
		$('.search #content li.large .related_inner > a img').wrap('<div class="loading" />');
		$('.slide img').wrap('<div class="loading" />');
	}
	

	// DEFINE PRELOADED IMAGES
	
	$.preLoadImages(
		[
			"/img/gallery_overlay.png",
			"/img/gallery_single.png",
			"/img/gallery_multiple.png",
			"/img/gallery_prev.png",
			"/img/gallery_next.png",
			"/img/connection_left_btm.png",
			"/img/connection_left_top.png",
			"/img/connection_right_btm.png",
			"/img/connection_left_top.png",
			"/img/footer.png",
			"/img/cta_btm.png",
			"/img/cta_top.png",
			"/img/btn_lg_loading.gif",
			"/img/btn.png",
			"/img/btn_ham_lg.png",
			"/img/btn_ham_sm_client.png",
			"/img/btn_ham_sm.png",
			"/img/btn_lg_shadow.png",
			"/img/btn_lg.png",
			"/img/btn_sm_client.png",
			"/img/btn_sm_shadow.png",
			"/img/btn_sm.png",
			"/img/tag.png",
			"/img/article_top.png",
			"/img/article_btm.png",
			"/img/endmark.png",
			"/img/btn_twitter.png",
			"/img/hr.png"
		], function(){ // Callback
			$("aside").show();
			$("#main").show();
			if ( !( (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) ) ) {
				rotateRelated(1.6); // The number defines the maximum allowed degrees of rotation.
			}
			transitionIn();
		}
	);
	

});



// ----------------------



$(window).load(function() {

	$('.loading').css('background', 'none'); // Save some processor resources by removing the loading indicators when everything's ready, instead of just hiding them behind the loaded elements.
	
});
