var img_pos=1;
var attribute='base';

var photo_offset;
var photo_width;
var photo_click;

$(function(){
	init_navigator();
	$('a').focus(function(){$(this).blur();});
	startResize();
	if(first) {setTimeout("displayPopin();",2000);}
	
	if(document.getElementById('to_slide')) $('#to_slide').slideDown(500);
	$('.news_video').each(function(){
		play_video(this.id);
	});

	if(document.getElementById('photo_cache')) {
		photo_offset=$('#photo_cache').offset();
		photo_width=$('#photo_cache').width();
		$('#photo_cache').mousemove(function(e){
			x_pos=e.pageX-photo_offset.left;
			y_pos=e.pageY-photo_offset.top;
			if(x_pos<photo_width/2) {
				photo_click=0;
				if($('#control_left').css('display')=='none'){
				if(attribute.indexOf('explorer')==-1) $('#control_left').fadeIn(imgFade);
				else $('#control_left').show();
				$('#control_right').hide();
				}
			} else if(x_pos>photo_width/2) {
				photo_click=1;
				if($('#control_right').css('display')=='none'){
				if(attribute.indexOf('explorer')==-1) $('#control_right').fadeIn(imgFade);
				else $('#control_right').show();
				$('#control_left').hide();
				}
			}	
			if(y_pos>200 && y_pos<220){
				if((x_pos>10 && x_pos<30) || (x_pos>photo_width-30 && x_pos<photo_width-10))
					{$('a','.photo_controls').css('background-position','0px -22px');$(this).css('cursor','pointer');}
				else {$('a','.photo_controls').css('background-position','0px 0px');$(this).css('cursor','default')};
			}
			else {$('a','.photo_controls').css('background-position','0px 0px');$(this).css('cursor','default');}
		});
		$('#photo_cache').mouseout(function(){
			if($('#control_right').css('display')!='none')if(attribute.indexOf('explorer')==-1) $('#control_right').fadeOut(imgFade); else $('#control_right').hide();
			if($('#control_left').css('display')!='none')if(attribute.indexOf('explorer')==-1) $('#control_left').fadeOut(imgFade); else $('#control_left').hide();
		});
		$('#photo_cache').click(function(){
			if(photo_click==0) img_dec(); else img_inc();
		});
	}

});


function init_navigator() {
	if (navigator.userAgent.indexOf('Firefox',0)!=-1) attribute='firefox3';
	if (navigator.userAgent.indexOf('Iceweasel',0)!=-1) attribute='linux';
	if (navigator.userAgent.indexOf('Safari',0)!=-1) attribute='safari';
	if (navigator.userAgent.indexOf('MSIE 6',0)!=-1) attribute='explorer6';
	if (navigator.userAgent.indexOf('MSIE 7',0)!=-1) attribute='explorer7';
	if (navigator.userAgent.indexOf('Opera',0)!=-1) attribute='opera';
	if (attribute!='explorer6'&& attribute!='explorer7'&&navigator.userAgent.indexOf('MSIE',0)!=-1) attribute='explorer';
	feuilles=document.getElementsByTagName('link');
	for (var i=0; i<feuilles.length;i++) {
	if((feuilles[i].getAttribute('rev')!=attribute)&&(feuilles[i].getAttribute('rev'))) feuilles[i].disabled='true'; 
	}
}

function displayPopin(){
$('#popin').fadeIn(300);
//if(attribute.indexOf('explorer')!=-1) $('#menu').css('filter','alpha(opacity=20)');
}

$(window).resize(function() {startResize();});
function toTop(){$('html,body').animate({ scrollTop: 0 }, 50);return false;}


function startResize(){
	if(isResize!=1) return;
	var wWidth=$(window).width();
	wWidth=Math.max(607,wWidth-300);
	wWidth=Math.floor(wWidth/202)*202;
	$('.centre').css('width',wWidth+'px');
}

function displayCache(Melmt){
	$(Melmt).attr('mouseIn','true');

	 $('.cache').fadeOut(10);
	$('.cache', Melmt).fadeIn(10);
}

function outCache(Melmt){
	$(Melmt).attr('mouseIn','false');
	hideCache(Melmt);
}

function hideCache(Melmt){
	if($(Melmt).attr('mouseIn')=='false')
		$('.cache',Melmt).fadeOut(10);
}

function slideMenu(){
	if($('ul.works').css('display')=='none') $('ul.works').slideDown(300);
	else $('ul.works').slideUp(300);
	return false;
}

	function img_inc(){
		var total=Math.floor(document.getElementById('total').innerHTML);
		var current=Math.floor(document.getElementById('current').innerHTML);
		$('#img_'+img_pos).fadeOut(imgFade);
		$('#comment_'+img_pos).hide();
		if(img_pos==total)img_pos=1;
		else img_pos++;
		$('#img_'+img_pos).fadeIn(imgFade);
		$('#comment_'+img_pos).fadeIn(200);
		$('#current').html(img_pos>9 ? img_pos : "0"+img_pos);
		play_video('player_'+img_pos);
		return false;
	}

	function img_dec(){
		var total=Math.floor(document.getElementById('total').innerHTML);
		var current=Math.floor(document.getElementById('current').innerHTML);
		$('#img_'+img_pos).fadeOut(imgFade);
		$('#comment_'+img_pos).hide();
		if(img_pos==1)img_pos=total;
		else img_pos--;
		$('#img_'+img_pos).fadeIn(imgFade);
		$('#comment_'+img_pos).fadeIn(200);
		$('#current').html(img_pos>9 ? img_pos : "0"+img_pos);
		play_video('player_'+img_pos);
		return false;
	}

	function img_go(index){
		$('#img_'+img_pos).fadeOut(imgFade);
		$('#comment_'+img_pos).hide();
		img_pos=index;
		$('#img_'+img_pos).fadeIn(imgFade);
		$('#comment_'+img_pos).fadeIn(200);
		$('#number').html(img_pos>9 ? img_pos : "0"+img_pos);
		play_video('player_'+img_pos);
		return false;
	}

	function play_video(video_id){
		if(!document.getElementById(video_id)) return;
		flowplayer(video_id, "swf/flowplayer-3.2.5.swf");
		/*
		flowplayer(video_id, {src:$('#'+video_id).attr('rel'),wmode:'opaque'}, {
			play: null,
	plugins: {
			controls: {
		      backgroundColor: '#FFFFFF',
		      backgroundGradient: 'none',
		      durationColor: '#88a423',
		      buttonOverColor: '#53Cdf3',
		      buttonColor: '#209DE8',
		      bufferGradient: 'none',
		      sliderColor: '#444444',
		      tooltipColor: '#5F747C',
		      progressColor: '#209DE8',
		      bufferColor: '#AAAAAA',
		      progressGradient: 'medium',
		      timeBgColor: '#555555',
		      volumeSliderColor: '#000000',
		      volumeSliderGradient: 'none',
		      borderRadius: '0px',
		      sliderGradient: 'none',
		      tooltipTextColor: '#ffffff',
		      timeColor: '#b8bca9',
		      height: 20,
		      opacity: 1.0,
		      volume:false,
		      time:false,
		      fullscreen:false,
		      mute:false
		   }


	},
			clip : {          
		        autoPlay: true, 
		        autoBuffering: true,
		        url: $('#'+video_id).attr('rel')
			}
		});*/
		return;
	}
