var time;
var isiPad = navigator.userAgent.match(/AppleWebKit/i) != null;
var myScroll, myScroll2, myScroll3;
var next, prev;
var total, total2, total3;
var vendor = (/webkit/i).test(navigator.appVersion) ? '-webkit-' :(/firefox/i).test(navigator.userAgent) ? '-moz-' :'opera' in window ? '-o-' : '';
var cssn = (/webkit/i).test(navigator.appVersion) ? 4 :(/firefox/i).test(navigator.userAgent) ? 4 :'opera' in window ? 4 : '';
var tochs;
var nWidth, nHeight;
var h = $('#img img').innerHeight();
var w = $('#img img').innerWidth();
var fh = $('#imgfull img').innerHeight();
var fw = $('#imgfull img').innerWidth();
function SetMyscroll(){
	if($('#boxcorre').length > 0){
		myScroll = new iScroll('boxcorre',{snap:'p',vScroll: false});
	}
	if($('#correcliente').length > 0){
		myScroll2 = new iScroll('correcliente',{snap:'ul',vScroll: false});
	}
	if($('#corremidia').length > 0){
		myScroll3 = new iScroll('corremidia',{snap:'ul',vScroll: false});
	}
}
setInterval(function () {
	if (self.innerWidth != undefined)
	{
	 nWidth = self.innerWidth;
	 nHeight = self.innerHeight;
	} else {
	 nWidth = document.documentElement.clientWidth;
	 nHeight = document.documentElement.clientHeight;
	}
	$('#imgfull img').css({'max-height':(nHeight-10)+'px','max-width':(nWidth-10)+'px'});
	if($('#imgfull').css('display') != 'none' || $('#img').css('display') != 'none'){
		var io, o, bot;
		if($('#imgfull').css('display') == 'none'){
			io = $('#img');
			o = io.offset();
			bot = 40;
		}else{
			io = $('#imgfull');
			o = io.offset();
			bot = 0;
		}
		if(o.top+'px' != $('#f1').css('height')){
			$('#f1, #f3').css({'height':o.top+'px'});
		}
		if((o.left-bot)+'px' != $('#f1').css('width')){
			$('#f2, #f4').css({'width':(o.left-bot)+'px'});
		}
	}
},100);
function fadepreto(){
	$('#imgfull').fadeOut(500);
	$('#pesquisa span').css({'color':'#777'});
	_('#pesquisa','out');
	_('#data','out');
	_('#cliente','out');
	_('#midia','out');
	_('#img','out');
	_('#preto','out');
	_('.preto','out');
	_('.fadeclick','out');
	$('#hidden').html('');
}
function stopscroll(){
	clearInterval(time);
	return false;
}
var alt;
var src2;
var iddes;
function getIMG(id){
	$('#imgfull').fadeOut(0);
	$('.fadeclick').fadeIn(0);
	$('#img img').fadeOut(500);
	var lid;
	var src = $('#'+id).attr('src').replace('_m','');
	var midia = $('#'+id).attr('midia');
	var media = $('#'+id).attr('media');
	var idsecret = $('#'+id).attr('idsecret').split('|');
	var description = $('#'+id).attr('descri');
	var linkf = $('#'+id).attr('linkf');
	alt = $('#'+id).attr('alt');
	iddes = '[descri="'+description+'"][alt="'+alt+'"]';
	src2 = $('#'+id).attr('src').replace('_m','_b');
	clearTimeout(tttime);
	var tttime = setTimeout(function (){
		$('#hidden').html('');
		if(media == 'photo'){
			$('#hidden').html('<img style="display:none;" src="'+src+'" alt="'+alt+'" title="'+alt+'">');
		}else{
			if(isiPad){
				var video;
				$('#hidden').html('<p id="video"></p>');
				$.post(RAIZ+'getvideo.ajx',{'url':linkf},function (d){
					$('#hidden').html('<p id="video"></p><img style="display:none;position:absolute;width:640px;height:480px;" src="'+src+'" alt="'+alt+'">'+d);
					$('#loader').fadeOut()
				});
			}else{
				$('#hidden').html('<p id="video"></p><img style="display:none;position:absolute;width:640px;height:480px;" src="'+src+'" alt="'+alt+'"><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/video/stewart.swf.v71377" style="visibility:visible;position:absolute;width:640px;height:480px;" base="." quality="high" allowfullscreen="true" allowscriptaccess="always" wmode="opaque" flashvars="intl_lang=pt-br&amp;flickr_notracking=true&amp;flickr_target=_self&amp;flickr_h=480&amp;flickr_w=640&amp;flickr_no_logo=true&amp;onsite=true&amp;flickr_noAutoPlay=true&amp;in_photo_gne=true&amp;photo_secret='+idsecret[1]+'&amp;photo_id='+idsecret[0]+'&amp;flickr_doSmall=true">');
			}
		}
		h = 0;
		w = 0;
		$('#img span').eq(0).text(description);
		$('#img span').eq(1).text(alt);
		$('#img span').eq(2).text(midia);
		$(iddes).each(function (e){
			if($(this).attr('id') == id){
				lid = e;
			}
		});
		if(lid == $(iddes).length-1){
			next = $(iddes).eq(0).attr('id');
		}else{
			next = $(iddes).eq(lid+1).attr('id');
		}
		prev = $(iddes).eq(lid-1).attr('id');
		if($(iddes).length == 1 || $('#img span').eq(0).html() == ''){
			$('#img .seta1, #img .seta2').css({'display':'none'});
		}else{
			$('#img .seta1, #img .seta2').css({'display':'block'});
		}
		$('#preto, #img, #img img').fadeIn(500);
		$('.preto').fadeIn(0);
	},500);
}
function tochstart(){
	tochs = 1;
}
function tochmove(){
	if(tochs == 1){
		setTimeout(function(){
			tochs = 0;
		},100);
	}
}
function tochend(este){
	if(tochs == 1){
		getIMG($(este).attr('id'));
	}
}
function _(id,css){
	var modi;
	if(css == undefined){
		if($(id).css('display') == 'block'){
			$(id).fadeOut(600);
			modi = 'out';
		}else{
			$(id).fadeIn(600);
			modi = 'in';
		}
	}else{
		if(css == 'out'){
			$(id).fadeOut(600);
			modi = css;
		}else if(css == 'in'){
			$(id).fadeIn(600);
			modi = css;
		}else{
			alert('Erro in function "_" ! css = '+css);
		}
	}
	return modi;
}
setInterval(function (){
	var nh = $('#img img').innerHeight();
	var nw = $('#img img').innerWidth();
	if(h != nh || w != nw){
		if(nh != 0 && nw != 0){
			h = nh;
			w = nw;
			$('#hidden').animate({'width':w,'height':h},500);
		}
	}
	var fnh = $('#imgfull img').innerHeight();
	var fnw = $('#imgfull img').innerWidth();
	if(fh != fnh || fw != fnw){
		if(fnh != 0 && fnw != 0){
			fh = fnh;
			fw = fnw;
			$('#imgfull').animate({'width':fw+'px','height':fh+'px'},500);
		}
	}	
},500);
$(document).ready(function(){
	var tscrol;
	$('#hidden').live('click',function(){
		if($('#video').length == 0){
			$('#imgfull').html('');
			$('#imgfull').html('<img src="'+src2+'" alt="'+alt+'" title="'+alt+'">');
			$('#img').fadeOut(500);
			$('#imgfull').delay(500).fadeIn(500);
		}
	});
	$('#imgfull').live('click',function(){
		$(this).fadeOut(500);
		$('#img').delay(500).fadeIn(500);
	});
	$('.iniout').fadeIn(1000);
	$('#preto, .fadeclick').live('click',function(){
		fadepreto();
	});
	var img;
	var ul;
	var ul2;
		setInterval(function (){
			img = $('.act');
			total = img.length;
			if(total > 0){
				var widthn = (190 + (8*2))*total;
				$('#imgs').css({'width':widthn+'px'});
			}
			ul = $('#cliente ul');
			total2 = ul.length;
			if(total2 > 0){
				var widthn2 = (parseInt(ul.css('width').replace('px','')) + (0*2))*total2;
				$('#scrolcli').css({'width':widthn2+'px'});
			}
			ul2 = $('#midia ul');
			total3 = ul2.length;
			if(total3 > 0){
				var widthn3 = (parseInt(ul2.css('width').replace('px','')) + (0*2))*total3;
				$('#scrolmid').css({'width':widthn3+'px'});
			}
		},500);
	$(document).keydown(function(e){
		var key = (e.keyCode ? e.keyCode : e.which);
		if(key == 37){
			if($('#img').css('display') != 'none'){
				if($(iddes).length != 1 && $('#img span').eq(0).html() != ''){
					getIMG(prev)
				}
			}else if($('#cliente').css('display') == 'block'){
				myScroll2.scrollToPage('prev', 0);
			}else if($('#midia').css('display') == 'block'){
				myScroll3.scrollToPage('prev', 0);
			}else{
				myScroll.scrollToPage('prev', 0);
			}
		}
		if(key == 39){
			if($('#img').css('display') != 'none'){
				if($(iddes).length != 1 && $('#img span').eq(0).html() != ''){
					getIMG(next)
				}
			}else if($('#cliente').css('display') == 'block'){
				myScroll2.scrollToPage('next', 0);
			}else if($('#midia').css('display') == 'block'){
				myScroll3.scrollToPage('next', 0);
			}else{
				myScroll.scrollToPage('next', 0);
			}
		}
	});
	$(document).keyup(function(e){
		stopscroll();
	});
	// MyScroll
		SetMyscroll();
	// End Myscroll
	$('#img .seta1').live('click',function(){
		if($(iddes).length != 1 && $('#img img').attr('descri') != ''){
			getIMG(prev);
		}
	});
	$('#img .seta2').live('click',function(){
		if($(iddes).length != 1 && $('#img span').eq(0).html() != ''){
			getIMG(next);
		}
	});
	$('#seta1').live('mousedown',function(){
		myScroll.scrollToPage('prev', 0);
	});
	$('#seta2').live('mousedown',function(){
		myScroll.scrollToPage('next', 0);
	});
	$('#corre .seta1, #corre .seta2').live('mouseup',function(){
		stopscroll();
	});
	var clicks;
	$('#imgs img').live('mousedown',function(){
		clicks = 1;
	});
	$('#imgs img').live('mousemove',function(){
		if(clicks == 1){
			setTimeout(function(){
				clicks = 0;
			},200);
		}
	});
	$('#imgs img').live('mouseup',function(){
		if(clicks == 1){
			getIMG($(this).attr('id'));
		}
	});
	$('#lupa,#lupaa').live('click',function (){
		$('#pesquisa span').css({'color':'#777'});
		if(_('#pesquisa') == 'out'){
			_('#data','out');
			_('#cliente','out');
			_('#midia','out');
		}
	});
	$('#botd').live('click',function (){
		if(_('#data') == 'in'){
			$('#pesquisa span').css({'color':'#777'});
			$(this).css({'color':'#DBC359'});
		}else{
			$('#pesquisa span').css({'color':'#777'});
		}
		_('#cliente','out');
		_('#midia','out');
		_('#preto','in');
	});
	$('#botc').live('click',function (){
		if(_('#cliente') == 'in'){
			$('#pesquisa span').css({'color':'#777'});
			$(this).css({'color':'#DBC359'});
		}else{
			$('#pesquisa span').css({'color':'#777'});
		}
		_('#data','out');
		_('#midia','out');
		_('#preto','in');
	});
	$('#botm').live('click',function (){
		if(_('#midia') == 'in'){
			$('#pesquisa span').css({'color':'#777'});
			$(this).css({'color':'#DBC359'});
		}else{
			$('#pesquisa span').css({'color':'#777'});
		}
		_('#data','out');
		_('#cliente','out');
		_('#preto','in');
	});
	$('#cliente span').live('click',function(){
		_('#pesquisa','out');
		_('#cliente','out');
		var clase = $(this).text().substring(0,27);
		$('[lang="s"]').each(function (){
			if($(this).find('img').attr('title').substring(0,27) != clase){
				$(this).fadeOut(500);
				$(this).find('img').attr('class','nact');
				$(this).find('p').remove();
			}else{
				$(this).delay(500).fadeIn(500);
				$(this).find('img').attr('class','act');
				$(this).find('p').remove();
				$('<p></p>').insertAfter($(this).find('b'));
			}
		});
		SetMyscroll();
		$('#preto').fadeOut(500);
	});
	$('#midia span').live('click',function(){
		_('#pesquisa','out');
		_('#midia','out');
		var clase = $(this).text().substring(0,28);
		$('[lang="s"]').each(function (){
			if($(this).find('img').attr('midia').substring(0,28) != clase){
				$(this).fadeOut(500);
				$(this).find('img').attr('class','nact');
				$(this).find('p').remove();
			}else{
				$(this).delay(500).fadeIn(500);
				$(this).find('img').attr('class','act');
				$(this).find('p').remove();
				$('<p></p>').insertAfter($(this).find('b'));
			}
		});
		SetMyscroll();
		$('#preto').fadeOut(500);
	});
	function time(){
		var metime = $('.atv').eq(0).attr('time');
		var matime = $('.atv').eq(1).attr('time');
		$('[lang="s"]').each(function (){
			if($(this).find('img').attr('time') >= metime && $(this).find('img').attr('time') <= matime){
				$(this).delay(500).fadeIn(500);
				$(this).find('img').attr('class','act');
				$(this).find('p').remove();
			}else{
				$(this).fadeOut(500);
				$(this).find('img').attr('class','nact');
				$(this).find('p').remove();
				$('<p></p>').insertAfter($(this).find('b'));
			}
		});
		SetMyscroll();
	}
	$('.dts .seta1').live('click',function (){
		var ant = $('#'+$(this).parent().find('ul').attr('id')+' .atv');
		if($('#'+$(this).parent().find('ul').attr('id')+' .atv').prev('li').html() != null){
			$('#'+$(this).parent().find('ul').attr('id')+' .atv').prev('li').attr('class','atv');
			ant.attr('class','');
			time();
		}
	});
	$('.dts .seta2').live('click',function (){
		var ant = $('#'+$(this).parent().find('ul').attr('id')+' .atv');
		if($('#'+$(this).parent().find('ul').attr('id')+' .atv').next('li').html() != null){
			$('#'+$(this).parent().find('ul').attr('id')+' .atv').next('li').attr('class','atv');
			ant.attr('class','');
			time();
		}
	});
	$('.bott').live('click',function(){
		$('#pesquisa span').css({'color':'#777'});
		_('#data','out');
		_('#cliente','out');
		_('#midia','out');
		_('#pesquisa','out');
		_('#preto','out');
		$('[lang="s"]').each(function (){
			$(this).fadeIn(500);
			$(this).find('img').attr('class','act');
			$(this).find('p').remove();
			$('<p></p>').insertAfter($(this).find('b'));
		});
		SetMyscroll();
	});
});
