var current_image = 1;
var secondary_page = 0;
$(function() {
	$('div.geek').hide();
	
	// countdown 
	var newYear = new Date("February 12, 2010 13:00:00 -0500");
	var video_playing = false;
	
	Cufon.replace('ul.subheader li');
	Cufon.replace('h1.special');
	Cufon.replace('div.IXbody.secondary .right_column h2');
	Cufon.replace('div.IXbody.secondary .right_column p a.featured span.blue_title');
	Cufon.replace('div.IXbody.secondary.bio h2 span');
	Cufon.replace('ul.works li .section h2');
	Cufon.replace('div.IXbody.secondary p.highlighted');
	Cufon.replace('div.IXbody.secondary .block h2.special');
	Cufon.replace('div.IXbody.secondary.contact .block ul li a');
	Cufon.replace('div.IXbody.secondary.contact .block ul li');
	Cufon.replace('.right_column p.featured_text span.highlight');
	Cufon.replace('.right_column p.featured_action');
	Cufon.replace('.stream_box span.text');
	Cufon.replace('.zone');
	Cufon.replace('h2.highlighted span.title');
	
	try {
		Cufon.replace('div.IXbody.secondary.featured div#defaultCountdown');
	}	catch(err) {
		
	}
	
	/*
	$('.IXsplash div.countdown_msg').click(function() {
		loadFeatured();
	});	
	
	$('div.countdown_msg').hover(
		function() {
			$(this).addClass('on');
		},
		function() {
			$(this).removeClass('on');
		}
	);
	
	$('.IXsplash div#defaultCountdown').click(function() {
		loadFeatured();
	});
	*/
	
	try {
		Cufon.replace('div#defaultCountdown span'); 
		Cufon.replace('div.countdown_msg');
		Cufon.replace('ul.content_info h3');
		Cufon.replace('div.thoughts_matter span');
		// addTrailer();
		/*
		$('#defaultCountdown').countdown({until: newYear}); 

		$('#removeCountdown').toggle(function() { 
	    $('#defaultCountdown').countdown('destroy'); 
		}, 
		function() { 
		  $('#defaultCountdown').countdown({until: newYear}); 
		});
		*/
		$('div#trailer img#view_' + current_image).css({ left: '0px' });
		$('img#image_text_1').animate({ right: '635px' }, 500, function() { });
		$('ul.viewing_buttons li#button_view_' + current_image).addClass('selected');
		loopHomeImage();
	} catch(err) { }	
	
	Cufon.replace('div#IXheader li.top a');
	
	$('div.IXlogoTop').click(function() {
		document.location.href = '/';
	});
	
	$('a.view_photos').hover(
		function() {
			$(this).css({ backgroundPosition: '-93px 0' });
		},
		function() {
			$(this).css({ backgroundPosition: '0 0' });
		}
	);
	
	$('ul.viewing_buttons li#view_1').addClass('selected');
	$('ul.viewing_buttons li#view_1').click();
	$('ul.viewing_buttons li').hover(
		function() {
			$(this).addClass('selected');
		},
		function() {
			if(current_image != $(this).attr('value') || video_playing == true) {
				$(this).removeClass('selected');
			}
		}
	);

	
	$('div#video_close').hover(
		function() {
			$(this).addClass('on');
		},
		function() {
			$(this).removeClass('on');
		}
	);
	
	$('#trailer object').hide();

	$('div#video_close').click(function() {
		resetHomeImages();
		$('ul.viewing_buttons li:first').click();
	});
	
	$('div.view_trailer_button').hover(
		function() {
			$('div.view_trailer').addClass('viewing');
		},
		function() {
			$('div.view_trailer').removeClass('viewing');
		}
	);
	
	$('img#image_text_1').click(function() {
		$('div.view_trailer_button').click();
	});
	
	$('img#image_text_3').click(function() {
		document.location.href = "/featured_project";
	});
	
	$('img#image_text_2').click(function() {
		document.location.href = "/working_with_us";
	});
	
	$('img#image_text_1').hover(
		function() {
			$(this).attr("src","/images/image_text_1_over.png");
		},
		function() {
			$(this).attr("src", "/images/image_text_1.png");
		}
	);
	
	$('img#image_text_2').hover(
		function() {
			$(this).attr("src","/images/image_text_2_over.png");
		},
		function() {
			$(this).attr("src", "/images/image_text_2.png");
		}
	);
	
	$('img#image_text_3').hover(
		function() {
			$(this).attr("src","/images/image_text_3_over.png");
		},
		function() {
			$(this).attr("src", "/images/image_text_3.png");
		}
	);
	
	$('ul.viewing_buttons li').click(function() {
		$('div#video_close').hide();
		$('div.thoughts_matter').slideDown('slow');
		$('ul.viewing_buttons li').removeClass('selected');
		$('ul.viewing_buttons').stopTime();
		$('div.thoughts_matter span').fadeIn(1500);
		
		loopHomeImage();
		$('div.view_trailer').show();
		$('div.countdown_msg').show();
		$('div#defaultCountdown').show();
		if(current_image != $(this).attr('value')) {
			if(video_playing == true) {
				$('div#trailer_video').animate({ left: '-947px' }, 500, function() {
					$(this).appendTo('div#trailer_wrapper');
					$(this).css({ left: '947px' });
					removeTrailer();
				});
				video_playing = false;
			} else {
				$('div#trailer img#view_' + current_image).animate({ left: '-947px' }, 500, function() {
					$(this).appendTo('div#trailer_wrapper');
					$(this).css({ left: '947px' });
				});
			}
			
			current_image = $(this).attr('value');
			$('div#trailer img#view_' + current_image).animate({ left: '0px' }, 500, function() {
				$(this).appendTo('div#trailer_wrapper');
			});
		
		}
		
		resetHomeImages();
		$(this).addClass('selected');

		setImage();
	});
	
	$('div#IXheader ul.IXneurons li ul').hide();
	
	$('div#IXheader ul.IXneurons li.top').hover(
		function() {
		//	if(secondary_page != $(this).attr("value")) {
				$(this).addClass('blue');
				$(this).find('a.top').animate({ paddingTop: '10px' }, 500, function() {});
				$(this).find('ul').fadeIn('slow');
		//	}
		},
		function() {
		//	if(secondary_page != $(this).attr("value")) {
				$(this).find('a.top').animate({ paddingTop: '0px' }, 500, function() {});
				if(secondary_page != $(this).attr("value")) {
					$(this).removeClass('blue');
				}
				$(this).find('ul').hide();	
		//	}
		}
	);
	
	$('#live_stream_wrapper').click(function() {
		closeStream();
	});
	
	$('div.view_trailer_button').click(function() {
		video_playing = true;
		$('div#trailer img#view_' + current_image).animate({ left: '-947px' }, 500, function() {
			$(this).appendTo('div#trailer_wrapper');
			$(this).css({ left: '947px' });
		});
		current_image = 0;
		resetHomeImages();
		$('div.thoughts_matter span').hide();
		$('ul.viewing_buttons').stopTime();
		$('ul.viewing_buttons li').removeClass('selected');
		$('div.thoughts_matter').slideUp('slow');
		$('#trailer img').css({ left: '947px' });
		$('div.view_trailer').hide();
		$('div.countdown_msg').hide();
		$('div#defaultCountdown').hide();
		$('div#trailer_video').animate({ left: '-1px' }, 500, function() {
			$(this).appendTo('div#trailer_wrapper');
			addTrailer();
			$('div#video_close').show();
		});
	});
	
	$('li div.translate').toggle(
		function() {
			$(this).parent().find('.section div.normal').fadeOut();
			$(this).addClass('normal');
			$(this).parent().find('.section div.geek').fadeIn();
		},
		function() {
			$(this).parent().find('.section div.geek').fadeOut();
			$(this).removeClass('normal');
			$(this).parent().find('.section div.normal').fadeIn();
		}
	);

/*	
	$('.featured_live').hover(
		function() { 
			$(this).css({ backgroundPosition: '-950px 0' });
		},
		function() { 
			$(this).css({ backgroundPosition: '0 0' });
		}
	);
	
	$('.featured_live').click(function() {
		loadStream();
	});
*/
	
	$('.stream_box').click(function() {
		$('#featured_default').fadeOut();
		$('div.stream_box').animate({ top: '0px', height: '65px' }, 300, function() { });
		$('div.stream_box span.stream_button').html('WATCH');
		$('div.stream_box span.stream_button').animate({ width: '35px' }, 300, function() { });
		$(this).animate({ top: '30px', height: '35px' }, 500, function() { });
		$(this).find('.stream_button').animate({ width: '145px' }, 500, function() { 
			$(this).find('.stream_button').html('NOW VIEWING >');
		});
		if($(this).find('span.text').hasClass('toronto')) {
			$('.zone_est span.city').html('Toronto');
			live_feed('cntower');
		} else if($(this).find('span.text').hasClass('ottawa')) {
			$('.zone_est span.city').html('Ottawa');
			live_feed('parlament');
		} else {
			$('.zone_est span.city').html('Niagara Falls');
			live_feed('falls');
		}
		$('span.live_title').html('LIVE');
		$('.zone_pst span.city').html('Vancouver');
		getTimezoneEst();
    getTimezonePst();
	});
});

function loopHomeImage() {
	$('ul.viewing_buttons').everyTime(7500, function() {
		$('div#trailer img#view_' + current_image).animate({ left: '-947px' }, 500, function() {
			$(this).appendTo('div#trailer_wrapper');
		  $(this).css({ left: '947px' });
		});
		if(current_image > 3) {
			current_image = 1;
		} else {
			current_image++;
		}
		$('div.thoughts_matter span').fadeIn(2000);
		$('ul.viewing_buttons li').removeClass('selected');
		$('div#trailer img#view_' + current_image).animate({ left: '0px' }, 500, function() {
			$(this).appendTo('div#trailer_wrapper');
		});
		$('ul.viewing_buttons li#button_view_' + current_image).addClass('selected');
		if(current_image > 1) {
			if(current_image == 3) {
				$('div.thoughts_matter span').hide();
			}
			$('img#image_text_' + current_image).fadeIn();
		}
		resetHomeImages();
		setImage();
	});
}

function resetHomeImages() {
	$('img#image_text_1').animate({ right: '-312px' }, 500, function() { });
	$('img#image_text_2').animate({ left: '-345px' }, 500, function() { });
	$('img#image_text_3').animate({ left: '-621px' }, 500, function() { });
	$('img#image_text_4').animate({ right: '-328px' }, 500, function() { });
}

function setImage() {
	if(current_image == 1) {
		$('img#image_text_' + current_image).animate({ right: '635px' }, 500, function() { });
	} else if(current_image == 2) {
		$('div.thoughts_matter span').hide();
		$('img#image_text_' + current_image).animate({ left: '607px' }, 500, function() { });
	} else if(current_image == 4) {
		$('img#image_text_' + current_image).animate({ right: '621px' }, 500, function() { });
	} else {
		$('div.thoughts_matter span').hide();
		$('img#image_text_' + current_image).animate({ left: '326px' }, 500, function() { });
	}
}

function addTrailer() {
	$('.view_trailer_button').hide();
	$('#trailer_video').flash({
			swf: 'http://vimeo.com/moogaloop.swf?clip_id=9145320&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1&autoplay=1',
			width: 948,
			height: 399,
			params: {
				loop: "false"
			}
	});
}

function removeTrailer() {
	$('.view_trailer_button').show();
	$('#trailer_video').empty();
}

function loadFeatured() {
	document.location.href = "/featured_project";
}

function getTimezonePst() {
  var now = new Date();
	var d = new Date(now.valueOf() + ((now.getTimezoneOffset() - (8 * 60)) * 60000));
	$('.zone_pst span.time').html(formatTime(d.getHours()) + ":" + formatTime(d.getMinutes()) + ":" + formatTime(d.getSeconds()) + " PST");
	setTimeout('getTimezonePst()',1000);
};

function getTimezoneEst() {
  var now = new Date();
	var d = new Date(now.valueOf() + ((now.getTimezoneOffset() - (5 * 60)) * 60000));
  $('.zone_est span.time').html(formatTime(d.getHours()) + ":" + formatTime(d.getMinutes()) + ":" + formatTime(d.getSeconds()) + " EST");
	setTimeout('getTimezoneEst()',1000);
};

function formatTime(num) {
	if(parseInt(num) < 10) {
		return '0' + new String(num);
	} else {
		return new String(num);
	}
}

function loadStream() {
	$('#live_stream_wrapper').fadeIn();
	$('#stream_wrapper').fadeIn();
	$('span.live_title').empty();
	document.location.href = '#';
	$('body.featured').css({ overflow: 'hidden' });
}

function closeStream() {
	$('#stream_wrapper').fadeOut(function() {
		$('#live_stream_wrapper').fadeOut('slow', function() {
			$('#stream_wrapper iframe').each(function() {
				$(this).attr({
					src: $(this).attr("src")
				});
			});
		});
	});
	$('body').css({ overflow: 'auto' });
}

function live_feed(type) {
	$('.video_feed.ontario .stream').empty();
	$('.video_feed.ontario .stream').html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"  codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="320" height="240"  id="V2Player"><param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="true" /><param name="movie" value="http://http.vitalstreamcdn.com/flashskins/V2Player.swf" /><param name="quality" value="high" /><param name="flashvars"  value="stream1=' + type + '_live&serverAppInstName=rtmp://interaxon.flash.internapcdn.net/interaxon/live_1"><embed src="http://http.vitalstreamcdn.com/flashskins/V2Player.swf" flashvars="stream1=' + type + '_live&serverAppInstName=rtmp://interaxon.flash.internapcdn.net/interaxon/live_1"   quality="high" width="320" height="240" name="V2Player" allowScriptAccess="sameDomain" allowFullScreen="true"  type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
	$('.video_feed.bc .stream').empty();
	$('.video_feed.bc .stream').html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"  codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="320" height="240"  id="V2Player"><param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="true" /><param name="movie" value="http://http.vitalstreamcdn.com/flashskins/V2Player.swf" /><param name="quality" value="high" /><param name="flashvars"  value="stream1=' + type + '_control&serverAppInstName=rtmp://interaxon.flash.internapcdn.net/interaxon/live_1"><embed src="http://http.vitalstreamcdn.com/flashskins/V2Player.swf" flashvars="stream1=' + type + '_control&serverAppInstName=rtmp://interaxon.flash.internapcdn.net/interaxon/live_1"   quality="high" width="320" height="240" name="V2Player" allowScriptAccess="sameDomain" allowFullScreen="true"  type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>')
}

var thumb_step = 5;

function loadPrevImage() {
	var previous_thumb = current_thumb;
	current_thumb = current_thumb - thumb_step;
	if(current_thumb < 0){
		current_thumb = 0;
	}
	target_location = (current_thumb * 95) * -1;
	if(previous_thumb != current_thumb){
		$('#thumbs').animate({ left: target_location + 'px' }, 500, function() { });
	}
}

function loadNextImage() {
	var previous_thumb = current_thumb;
	current_thumb = current_thumb + thumb_step;
	if(current_thumb > (total_images - 2)) {
		current_thumb = total_images - 2;
	}
	if(current_thumb < 0){
		current_thumb = 0;
	}
	target_location =  (current_thumb * 95) * -1;
	if(previous_thumb != current_thumb){
		$('#thumbs').animate({ left: target_location + 'px' }, 500, function() { });
	}
}

