/*Main variables*/
var isiPad = navigator.userAgent.match(/iPad/i) != null;
var mediaServerLocation = 'http://bobo1.tempsite.ws/';
var serverLocation = location.host == 'localhost' ? location.protocol + '//' + location.host + '/smart/boboinstitucional/www/': location.protocol + '//' + location.host + '/institucional/';
var shadowSize = 5;
var labels;
var user;

var windowWidth;
var windowHeight;

$(document).ready(function()
{
        //if (isiPad) document.ontouchmove = function(e){e.preventDefault();}
        placeMainElements();
});

$(window).resize(function() {placeMainElements()} );


function placeMainElements()
{
        windowWidth = $(window).width();
        windowHeight = $(window).height();
        
        if (windowWidth < 1000) windowWidth = 1000;
        if (windowWidth > 1440) windowWidth = 1440;
        if (windowHeight < 750) windowHeight = 750;
        if (windowHeight > 808) windowHeight = 808;
        
        $('#wrapper').css("width", windowWidth);
        $('#wrapper').css("height", windowHeight);
        $('#wrapper nav').css("height", windowHeight);
        $('#wrapper').css("marginLeft", -windowWidth/2);
        $('#wrapper').css("marginTop", -windowHeight/2);
	
	$('section').css("width", windowWidth - 242);
        $('section').css("height", windowHeight);
	
	$('#menu-rights').click(function() 
	{ 
		closeTabs();
		var el = $('#rights-content');
		el.addClass('open'); 
		el.animate( {left: 0, queue: true}, 200 ); 
	})
	
	$('#menu-work').click(function() 
	{ 
		closeTabs();
		var el = $('#work-content');
		el.addClass('open'); 
		el.animate( {left: 0, queue: true}, 200 ); 
	})
	
	$('#menu-support').click(function() 
	{ 
		closeTabs();
		var el = $('#support-content');
		el.addClass('open'); 
		el.animate( {left: 0, queue: true}, 200 ); 
	})
	
	$('#menu-newsletter').click(function() 
	{ 
		closeTabs();
		var el = $('#newsletter-content');
		el.addClass('open'); 
		el.animate( {left: 0, queue: true}, 200 ); 
	})
	
	$('.tab-button-close').click(function()
	{
		$(this).parent().animate( {left: -(parseFloat($(this).parent().css("width")) + parseFloat($(this).parent().css("padding-left")) + parseFloat($(this).parent().css("padding-right")) + shadowSize), queue: true}, 200 );
		$(this).parent().removeClass('open');
	})
	
}


/*General use methods*/
function closeTabs()
{
	//$('.open').animate({left:-650}).removeClass('open');
	$('.open').animate({left:-650}, 100, function(){
		$(this).removeClass('open');
	});
}

function getParameterByName(name)
{
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.href);
        if(results == null)
                return "";
        else
                return decodeURIComponent(results[1].replace(/\+/g, " "));
}

// Read a page's URL segments and return them as an associative array.
function getUrlSegments()
{
	var vars = [];
	var hashes = window.location.href.slice(window.location.href.indexOf('http://') + 7).split('/');
	for(var i = 0; i < hashes.length; i++)
	{
		vars.push(hashes[i]);
	}
	return vars;
}

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++)
	{
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}

function fitToStage(image)
{
        var imageWidth = parseInt($(image).css("width"));
        var imageHeight = parseInt($(image).css("height"));
        var propX = imageWidth / $(window).width();
	var propY = imageHeight / $(window).height();
	propX > propY ? fitStageHeight(image) : fitStageWidth(image);
}

function fitStageWidth(image)
{
        var imageWidth = parseInt($(image).css("width"));
        var imageHeight = parseInt($(image).css("height"));
	var proportion = $(window).width() / imageWidth;
	$(image).css("width", $(window).width());
	$(image).css("height", imageHeight * proportion);
}

function fitStageHeight(image)
{
        var imageWidth = parseInt($(image).css("width"));
        var imageHeight = parseInt($(image).css("height"));
	var proportion = $(window).height() / imageHeight;
        $(image).css("height", isiPad ? 692 : $(window).height());
	$(image).css("width", imageWidth * proportion);
}

