var e
var e2
var ready = true
var map_working
var myWidth
var myHeight
var clear
rollovers = ["RolloverG2","RolloverA","RolloverA2","RolloverBb","RolloverBb2","RolloverC","RolloverC1","RolloverC2","RolloverD","RolloverD1","RolloverD3","RolloverEb","RolloverEb2","RolloverF","RolloverF2","RolloverG"]
othersounds = ["ClickOpen","ClickClose","LightOff","LightOn","ScrollLeft","ScrollRight"]

var requiem = {
	src: 'swf/requiem.swf'
	,ratios: [7, 1.32, 11, 1.31, 13, 1.24, 14, 1.25, 19, 1.23, 27, 1.2, 34, 1.19, 42, 1.18, 47, 1.17, 48, 1.18, 69, 1.17, 74, 1.16, 75, 1.17, 1.16]
};
sIFR.activate(requiem);

soundManager.onload = function() {
	//Prepare Audio
	$.each(othersounds, function( intIndex, item ){
		window[item] = soundManager.createSound({
			id: item+"_load",
			url: 'sounds/'+item+'.mp3',
			volume: 0
		});
		window[item].play()
		window[item] = soundManager.createSound({
			id: item,
			url: 'sounds/'+item+'.mp3',
			volume: 40
		});
	});
	$.each(rollovers, function( intIndex, item ){
		window[item] = soundManager.createSound({
			id: item+"_load",
			url: 'sounds/'+item+'.mp3',
			volume: 0
		});
		window[item].play()
		window[item] = soundManager.createSound({
			id: item,
			url: 'sounds/'+item+'.mp3',
			volume: 30
		});
	});
}

function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=430,height=360,left = 745,top = 360');");
}

function randomSound(){
	var soun = rollovers[Math.floor(Math.random()*rollovers.length)]
	window[soun].play()	
}
function next(){	
	if (ready){
		window["ScrollRight"].play()
		ready = false;
		$("#top").animate({ marginLeft: "-300px" }, 800,
		function(){
			e = $("#top li:first").clone()
			$("#top li:first").remove()
			$("#top").css("marginLeft","-150px")
			$("#top").append(e)

		});
		var me = $("#middle li:first").clone()
		var me2 = $("#middle li:first");

		$("#middle").animate({ marginLeft: "-300px" }, 800,
		function(){		
			me2.remove()
			$("#middle").append(me)
			$("#middle").css("marginLeft","-150px")
		});
		$("#middle li.next_twice img").animate({ marginLeft: "-150px" }, 800,
		function(){			
			update_twice_next()
		});
		$("#bottom").animate({ marginLeft: "-300px" }, 800,
		function(){
			e = $("#bottom li:first").clone()
			$("#bottom li:first").remove()
			$("#bottom").css("marginLeft","-150px")
			$("#bottom").append(e)
		});
	}
}
function update_twice_next(){
	$("#middle li.next_twice img").css("marginLeft","0px")
	$("#middle li.next_twice").removeClass("next_twice")
	$("#middle li:eq(3)").addClass("next_twice")
	ready=true
}
function previous(){
	if (ready){
		window["ScrollLeft"].play()
		ready = false;
		$("#top").animate({ marginLeft: "0px" }, 800,
		function(){		
			$("#top").prepend($("#top li:last").clone())
			$("#top li:last").remove()
			$("#top").css("marginLeft","-150px")
		});
		$("#middle").animate({ marginLeft: "0px" }, 800,
		function(){	
			$("#middle").prepend($("#middle li:last").clone())
			$("#middle li:last").remove()
			$("#middle").css("marginLeft","-150px")
		});
		$("#middle li:eq(2) img").animate({ marginLeft: "150px" }, 800,
		function(){
			$("#middle li.next_twice").removeClass("next_twice")
			$("#middle li:eq(3)").addClass("next_twice")
			$("#middle li.next_twice img").css("marginLeft", "0px")
			ready = true;
		});
		$("#bottom").animate({ marginLeft: "0px" }, 800,
		function(){		
			e = $("#bottom li:last").clone()
			$("#bottom li:last").remove()
			$("#bottom").css("marginLeft","-150px")		
			$("#bottom").prepend(e)
		});
	}

}

$(document).ready(function() {

	jQuery.each($(".work_row li img:not(.audio)"), function() {
		$.cacheImage($(this).attr("src").replace("_bw", ""));
	});	 
	jQuery.each($(".work_row li img.audio"), function() {
		$.cacheImage($(this).attr("src").replace("_bw", "_play"));
	});
	jQuery.each($(".work_row li img.audio"), function() {
		$.cacheImage($(this).attr("src").replace("_bw", "_pause"));
	});
	$.cacheImage([
	    '/images/navigation/about_over.gif',
	    '/images/navigation/services_over.gif',
		'/images/navigation/news_over.gif',
		'/images/navigation/studios_over.gif',
		'/images/navigation/contact_over.gif',
		'/images/navigation/client_over.gif',
		'/images/lightboxbg.png',
		'/images/overlay_bg2.png',
		'/images/designedby_over.gif',
		'/images/developedby_over.gif'
	  ])

	
	$(".work_row li img:not(.light):not(.audio)").livequery("mouseover", function(){
		$(this).addClass("over")
		$(this).attr("src", $(this).attr("src").replace("_bw", ""));
		randomSound()
	});
	$(".work_row li img:not(.light):not(.audio)").livequery("mouseout", function(){
		$(this).removeClass("over")
		$(this).attr("src", $(this).attr("src").replace("thumbs/", "thumbs_bw/"));
	})
	$(".work_row li img.audio").livequery("mouseover", function(){
		$(this).addClass("over")
		$(this).attr("src", $(this).attr("src").replace("_bw", "_play"));
		randomSound()
	});
	$(".work_row li img.audio").livequery("mouseout", function(){
		$(this).removeClass("over")
		$(this).attr("src", $(this).attr("src").replace("_play", "_bw"));
	})
	$(".work_row li img:not(.light)").livequery("mouseover", function(){
		if ($(this).hasClass("audio")){
			$("#prev_title").html("<strong>AUDIO</strong> - "+this.alt)
			$("#prev_title").fadeIn("fast")
		}else{
			$("#prev_title").html("<strong>VIDEO</strong> - "+this.alt)
			$("#prev_title").fadeIn("fast")
		}
	})
	$(".work_row li img").livequery("mouseout", function(){
		$("#prev_title").hide()
	})
		
	$('#inner_next').click(function(){
		show_next_work(next_work)
	});
	$('#inner_previous').click(function(){
		show_next_work(prev_work)
	});	
	
	$(".arrow_over").mouseover(function(){
		$(this).attr("src", $(this).attr("src").replace("_arrow", "_arrow_over"));
	})
	$(".arrow_over").mouseout(function(){
		$(this).attr("src", $(this).attr("src").replace("_arrow_over", "_arrow"));
	})
	$("#next").click(function(){
		next()
	})
	$("#previous").click(function(){
		previous()
		
	})
	$("#studio_inner_previous").click(function(){
		changeImage("next")
		
	})
	$("#login").click(function(){
		$("#login").hide()
		$("#login_load").show()
		var user = $("#username").val();
		var pass = $("#password").val();
		$.ajax({
		   type: "POST",
		   url: "client.php",
		   data: "username="+user+"&password="+pass,
		   success: function(msg){
		    if (msg=="password"){
				alert('Incorrect Password')
				$("#login").show()
				$("#login_load").hide()
			}else{
				if ($("#client_login_title2").hasClass('sIFR-replaced')){
					$("#client_login_title2").removeClass('sIFR-replaced')
				
				}
				
				$("#client_login_title2").html("Welcome, "+user)
				
				$("#client_login_content .cont").html("<p>Please right click on the file and select save as (or option click):</p>"+msg)
				
				$("#client_login_content").fadeIn(function(){
					
								sIFR.replace(requiem, {
									selector: "#client_login_title2"
									,css: {
										'.sIFR-root': { 'color': '#c89071', 'text-align': 'left', 'letter-spacing': 0 }
										},wmode: 'transparent'
									});
									$("#client_login").fadeOut(function(){
										$("#login").show()
										$("#login_load").hide()
										
									});
									
				});


			}
		   }
		 });
		
	})
	$("#studio_inner_next").click(function(){
		changeImage("previous")
	})
	$("#getdirections").click(function(){
		popUp("/directions.php?address="+$("#dirAddress").val())
		
	})
	$("#close").click(function(){
		$(".ref_block").show();
		
		$(".menu_item").fadeOut("fast")
		$('#movie').html("")
		window["ClickClose"].play()
		overlay()
	})
	$(".work_row li img:not(.light):not(.audio)").livequery("click",function(){
		stop_playing(currently_playing)
		
		window["ClickOpen"].play()
		display_work(this.id.replace(/t_/,""))
	})
	$(".work_row li img.audio").livequery("click",function(){
		play_audio(this.id)
	})
	
	
	
	$(".nav li a").click(function(){
		this.blur()
		window["ClickOpen"].play()
		overlay(true)
		area = this.id.split("-")[1]
		setTimeout("show('"+area+"')",200)
	})

	$(".work_row li img.light").livequery("click", function(){
		if ($(this).hasClass("on")){
			$(this).attr("src", $(this).attr("src").replace("thumbs/", "thumbs_bw/"))
			$(this).removeClass("on")
			window["LightOff"].play()
		}else{
			$(this).attr("src", $(this).attr("src").replace("_bw", ""))
			$(this).addClass("on")
			window["LightOn"].play()
		}
	}); 
	get_screen_dimensions()
	window.moveTo(0,0);
	window.resizeTo(screen.width,screen.height);
	
	$('#silencio').css("marginTop",Math.round((myHeight-689)/2)+'px')
	$('#lightbox').css("top",Math.round((myHeight-545)/2)+'px')
	setTimeout("loaded()",15000)
});
function loaded(){
	
		$("#website_loading").fadeOut()
}
function overlay(show){
	get_screen_dimensions();
	if (show){
		$('#next').fadeOut("fast")
		$('#previous').fadeOut("fast")
		loading(true);
		$('#overlay').css("height", myHeight+'px')
		$('#overlay').fadeIn("fast")
		$('#lightbox').css("left", ((myWidth-974)/2)+'px')
		$('#lightbox').fadeIn("fast")
		$('#cover').hide();
	}else{
		$('#next').fadeIn("fast")
		$('#previous').fadeIn("fast")
		$('#lightbox').fadeOut("fast", function(){
			$('#overlay').hide();
		})
		$('#cover').show();
	}

}

function show(area){
	$(".ref_block").hide();
	$('#content').hide()
	$('#'+area).show()
	if (!$('#'+area+'_title').hasClass('sIFR-replaced')){

		
		sIFR.replace(requiem, {
			selector: "#"+area+"_title"
			,css: {
				'.sIFR-root': { 'color': '#c89071', 'text-align': 'left', 'letter-spacing': 0 }
				},wmode: 'transparent'

			});


				if (area!="contact" && area!="client_login"){
					sIFR.replace(requiem, {
						selector: "#"+area+" p"
						,css: {
							'.sIFR-root': { 'color': '#c89071', 'text-align': 'left', 'letter-spacing': 1 },
							'a:link': { 'color': '#ffffff' }
							
							},wmode: 'transparent'
						});
					}

					if (area=="client_login"){
						sIFR.replace(requiem, {
							selector: "#"+area+" p"
							,css: {
								'.sIFR-root': { 'color': '#c89071', 'text-align': 'center', 'letter-spacing': 1 },
								'a:link': { 'color': '#ffffff' },
								'a:hover': { 'color': '#ffffff' }
								}, wmode: 'transparent'
							});
						}
					}

					if (area=="contact" && !map_working){
						sIFR.replace(requiem, {
							selector: "#"+area+" p"
							,css: {
								'.sIFR-root': { 'color': '#c89071', 'text-align': 'left', 'letter-spacing': 1 },
								'a:link': { 'color': '#ffffff', 'text-decoration':'none' },
								'a:hover': { 'color': '#ffffff', 'cursor': 'pointer', 'text-decoration':'underline' }
								},wmode: 'transparent'
							});
							map_working=true;
							initialize()
						}
						loading()
					}

					function loading(show){
						show ? $('#loading').show() : $('#loading').fadeOut("fast")
					}

					function get_screen_dimensions(){
						if( typeof( window.innerWidth ) == 'number' ) {
							myWidth = window.innerWidth;
							myHeight = window.innerHeight;
						}else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
							myWidth = document.documentElement.clientWidth;
							myHeight = document.documentElement.clientHeight;
						}else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
							myWidth = document.body.clientWidth;
							myHeight = document.body.clientHeight;
						}

					}
					function display_work(id){
						$(".ref_block").hide();
						
						$("#content").show()
					overlay(true)
					update_work(id)
				  }
				function stop_quicktime(){
					$('#movie').html("");
					
				}
				var work_info;
				var next_work;
				var prev_work;
				
				function update_work(id){
					stop_quicktime()
				
				 $.ajax({
				   type: "POST",
				   url: 'get_work.php?id='+id,
					dataType: 'json',
				   success: function(msg){
					work_info = msg;
					work_info.next_id=="" ? next_work = 1 : next_work = work_info.next_id 
					prev_work = work_info.prev_id
					fill_work()
					sifr_work()
					play_quicktime("media/"+id+".mov")	
					setTimeout("loading()",100)				
				   }
				 });				
				}
				function play_quicktime(movie){
					var quicktimeCode = '<object width="512" height="288" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab"><param name="src" value="'+movie+'"><param name="bgcolor" value="000000"><param name="autoplay" value="true"><param name="controller" value="true"><param name="scale" value="aspect"><embed src="'+movie+'" width="512" height="288" autoplay="true" controller="true" pluginspage="http://www.apple.com/quicktime/download/" bgcolor="000000" scale="aspect"></embed></object>';
					$('#movie').html(quicktimeCode);				
				}
				function fill_work(){
					$('#all_info').html(work_info.agency+work_info.post_production+work_info.producer+work_info.director+work_info.details+work_info.silencio_work)
					$('#title').html(work_info.title)		  
				}
				function show_next_work(id){
				 	$('#loading').fadeIn("fast")
				 	 update_work(id)				
				}
				function sifr_work(){
					$("#content .sIFR-replaced").removeClass('sIFR-replaced')
							sIFR.replace(requiem, {
							  selector: "#title"
							  ,css: {
							    '.sIFR-root': { 'color': '#c89071', 'text-align': 'left', 'letter-spacing': -1 },'a': { 'text-decoration': 'none', 'color': '#ffffff' },
								'a:link': { 'color': '#ffffff' }
							  }, wmode: 'transparent'
							});	
							sIFR.replace(requiem, {
							  selector: "#all_info"
							  ,css: {
							    '.sIFR-root': { 'color': '#c89071', 'text-align': 'left', 'letter-spacing': 1 }, 'a': { 'color': '#ffffff', 'text-decoration': 'none' }, 'a-hover': { 'color': '#ffffff' }
							  },wmode: 'transparent'

							});
					
				}
				var upto=1;
				function changeImage(d){
						$("#large_1").hide()
						$("#large_2").hide()
						$("#large_3").hide()
						$("#large_4").hide()
						$("#large_5").hide()
						$("#large_6").hide()

						if (d=="next"){
							upto++;

							if (upto==7){
								upto=1;
							}
								$("#large_"+upto).fadeIn("fast")
						}else{
							upto--;

							if (upto==0){
								upto=6;
							}
								$("#large_"+upto).fadeIn("fast")
						}
					
				}
				var currently_playing = false;
				var el;
				
				function play_audio(id){
					el = $("#"+id)	
					if (currently_playing==id){	
						stop_playing(id, true)
						currently_playing=false;
					}else{
						if (currently_playing){
							stop_playing(currently_playing, false)
						}
						
						start_playing(id)
					}
				}
				
				function stop_playing(id, iscurrent){
					if (id){
					if (!iscurrent){
						$("#"+id).attr("src",$("#"+id).attr("src").replace(/thumbs_pause/,"thumbs_bw"))
					}else{						
						$("#"+id).attr("src",$("#"+id).attr("src").replace(/thumbs_pause/,"thumbs_play"))
					}
					window['audio_'+id].stop()
					$("#"+id).removeClass('now_playing')
					}
				}
				function strip_id(id){
					return id.replace(/t_/,"")
				}
				function start_playing(id){
	
					$("#"+id).attr("src",$("#"+id).attr("src").replace(/thumbs_play/,"thumbs_pause"))
				
					
					window['audio_'+id] = soundManager.createSound({
						id: 'audio_'+id,
						url: 'media/'+strip_id(id)+'.mp3',
						volume: 80,
						onfinish: function(){ stop_playing(id,false) }
					});
					window['audio_'+id].play()
					currently_playing=id;
					$("#"+id).addClass('now_playing')				
					
				}