// ============================================== //

		// TIMER FUNCTIONS //

// ============================================== //

jQuery.fn.extend({everyTime:function(interval,label,fn,times,belay){return this.each(function(){jQuery.timer.add(this,interval,label,fn,times,belay)})},oneTime:function(interval,label,fn){return this.each(function(){jQuery.timer.add(this,interval,label,fn,1)})},stopTime:function(label,fn){return this.each(function(){jQuery.timer.remove(this,label,fn)})}});jQuery.extend({timer:{guid:1,global:{},regex:/^([0-9]+)\s*(.*s)?$/,powers:{'ms':1,'cs':10,'ds':100,'s':1000,'das':10000,'hs':100000,'ks':1000000},timeParse:function(value){if(value==undefined||value==null)return null;var result=this.regex.exec(jQuery.trim(value.toString()));if(result[2]){var num=parseInt(result[1],10);var mult=this.powers[result[2]]||1;return num*mult}else{return value}},add:function(element,interval,label,fn,times,belay){var counter=0;if(jQuery.isFunction(label)){if(!times)times=fn;fn=label;label=interval}interval=jQuery.timer.timeParse(interval);if(typeof interval!='number'||isNaN(interval)||interval<=0)return;if(times&&times.constructor!=Number){belay=!!times;times=0}times=times||0;belay=belay||false;if(!element.$timers)element.$timers={};if(!element.$timers[label])element.$timers[label]={};fn.$timerID=fn.$timerID||this.guid++;var handler=function(){if(belay&&this.inProgress)return;this.inProgress=true;if((++counter>times&&times!==0)||fn.call(element,counter)===false)jQuery.timer.remove(element,label,fn);this.inProgress=false};handler.$timerID=fn.$timerID;if(!element.$timers[label][fn.$timerID])element.$timers[label][fn.$timerID]=window.setInterval(handler,interval);if(!this.global[label])this.global[label]=[];this.global[label].push(element)},remove:function(element,label,fn){var timers=element.$timers,ret;if(timers){if(!label){for(label in timers)this.remove(element,label,fn)}else if(timers[label]){if(fn){if(fn.$timerID){window.clearInterval(timers[label][fn.$timerID]);delete timers[label][fn.$timerID]}}else{for(var fn in timers[label]){window.clearInterval(timers[label][fn]);delete timers[label][fn]}}for(ret in timers[label])break;if(!ret){ret=null;delete timers[label]}}for(ret in timers)break;if(!ret)element.$timers=null}}}});if(jQuery.browser.msie)jQuery(window).one("unload",function(){var global=jQuery.timer.global;for(var label in global){var els=global[label],i=els.length;while(--i)jQuery.timer.remove(els[i],label)}});


// ============================================== //


(function($){
		  
	$.fn.initProject_button = function() {
		var target = $(this);
		
		var newHtml = '<a href="" class="project_overlay"></a>';
		target.find('div.project').each(function(){
			var tempvimLink = $(this).find('a').attr('rel');
			//
			$(this).append(newHtml);
			var thisFirst = $(this).find('a:first');
			var newHref = thisFirst.attr('href');
			$(this).find('a.project_overlay').attr('href', newHref).data('vimLink', tempvimLink);
			//
			$(this).find('a.project_overlay').css({'opacity':0,'display':'block'}).hover(function() {
				$(this).animate({'opacity':1},200);
			}, function() {
				$(this).animate({'opacity':0},300);
			});
			//
			$(this).find('a.project_overlay').click(function(){
				//alert($(this).data('vimLink'));
				//$(".youtube").colorbox({iframe:true, innerWidth:425, innerHeight:344});
				var vimCode = 'http://player.vimeo.com/video/'+$(this).data('vimLink')+'?title=0&amp;byline=0&amp;portrait=0';
				$.colorbox({href:vimCode, iframe:true, innerWidth:640, innerHeight:360, transition:"fade"});
				return false;
			});
			//
		});
	};
	
	initHomePage = function(){
		var target = $('#content');
		//
		//// Jplayer
		//
		$('body').append('<div id="jquery_jplayer"></div>');
		//
		$("#jquery_jplayer").jPlayer({
			ready: function() {
				$(this).jPlayer("volume", 0.4);
				//$(this).jPlayerId("loadBar", "home_tip_audio")
			},
			ended: function() {
				$(this).jPlayer("play");
			},
			swfPath: "http://www.xyz-productions.gr/includes/js"
		});
		//
		//// Tool Tip
		//
		target.prepend('<div id="home_tip"><\/div>');
		//
		$("#home_tip").css({
			opacity: 0				   
		});
		//
		setMouseMove = function(){
			var offset = target.offset();
			target.mousemove(function(e){
				$("#home_tip").css({
					top: (e.pageY - offset.top - 20) + "px",
					left: (e.pageX - offset.left + 20) + "px"
				});
			});
		};
		//
		endMouseMove = function(){
			target.unbind('mousemove');
			$("#home_tip").css({
				top: 0 + "px",
				left: 0 + "px"
			});
		};
		//
		target.find('a').each(function(){
			var myIndex = target.find('a').index(this);
			$(this).data('myId', myIndex);
			//
			$(this).hover(function() {
				setMouseMove();			   
				var tipContent = $('#home_link_list').find('li').eq($(this).data('myId')).html();
				//
				if($('#home_link_list').find('li').eq($(this).data('myId')).hasClass('audio')){
					$("#jquery_jplayer").jPlayer("setMedia", {mp3: tipContent});
					$("#jquery_jplayer").jPlayer("play");
					$("#home_tip").html('').addClass('home_tip_audio');   
				}else{
					$("#home_tip").html('<img src="'+tipContent+'" width="320" height="184" \/>').removeClass('home_tip_audio');     
				}
				$("#home_tip").stop().animate({'opacity':1},100);
			}, function() {
				$("#jquery_jplayer").jPlayer("stop");
				$("#home_tip").stop().animate({'opacity':0},200,function(){
					endMouseMove();
				});
			});
		});
	};
	
	initVideoHomePage = function(){
		
		setPlayer = function(playerId, moviePath){
			jwplayer(playerId).setup({ 
				flashplayer: "http://www.xyz-productions.gr/includes/flash/jwplayer.swf", 
				file: moviePath,
				bufferlength: 10,
				/*controlbar: 'bottom',*/
				skin: "http://www.xyz-productions.gr/includes/flash/unit.zip",
				controlbar: 'none',
				"controlbar.idlehide": true,
				stretching:'fill',
				autostart: true,
				volume: 0,
				height: '100%', 
				width: '100%' 
			});
		};

		setPlayer('player', 'http://www.xyz-productions.gr/movies/test_video.mp4');
		
	};
	
	swicthVimeo = function(vimeoId){
		var newUrl = 'http://player.vimeo.com/video/'+vimeoId+'?title=0&amp;byline=0&amp;portrait=0';
		$('div.content_sub_a iframe').attr('src', newUrl);
	};
	
	initVimeoHomePage = function(){
	
		$('#page_home div.content_sub_a ul li a').each(function(){
			//
			$(this).click(function(){
				swicthVimeo($(this).attr('href'))	;
				return false;
			});
			//
		});
	
	};
	/*
		---------------------------------------------------------------------------------- QUOTE CAROUSEL
																	
		------------------------------------------------------------------------------------------------
	*/
	
	$.fn.initQuotes = function() {
		var target = $(this);
		var arrayItem = 0;
		var arrayLength = 0;
		var speed = 5000;
		//
		target.find('li').hide();
		//
		target.find('li').each(function(i) {
			$(this).animate({ opacity: 0 }, 0.001);
			$(this).css({ 'position' : 'absolute', 'top' : 0, 'left' : 0 });
			arrayLength+=1;
		});
		//
		arrayItem=Math.floor(Math.random()*arrayLength)
		//
		showLi = function(){
			target.find('li').eq(arrayItem).show().animate({ opacity: 1 }, 300, function(){
				advanceItem();																		 
			});
		};
		//
		advanceItem = function(){
			//
			target.oneTime(speed, function() {
				target.find('li').eq(arrayItem).animate({ opacity: 0 }, 400, function(){
					$(this).hide();														  
					arrayItem++;
					if(arrayItem>(arrayLength-1)){
						arrayItem=0;
					}																  
					showLi();																  
				});
			});
			//
		};
		//
		showLi();
		//
	};
	
	
})(jQuery);


$(document).ready(function(){
	if ($('.project_row').length) {
		$('#content').initProject_button();
	};
	//
	if ($('#page_home').length) {
		//initHomePage();
		//initVideoHomePage();
		initVimeoHomePage();
	};
	//
	if ($('#quote_carousel').length) {
		$('#quote_carousel').initQuotes();
	};
	
});

