/**************************
	JavaScript by Ginga Interactive
	Created on August 2009
	Coder: flep!
***************************/
jQuery.noConflict();
jQuery(document).ready(function($){
	
	$('#full').css({'height': $(document).height() +'px'});
	
	if(window.location.href.indexOf('project') != -1){
		$('#menu a:eq(1)').addClass('selected');
	} else if(window.location.href.indexOf('platform') != -1){
		$('#menu a:eq(2)').addClass('selected');
	}
	
	if($.browser.msie && $.browser.version=="6.0"){
		html = "<div class='ie6alert' style='height:"+ $(document).height() +"px;'>";
		html += "<\/div>"
		html += "<div class='ie6alert_text'>";
		html += "Sorry, but our website doesn't support Internet Explorer 6 and will never do.<br>";
		html += "We use advanced CSS3 features, so we recommend you to choose between <a href='http://www.mozilla.org/firefox'>Firefox</a>, <a href='http://www.apple.com/safari/'>Safari</a> or <a href='http://www.google.com/chrome'>Google Chrome</a>.<br>";
		html += "Of course, you can choose to use iE7 or iE8, however, these browsers wont support all of our website's features.<br><br>";
		html += "Thank you";
		html += "<\/div>";
		$('body').after($(html));
		$('.ie6alert').css({'opacity':'0.9'})
	}
	notiE6 = function(){
		if(!$.browser.mozilla && !$.browser.webkit && ($.browser.msie && $.browser.version>="7.0")){
			if(!$.cookie("notiE6")){
				html="<div class='ie_alert'>";
				html+="&nbsp;&nbsp;We highly recommend you to use <a href='http://www.mozilla.org/firefox' target='_blank'>Firefox</a>, <a href='http://www.apple.com/safari/' target='_blank'>Safari</a> or <a href='http://www.google.com/chrome' target='_blank'>Google Chrome</a> to have the best experience in our website, due the lack of CSS3 support in other browsers. <a href='javascript:;' onclick='alertClose()' style='float:right;'>close&nbsp;&nbsp;</a>";
				html+= "<\/div>";
				$('body').before($(html));
				$('.ie_alert').animate({height:'20px'});
				$.cookie("notiE6", "true", { expires: 0 });
			}
		}	
	}
	
	alertClose = function(){
		$('.ie_alert').animate({height:'0px'});
		//$.cookie("notiE6", null);
	}
	
	setLateral = function(color,type,file,margin,height){
		
			$('body').css({'background' : ''+ color +'' });
			if($('body').width() >= '1260'){
				if (type == 'img'){
					$('div#footer').after(
						'<div id="lateral" style="padding-top:'+ margin +'px;">'
							+'<img src="'+ file +'" border="0" />'
						+'</div>'
					);
					$('#lateral').fadeIn(300);
				} else if(type == 'flash'){
					$('div#footer').after(
						"<div id='lateral' style='padding-top:"+ margin +"px;'>"
							+"<div id='Lflash'>"
							+"</div>"
						+"</div>"
						+"<script type='text/javascript'>"
							+"var flashvars = false; var params = {bgcolor: '#E9E6D4', scale: 'noscale', wmode: 'transparent', align: 'middle'}; var attributes = false;"
    						+"swfobject.embedSWF('"+ file +"', 'Lflash', '100%', '"+ height +"', '10.0.2', 'expressInstall.swf', flashvars, params, attributes);"
    					+"</script>"
					);
					$('#lateral').fadeIn(300);
				} else{}
			}
		
	};

	function get_rss_feed(url,alvo) {
		$.get('proxy?url=' + url, function(t){
			$(alvo + " span").fadeOut(300, function(){
			$(t).find('item').each(function(i) {
				var $item = $(this);
				var title = $item.find('title').text();
				title = title.replace("agenciaginga:", "");
				var link = $item.find('link').text();
				var html = "<div style='display:none'><a href='"+ link +"' target='_blank'>" + title + "<\/a><\/div>";
				if (i < 1){
					$(alvo).append($(html));
				}
			})
			$(alvo + ' div').fadeIn(400);
			});
		});
	}
	
	if($('.h_externals td').hasClass("rss_twitter")){
		url = 'http://twitter.com/statuses/user_timeline/14592257.rss';
		alvo = '.rss_twitter';
		get_rss_feed(url,alvo);
	}
	if($('.h_externals td').hasClass("rss_blah")){
		url = 'http://blah.agenciaginga.com/?feed=rss2';
		alvo = '.rss_blah';
		get_rss_feed(url,alvo);
	}
	if($('.h_externals td').hasClass("rss_ant")){
		url = 'http://antenado.agenciaginga.com/feed/';
		alvo = '.rss_ant';
		get_rss_feed(url,alvo);
	}
	if($('.h_externals td').hasClass("rss_press")){
		url = 'http://press.agenciaginga.com/?feed=rss2';
		alvo = '.rss_press';
		get_rss_feed(url,alvo);
	}

	$(function(){$('a').attr('onFocus','this.blur()')}); 
	//using onfocus with "F" for iE8
	
	$('#top h1').click(function(){
		document.location.href="/";
	}); 
	
	$(function() {
		$('.box_cases').hover(function(){
			$(this).find('p').animate({top:'1px'},{queue:false,duration:500});
		}, function(){
			$(this).find('p').animate({top:'150px'},{queue:false,duration:500});
		});
	});

	$(function() {
		$('.box_customers').hover(function(){
			$(this).find('div.inner_box').animate({top:'0px'},{queue:false,duration:500});
		}, function(){
			$(this).find('div.inner_box').animate({top:'215px'},{queue:false,duration:500});
		});
	});

	magicControl = function(e){
		var gmaps = '<iframe id="google" width="515" height="345" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?q=R.+Dr.+Paulo+Vieira,+270&amp;hl=en&amp;cd=1&amp;ei=MNgOS8rIL4LKyQSSk_GYCQ&amp;sll=-23.543296,-46.686806&amp;sspn=0.071946,0.071946&amp;ie=UTF8&amp;view=map&amp;cid=1058836361895055475&amp;ved=0CBYQpQY&amp;hq=R.+Dr.+Paulo+Vieira,+270&amp;hnear=&amp;ll=-23.540265,-46.685951&amp;spn=0.006787,0.011051&amp;z=16&amp;iwloc=A&amp;output=embed"></iframe>'
		if($('div#inner_magic').height() == "0"){
			$('div#magic_areas').css({'z-index':'4'});
			$('div#inner_magic').stop().animate({height:'500px'}, 700);

			if(e.id == 'about' || e == 'keys'){
				$('.magic_divs').css({'display':'none'});
				$('#dv_about').css({'display':'block'});
			} else if(e.id == 'trends'){
				$('.magic_divs').css({'display':'none'});
				$('#dv_trends').css({'display':'block'});
			} else if(e.id == 'contact'){
				$('.magic_divs').css({'display':'none'});
				$('#dv_contact').css({'display':'block'});
				$('.map:empty').html(gmaps);
			}
		} else {
			if($('#dv_about').css('display') != 'block' && (e.id == 'about' || e == 'keys')){
				$('.magic_divs:not(#dv_about)').customFadeOut(300, function(){
					$('#dv_about').customFadeIn(300);
					if (e.id == 'about') {$('.anythingSlider').anythingSlider(1)};
				});
			} else if($('#dv_trends').css('display') != 'block' && e.id == 'trends'){
				$('.magic_divs:not(#dv_trends)').customFadeOut(300, function(){
					$('#dv_trends').customFadeIn(300);
				});
			} else if($('#dv_contact').css('display') != 'block' && e.id == 'contact'){
				$('.magic_divs:not(#dv_contact)').customFadeOut(300, function(){
					$('#dv_contact').customFadeIn(300);
					$('.map:empty').html(gmaps);
				});
			} else if(e != 'keys') {
				magicClose();
			}
		}
	}
	magicClose = function(){
		$('div#inner_magic').stop().animate({height:'0px'}, 700, function(){
			$('div#magic_areas').css({'z-index':'2'});
			$('#dv_about').css({'display':'block'});
			$('.magic_divs:not(#dv_about)').css({'display':'none'});
			$('.anythingSlider').anythingSlider(1);
		});
	}

		
	magicScroll = function(e){
		$('#'+e).parent().parent().find('a.selected').removeClass('selected')
		//$('.areas_c1 a').removeClass('selected');
		$('#'+e).addClass('selected');
		$target = '#'+ e +'t';
		$('#'+e).parent().parent().parent().parent().find('.areas_c2').stop().scrollTo( $target , 500 );
		
	}
	
	$('#menu a.magic_links').click(function(){
		magicControl(this);
		if($(this).hasClass('selected') == true){
			$('#menu a').removeClass('selected');
		} else {
			$('#menu a').removeClass('selected');
			$(this).addClass('selected');
		}
	});
	
	$("#magic_menu a[id]").click(function(){
    	slide = ($('#magic_menu a').index(this) + 1 );
		$('.anythingSlider').anythingSlider(slide);
	});
		
	$('.areas_c1 a').click(function(){
		magicScroll(this.id);
	});
	
	$('.magic_close').click(function(){
		magicClose();
		$('#menu a').removeClass('selected');
	})
	
	openKeys = function(a){
		target = a.id.replace('key_',"");
		key = target.replace(/.{1}$/,"");
		$('#'+key).click();
		//$('#dv_about').css({'display':'block'});
		$('#dv_about').fadeIn(300);
		$('.magic_divs:not(#dv_about)').css({'display':'none'});
		$('#'+target).click();
		$('body').scrollTo(0, 300, {
			onAfter:function(){
				magicControl('keys');
				$('#menu a').removeClass('selected');
				$('#menu a#about').addClass('selected');
			}
		});
	}
	
	//KEY BODY LINKS
	$('#h_keys a').click(function(){
		openKeys(this);	
	});
	$('#ficha a[id]').click(function(){
		openKeys(this);	
	});
	
	$('a.bt_pj:odd').addClass('bt2');
	
	$(function(){
		$('a.bt_pj').click(function(){
			target = 'target'+ ($('#pj_left a').index(this) + 1);
			$('#pj_right div.pj_tabs:not(#'+ target +')').hide(500, function(){
				$('#'+ target +':hidden').stop().show(500, arguments.callee);
			});
			
		});
	});
	
	$('.customers_links').jScrollPane({showArrows:true, scrollbarWidth:7, scrollbarMargin:10});
	
	 $("#tags_bt").hover(function(){ 
		$('#tags_bt').css({'border-bottom':'1px solid #660000'});
		$(this).parent().find("ul.subnav").slideDown('fast').show();
		$(this).parent().hover(function(){
		}, function(){  
			$(this).parent().find("ul.subnav").slideUp('fast',function(){$('#tags_bt').css({'border-bottom':'0'});});
		});  
	 });
	
	$('.anythingSlider').anythingSlider({
		easing: "easeInOutExpo",
		autoPlay: true,
		delay: 3000,
		startStopped: true,
		animationTime: 600,
		hashTags: false,
		buildNavigation: false,
		pauseOnHover: false,
		startText: "",
		stopText: "",
		navigationFormatter: null
	});
	$('#customers_gallery').anythingSlider({
		loop:true,
		easing: "easeInOutCubic",                // Anything other than "linear" or "swing" requires the easing plugin
		autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not
		startStopped: false,            // If autoPlay is on, this can force it to start stopped
		delay: 3000,                    // How long between slide transitions in AutoPlay mode
		animationTime: 800,             // How long the slide transition takes
		hashTags: false,                 // Should links change the hashtag in the URL?
		buildNavigation: false,          // If true, builds and list of anchor links to link to each slide
		pauseOnHover: false,             // If true, and autoPlay is enabled, the show will pause on hover
		startText: "",             // Start text
		stopText: "",               // Stop text
		navigationFormatter: null       // Details at the top of the file on this use (advanced use)
	});

	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	
	$('#showreel').fancybox({
		hideOnContentClick: false,
		overlayOpacity: 0.8,
		zoomOpacity: false,
		frameWidth: 705,
		frameHeight: 430
	});
	$('.tooltip').css('opacity', 0);
	
	$('.premio_tooltip').hover(function(){
			$('.tooltip').css('display', 'block');
			$('.tooltip').stop().animate({left:'90px', opacity:1}, 300);
		}, 
		function(){
			$('.tooltip').stop().animate({left:'130px', opacity:0}, 300, function(){$('.tooltip').css('display', 'none');});
		}
	);
	
	$('#bt_en').hover(function(){
			$('#tt').css('display', 'block');
			$('#tt').animate({top:'17px', opacity:0.8}, 300);
		},
		function(){
			$('#tt').animate({top:'0px', opacity:0}, 300, function(){$('#tt').css('display', 'none');});
	});
	
	setTimeout("jQuery('#full').fadeOut(300, notiE6());", 500)
	 
});
