(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

$(document).ready(function() {
	
	$("#logo").hover(function() { $("#logo .switcher").slideDown(); }, function() { $("#logo .switcher").slideUp(); });
	$("#logo .post").click(function(e) { e.preventDefault(); window.location='/home.php'; });
	$("#logo .ent").click(function(e) { e.preventDefault(); window.location='/home_entertainment.php'; });
	
	$("#search input").focus(function() {
		if ($(this).val() == $(this).attr('originalValue')) $(this).val('');
		$(this).removeClass('defaultValue');
	});
	$("#search input").blur(function() {
		if ($(this).val() == '') { $(this).val($(this).attr('originalValue')); $(this).addClass('defaultValue'); }
	});
	$("#search input").keyup(function(e) {
		if (e.keyCode == 13) { window.location = $(this).attr('page')+$(this).val(); }
	});
	
	if ($("#splash").length > 0) {
		setCookie('homepage','none',365);
		$.preLoadImages('/interface/full/images/splash/clickhere_over.png','/interface/full/images/splash/logo_entertainment.png','/interface/full/images/splash/logo_postprod.png','/interface/full/images/splash/entertainment_rollover.png','/interface/full/images/splash/postprod_rollover.png');
	}
	$("#splash .remember_label").click(function() { $("#splash .remember input").click(); });
	$("#splash .switcher").click(function() {
		if ($(this).attr('openItem') == 'yes') {
			$(this).css('backgroundImage','url(/interface/full/images/splash/clickhere.png)');
			$(this).children().hide();
			$(this).attr('openItem','no');
		} else {
			$(this).css('backgroundImage','url(/interface/full/images/splash/clickhere_over.png)');
			$(this).children().show();
			$(this).attr('openItem','yes');
		}
	});
	$("#splash .switcher .entertainment").hover(function() {
		$("#splash .logo").addClass('entertainment');
	}, function() { if (!$("#splash .logo").hasClass('selected')) $("#splash .logo").removeClass('entertainment'); });
	$("#splash .switcher .postprod").hover(function() {
		$("#splash .logo").addClass('postprod');
	}, function() { if (!$("#splash .logo").hasClass('selected')) $("#splash .logo").removeClass('postprod'); });
	$("#splash .switcher .postprod").click(function() { $("#splash .logo").addClass('selected'); $(this).parent().children().hide(); if ($("#remember").attr('checked') == 'checked') {setCookie('homepage','post',365);} window.location = 'home.php';  });
	$("#splash .switcher .entertainment").click(function() { $("#splash .logo").addClass('selected'); $(this).parent().children().hide(); if ($("#remember").attr('checked') == 'checked') {setCookie('homepage','entertainment',365);} window.location = 'home_entertainment.php';  });
	
	$("a").click(function() { if ($(this).attr('href') == '#top') {return(!$('html, body').animate({ scrollTop: 0 }, 1000));} });
	
	$("#showcase .title").css('backgroundImage','url('+ $($("#viewport img")[0]).attr('titleImg')  +')');
	if ($('#viewport').length > 0) $('#viewport').carousel('#previous', '#next', function() { $("#showcase .title").hide('fast'); }, function() {
		var num = $("#viewport ul").attr('elementIndex');
		var url = $($("#viewport img")[num]).attr('titleImg');
		$("#showcase .title").css('backgroundImage','url('+ url  +')');
		$("#showcase .title").show('fast');
	});
	
	$(".overlayBox").each(function() {
		$(this).fancybox({'transitionIn':'elastic','transitionOut':'elastic','overlayColor':'#000'});
	});
	$(".showReelBox").each(function() {
		$(this).fancybox({'transitionIn':'elastic','transitionOut':'elastic','overlayColor':'#000', content: $('#showreel_content').html()});
	});
	$('#showreel_content').remove();//dont load vimeo player unless wanted! slows down site

	if ($('#viewport li').length > 0) {
		$("#viewport li img").each(function() { $.preLoadImages($(this).attr('titleImg')); });
	}
	
	$(".showreel .play, .showreel .image, .showreel-main .play, .showreel-main .image").hover(function(){$(this).parent().find(".play").stop().animate({opacity:1})},function(){$(this).parent().find(".play").stop().animate({opacity:0.4})});	
	$(".showreel .play, .showreel-main .play").click(function() { $(this).siblings(".image").children("a").click(); });

	// Hover effect credit: http://www.sohtanaka.com/web-design/greyscale-hover-effect-w-css-jquery/
	$("#menu li").each(function() {
		var thumbOver = $(this).find("img").attr("src");
		$(this).find("a").css({'background-image':'url('+thumbOver+')'});
	});
	
	$("#menu li").hover(function() {
		if ($(this).find("a").hasClass('active')) return;
		var thumbOver = $(this).find("img").attr("src");
		$(this).find("a").css({'background' : 'url(' + thumbOver + ') 0px 114px'});
		$(this).find("span").hide();
		var obj = $(this);
		} , function() { if ($(this).find("a").hasClass('active')) return; $(this).find("span").show();
		$(this).find("a").css('backgroundPosition','0px 0px');  });
		
/*	$("#menu li").hover(function() {
		if ($(this).find("a").hasClass('active')) return;
		var thumbOver = $(this).find("img").attr("src");
		var obj = $(this);
		$(this).find("a").css({'background' : 'url(' + thumbOver + ') 0px 114px'});
		$(this).find("span").stop(false,true).fadeTo('fast', 0 , function() { $(this).hide(); }); } , function() { if ($(this).find("a").hasClass('active')) return; $(this).find("span").stop(false,true).fadeTo('fast', 1).show('fast', function() { $(this).parent().css('background','');  });  });*/
		
	$(".hovers li").hover(function() {
		var thumbOver = $(this).find("img").attr("src");
		$(this).find("a.thumb").css({'background' : 'url(' + thumbOver + ') no-repeat center bottom'});
		$(this).find("span").stop().fadeTo('normal', 0 , function() { $(this).hide(); });
		$(this).parent().parent().siblings(".projects").children().hide();
		$("#newproj-"+ $(this).children("a").attr('rel')).show();
		}, function() { $(this).find("span").stop().fadeTo('fast', 1).show(); });
	$(".hovers li").click(function() { $(this).find('a').click(); });

	$(".highlights li").live({mouseover: function() {
		var thumbOver = $(this).find("img").attr("src");
		$(this).find("a.thumb").css({'background' : 'url(' + thumbOver + ') no-repeat center right'});
		$(this).find("a.thumb").css('height', $(this).find("img").css('height'));
		$(this).find("span").stop().fadeTo('normal', 0 , function() { $(this).hide(); }); } ,
		mouseout: function() { $(this).find("span").stop().fadeTo('fast', 1).show(); } });

	$("#workpage .project .files").live({mouseover: function() {
		$(this).find(".image").stop().fadeTo('normal', 0 ); $(this).children(".list").slideDown(); } ,
		mouseout: function() { $(this).find(".image").stop().fadeTo('fast', 1).show(); $(this).children(".list").slideUp(); } });
		
	$("#workpage .project .files li").live('click', function() { $(this).children('a').click(); });
		
	$("#blogpage .entry .readmore").live('click',function() {
		var obj = $(this).parent().parent().siblings(".text").children(".excerpt");
		window.setTimeout(function() { obj.toggle(); }, 350);
		$(this).parent().parent().siblings(".text").children(".full").slideToggle('slow');
	});
	
	$("#searchpage .filterHolder a").live('click', function() {
		h = $(this).attr('href'); if (h == '#all') { $("#searchresults").children().slideDown(); return false; }
		$("#searchresults").children(":not("+h+", .nr)").slideUp();
		$("#searchresults "+h).slideDown();		
		return false;
	});
	
	$("#blogpage .filterHolder a").live('click', function() {
		$('body').css('cursor','wait');
		$.get('/ajax/getBlogs.php', 'key='+$(this).attr('key')+'&value='+$(this).attr('val'), function(data) { $("#blogpage .entries").html(data); $('body').css('cursor','default'); $('html, body').animate({ scrollTop: 0 }, 1000); });
		return false;
	});
	
	$("#blogpage .navigation div").live('click', function() {
		$('body').css('cursor','wait');
		$.get('/ajax/getBlogs.php', 'page='+$(this).attr('page')+'&key='+$(this).attr('key')+'&value='+$(this).attr('val'), function(data) { $("#blogpage .entries").html(data); $('body').css('cursor','default'); $('html, body').animate({ scrollTop: 0 }, 1000); });
		return false;
	});
	
	$("#blogpage #commentform").submit(function() {
		$("#commentform_div").slideUp();
		$.post('/ajax/blog_action.php', $(this).serialize(), function(data) {
			if (data.error === true) {
				$("#commentform_msg").html(data.errorMessage);
				$("#commentform_div").slideDown();
				$('html, body').animate({ scrollTop: $("#commentform_div").offset().top+100 }, 1000);
				return;
			}
			$("#commentform_success").show();		
		}, 'json');
		return false;
	});
	
	$("#workpage .entryline .entry .image").hover(function() {
		$(this).children("div").fadeIn('slow'); $(this).siblings(".title").fadeIn('slow');
	},  function() {
		$(this).children("div").fadeOut('fast'); $(this).siblings(".title").fadeOut('fast'); }
	);
	
	$("#workpage #viewer").click(function() { $(this).children(".tagline").hide('fast'); } );
	$("#workpage #viewer").children().click(function() { $(this).parent().click() });
	if ($("#photosCarousel").length > 0) {
		$('#photosCarousel').carousel('#previous', '#next', $.noop, $.noop);
	}
	
	$("#workpage.tv .reel .video").click(function() {
		if ($("#mainVideo").length == 1) $.fancybox({'transitionIn'	: 'elastic','transitionOut'	: 'elastic','overlayColor':'#000', href:'#mainVideo' });
		else showLogin();
	});
	$("#workpage.tv .reel .photos a").click(function() {
		if ($(this).attr('href') == '#loginRequired') {
			showLogin();
			return false;
		}
		if ($(this).attr('ref') == 'image') {
			$.fancybox({'transitionIn'	: 'elastic','transitionOut'	: 'elastic','overlayColor':'#000', content:"<img src='"+ $(this).attr('href') + "' onload='showViewerPhoto()' alt='' />" });
		} else {
			$.fancybox({'transitionIn'	: 'elastic','transitionOut'	: 'elastic','overlayColor':'#000', content:"<div id='mediaspace' style='width:846px;height:480px'></div>" });
			jwplayer('mediaspace').setup({
			    'flashplayer': '/js/mediaplayer/player.swf',
			    'file': $(this).attr('href'),
			    'controlbar': 'bottom',
			    'fullscreen': 'true',
			    'stretching': 'uniform',
			    'width': '846',
			    'height': '480', 'autostart':'true',
			    'plugins': {
				       'hd-1': {
				           'file': $(this).attr('hd')
				       }
				    }
			});
		}
		return false;
	});
	
	$("#workpage.post .reel .photos a").click(function() {
		if ($(this).attr('href') == '#loginRequired') {
			$("#workpage #viewer .login").fadeIn();
			return false;
		}
		$("#workpage #viewer .video").fadeOut();
		if ($(this).attr('ref') == 'image') {
			$("#workpage #viewer .photo").html("<img src='"+ $(this).attr('href') + "' onload='showViewerPhoto()' alt='' />");
			$("#workpage #viewer .photo img").css({'visibility':'hidden'});
		} else {
			$("#workpage #viewer .photo").html("<div id='mediaspace'></div>");	
			jwplayer('mediaspace').setup({
			    'flashplayer': '/js/mediaplayer/player.swf',
			    'file': $(this).attr('href'),
			    'controlbar': 'bottom',
			    'fullscreen': 'true',
			    'stretching': 'uniform',
			    'width': '846',
			    'height': '480', 'autostart':'true',
			    'plugins': {
				       'hd-1': {
				           'file': $(this).attr('hd')
				       }
				    }
			});
		}		
		$("#workpage #viewer .photo").fadeIn();
		$('html, body').animate({ scrollTop: $("#workpage #viewer").offset().top-20 }, 1000);
		return false;
	});
	$("#workpage.post .reel .photos .entry").hover(function() { $(this).children(".itemtitle, .itemtitlebg").fadeIn(); }, function() { $(this).children(".itemtitle, .itemtitlebg").fadeOut(); }).click(function() { $(this).children("a").click(); });
	$("#workpage.tv .reel .photos .entry, #workpage.tv .reel .video").hover(function() { $(this).children(".itemtitlebg").fadeIn(); }, function() { $(this).children(".itemtitlebg").fadeOut(); }).click(function() { $(this).children("a").click(); });
	
	$("#workpage .reel .photos a").each(function() { if ($(this).attr('rel') == 'image') $.preLoadImages($(this).attr('href')); });
	$("#workpage.post .reel .video").click(function() {
		$("#workpage #viewer .photo, #workpage #viewer .login").fadeOut(); $("#workpage #viewer .video").fadeIn();
		$('html, body').animate({ scrollTop: $("#workpage #viewer").offset().top-20 }, 1000);
	});
	$("#workpage .entry .image .highlight, #workpage .entry .image .text").live('click',function() {
		h=$(this).siblings('a').attr('href'); t=$(this).siblings('a').attr('title');
		if ($(this).siblings('a').attr('rel') == 'local') {
			window.location = h;
			return;
		}
		if (h.substr(0,5)!='vimeo') {
			k='<iframe src="/viewVideo.php?url='+encodeURI(h)+'&urlhd='+encodeURI($(this).siblings('a').attr('hd'))+'" width="846" height="480" frameborder="0"></iframe><br /><h2>'+t+'</h2>';
		} else {
		vi=h.substring(6, h.length); vi=vi.split('/'); v=vi[0];w=vi[1];h=vi[2];
		k='<iframe src="http://player.vimeo.com/video/'+v+'?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=0" width="'+w+'" height="'+h+'" frameborder="0"></iframe><br /><h2>'+t+'</h2>';
		}
		$.fancybox({'transitionIn'	: 'elastic','transitionOut'	: 'elastic','overlayColor':'#000', content:k});
		return false;
	});
	$("#results-work a").live('click',function() {
		h=$(this).attr('href'); if (h.substr(0,5)!='vimeo') return;
		vi=h.substring(6, h.length); vi=vi.split('/'); v=vi[0];w=vi[1];h=vi[2];t=$(this).attr('title');
		k='<iframe src="http://player.vimeo.com/video/'+v+'?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=0" width="'+w+'" height="'+h+'" frameborder="0"></iframe><br /><h2>'+t+'</h2>';
		$.fancybox({'transitionIn'	: 'elastic','transitionOut'	: 'elastic','overlayColor':'#000', content:k});
		return false;
	});
	$("#workpage .entry .image .view").live('click',function() {window.location = $(this).children('a').attr('href'); });
	
	if ($("#workpage.main").length == 1 && window.location.hash.substring(1).length > 0) {
		var id = window.location.hash.substring(1);
		var obj = $("#workpage .entry[entryId='"+id+"'] .image a");
		if (obj.length == 1) $("#workpage .entry[entryId='"+id+"'] .highlight").click();
	}
	
	$(".genericform").submit(function() {
		$(this).css({visibility:'hidden'});
		$("#loading").show();
		var obj = $(this);
		$.post($(this).attr('action'), $(this).serialize(), function(data) {
			$("#loading").hide();
			if (data.done === false) { obj.css({visibility:'visible'}); $("#error").html(data.errorMessage); }
			else { $("#success").show(); if (data.js) eval(data.js); }
		}, 'json');
		return false;
	});
	
	$("#loading").ajaxSend(function() { $(this).show(); } );
	$("#loading").ajaxStop(function() { $(this).hide(); } );
	
});

function moveHighlights() {
	obj = $("#showcase .highlights");
	elem = obj.find("li:eq(1)");
	if (elem.html() != '') obj.children("ul").append('<li>'+elem.html()+'</li>');
	elem.slideUp(400, function() { elem.remove(); window.setTimeout(function() { moveHighlights(); }, 4000); });
}

function showViewerPhoto() {
	$("#workpage #viewer .photo img").hide(); $("#workpage #viewer .photo img").css({'visibility':'visible'});
	$("#workpage #viewer .photo img").fadeIn();
}

function showLogin() {
	$('#login').modal({
		closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
		position: ["20%",],
		minHeight: 300,
		overlayId: 'login-overlay',
		containerId: 'login-container', 
		onShow: function (dialog) {
			var modal = this;
			$('.message', dialog.data[0]).append(message);
		}
	});
}

$(window).load(function() {
	$("#showcase, #menu").css({'visibility':'visible'});
	if ($("#showcase .highlights").length == 1) {
		$("#showcase .highlights").scrollTop(65);
		window.setTimeout(function() { moveHighlights(); }, 4000);
	}
	if ($('#viewport li').length > 0) {
		window.setInterval(function() { $("#next").click(); }, 8000);
	}
});
