if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
var viewportmeta = document.querySelectorAll('meta[name="viewport"]')[0];
if (viewportmeta) {
viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0';
document.body.addEventListener('gesturestart', function() {
viewportmeta.content = 'width=device-width, minimum-scale=0.25, maximum-scale=1.6';
}, false);
}
}

Cufon.replace('#menu-main a, #menu-sub a, #submenu a, h1, h2, h3, h4, #news-footer .left', {
	hover: true
});


function slideSwitchAbout(){
	//set active var
    var active = $('.slideshow li.active');
    //if active is empty set the first item active
    if ( active.length == 0 )
      active = $('.slideshow li:first');
    
    //define next var
    var next =  active.next().length  ?  active.next()  :  $('.slideshow li:first');
    
    nextId = next.attr('class').split('-');
   //set active as last active
    active.addClass('last-active');
    //set next to active
    next.css({opacity: 0.0})
       .addClass('active')
       .animate({opacity: 1.0}, 800, function() {
	    $('#menu-sub li').removeClass('aniactive');
	    Cufon.refresh;
    	$('#menu-sub a.'+nextId[1]).parent().addClass('aniactive');
    	Cufon.refresh;
      	//make ie behave
      	$(this).removeAttr('style');
      	//remove classes from the active
          active.removeClass('active last-active');
       });
 }

function handleAboutMenu()
{
   //run the slider
   interval = setInterval('slideSwitchAbout()', 4000 );
   //when hovered ove menu item
   $('#menu-sub li a').mouseenter(function (){
     // break out of loop
     clearInterval(interval);
     
     //define id variables
     id = $(this).attr('class').split(' ');
     currentId = $('.slideshow li.active').attr('class').split('-');
        
        //if id is not the same as current active id
     if(id[1] != currentId[1]){
        $('#menu-sub li').removeClass('aniactive')
        $(this).parent().addClass('aniactive');
        //remove last active classes
        $('.slideshow li').removeClass('last-active');
        //show the hovered slide
        $('.slideshow li.n-' + id[1]).css({opacity: 0.0})
        .addClass('active-top')
        .animate({opacity: 1.0}, 200, function(){
           //remove actives
           $('.slideshow li').removeClass('active');
           Cufon.refresh();
           //make ie behave
           $(this).removeAttr('style');
           //set slide to active
           $(this).removeClass('active-top').addClass('active');
        });
     }
   }).mouseleave(function(){
     //re-initiate the loop from current position
     interval = setInterval('slideSwitchAbout()', 4000 );
   });
   
   
   // Ajax handler with click
   $('#menu-sub a').click(function(){
	   id = $(this).attr('href').split('/');
	   $.bbq.pushState('#'+id[1]);
	   return false;
   });
   
   
   // The haschange event is fired when history changes
   $(window).bind('hashchange', function(e)
   {
	   	// Get the URL after the #
	   	var url = $.param.fragment();
	   	
	   if (url != '')
	   {
		   //fetch link
		   href = $('#menu-sub a.'+url).attr('href');
		   if(href != undefined){
			   // get & set height of main
			   mainHeight = $('#main').height();
			   $('#main').css({'height' : mainHeight});
			   
			   //animate #content
			   $('#content').animate({'opacity' : 0.0}, 400, function(){
				   $('#ajax-loader').fadeIn(200);
				   // load #content with new content
				   $('#content').load(href, function(){
					   $('#ajax-loader').fadeOut(200, function(){
						   $('#menu-sub li').removeClass('active');
						   $('#menu-sub a.'+url).parent().addClass('active');
						   Cufon.refresh();
						   //get height of new #content and animate #main to that height, then show content
						   contentHeight = $('#content').height();
						   $('#main').animate({'height' : contentHeight+90},400, function(){
							   $('#content').animate({'opacity' : 1.0}, 400, function(){
								   $(this).removeAttr('style');
							   });
						   });
					   });
				   });
			   }); 
		   }
	   }
   });
   // Trigger the event the first time
   $(window).trigger('hashchange');
  
}

var imageCount = 0;
function handleNewsMenu()
{
   // Ajax handler with click
   $('#menu-news a').click(function(){
	   id = $(this).attr('href').split('/');
	   $.bbq.pushState('#'+id[1]);
	   return false;
   });
   
   // The haschange event is fired when history changes
   $(window).bind('hashchange', function(e)
   {
	   	// Get the URL after the #
	   	var url = $.param.fragment();
	   	
	   if (url != '')
	   {
		   //fetch link
		   href = $('#menu-news a.'+url).attr('href');
		   if(href != undefined){
			   // get & set height of main
			   mainHeight = $('#right').height();
			   $('#right').css({'height' : mainHeight});
			   
			   //animate #content
			   $('.content-news').animate({'opacity' : 0.0}, 400, function(){
				   $('#ajax-loader').fadeIn(200);
				   // load #content with new content
				   $('.content-news').load(href, function(){
				   	
				   	imageCount = $('.content-news img').length;
				   	// image preloader
						if (imageCount)
						{
							loadCount = 0;			
							$(".content-news img").one('load', function() {
								loadCount++;
								newsAniDown(url);	
							}).one('error', function(){ 
								loadCount++;
								newsAniDown(url);
							}).each(function() {
							  if(this.complete) $(this).load();
							});
						}
						else
							newsAniDown(url);
				   	
					   
				   });
			   }); 
		   }
	   }
   });
   // Trigger the event the first time
   $(window).trigger('hashchange');
  
}

function newsAniDown(url)
{
	$('#ajax-loader').fadeOut(200, function(){
	   $('#menu-news a h4, #menu-news a .intro').animate({'margin-left' : 0}, 400, function(){
		   $('#menu-news li').removeClass('active');
		   $('#menu-news a.'+url+' h4, #menu-news a.'+url+' .intro').animate({'margin-left' : 30}, 400, function(){
				   $('#menu-news a.'+url).parent().addClass('active');
		   });
	   });
	   Cufon.refresh();
	   //get height of new #content and animate #main to that height, then show content
	   contentHeight = $('.content-news').height();
	   $('#right').animate({'height' : contentHeight},400, function(){
		   $('.content-news').animate({'opacity' : 1.0}, 400, function(){
			   //make ie behave
	           $(this).removeAttr('style');
		   });
	   });
	});
}

function handleRefs()
{
	$('.container-reference li').click(function(){
		id = $(this).attr('class').split(' ');
		$('.container-referenceslide').scrollTo($('.container-referenceslide ul .'+id[0]), 1500, { easing:'easeInOutExpo' });
	});
}


// Twitter
function animateTweets()
{
   var current = $('.tweet ul li:nth-child(1)');
   var container = $('.tweet ul');
   if (container){
	  liheight =  $('.container-tweet').height();
	  //alert(liheight);
      container.animate({'margin-top' : -liheight}, function(){
         current.hide();
         container.animate({'margin-top' : 0}, 0);
         current.appendTo('.tweet ul');
         current.show();
      });
   }
}

function loadTweets(){
	$(".tweet").tweet({
		avatar_size: 32,
	    count: 8,
	    username: "extrapeekaa",
	    template: function(i){return i["text"]}
	});
	setInterval('animateTweets()', 5000);
}

