var subsections = ''; 
$(document).ready(function() {
	
	$('.scroll').each(function(){
		$(this).jScrollPane({scrollbarWidth : 11, dragMaxHeight: 122});
	});
	
	//bg
	if($('#bgimg').length > 0){
		var FullscreenrOptions = {  width: 1440, height: 900, bgID: '#bgimg', 'func': showlink	 };
		jQuery.fn.fullscreenr(FullscreenrOptions);
	}
	//vertical line
	moveMainLine();

	
	//main menu hover
	$('#mainmenu li a').css('color', '#FFFFFF');
	$('#bloglink a').css('color', '#000000');
	if($('#bio').length > 0){
		var subsections = $('.section').hide();
	}else{
		var subsections = $('.section');
	}		
	$('#mainmenu li').each(function(){
		$(this).data('active', 0);
		$(this).hover(
			function(){
				$(this).addClass('menuhover');
			},
			function(){
				if($(this).data('active') == 0){
					$(this).removeClass('menuhover');
				}
			}
		).find('a:first').click(function(e){
			if(!$(this).hasClass('external')){
				e.preventDefault();
				$(subsections).hide();
				$('#'+$(this).attr('rel')).show();
				/*
				moveMainLine();
				$('#bigline').show();
				*/
				$('#mainmenu li').data('active', 0);
				$(this).parents('ul').find('li').each(function(){
					$(this).removeClass('menuhover');
				});
				$(this).parent('li').addClass('menuhover');
				$(this).parent('li').data('active', 1);
			}
		});
		
	});
	
	//bio menu
	
	var biotexts = $('.biotext').hide();
	$('#biomenu ul li a').each(function(){
		$(this).click(function(e){
			e.preventDefault();
			$(biotexts).hide();
			$('#biomenu ul li').removeClass('active');
			$(this).parent().addClass('active');
			$('#'+$(this).attr('rel')).show();
		});
	});
	$('#biomenu ul li:first').find('a:first').click();
	
	//portfolio grid
	if($('#grid').length > 0){
		$('body').css({'overflow': 'auto','background':'none'});
		$('#bigline').remove();
		$('#mainmenu').css({'position':'relative', 'z-index':10, 'float':'left'});
		$('a.post-over').attr('title', '')
		$('.clientimage').each(function(){
			var clientlink = $(this).parent().find('.client-link:first').attr('href');
			$(this).parent().find('.post-over:first').attr('href', clientlink);
			$(this).after($('<a href="'+clientlink+'" class="clientgrid"></a>'));
			var srcsearch = $(this).attr('href');
			var parentcontent = $(this);
			var img = new Image();
			$(img).load(function () {
				$(this).hide();
				$(parentcontent).append(this);
				$(parentcontent).parent().find('a.post-over').data('loaded', 1).attr('title', '').hide();
				$(parentcontent).parent().css('backgroundImage', 'url(../)');
				$(this).fadeIn();
			}).error(function () {
			}).attr('src', srcsearch);
		});
		$('.client-link').remove();
		
		
		$('.post-over').css('opacity', 0);
		$('.post-over span').each(function(){
			var newtop = $(this).parent().height() - $(this).height();
			$(this).parent().css('paddingTop', newtop+'px');
		});
		$('.post-img').hover(
			function () {
				$(this).find('a.post-over').show();
				$(this).find('.clientimage:first').stop().hide();
			},
			function () {
				if($(this).find('a.post-over').data('loaded') == 1){
					$(this).find('a.post-over').hide();
					$(this).find('.clientimage:first').show();
				}
			}
		);
		//fix FF issue
		if($.browser.mozilla ) { $('.clientgrid').css('marginTop', '-160px'); }
		
		$('.post-img').each(function(){
			$(this).find('a.post-over').data('loaded', 0).stop().fadeTo('fast', 1);
		});
		
		$('#grid').masonry({
		  singleMode: true, 
		  itemSelector: '.post-img',
		  columnWidth: 200
		});
		$('#realBodyPorftolio').scroll(function(){
			moveBg();
		});

	}else{
		$(window).scroll(function(){
			$.scrollTo('0px');
		});
	}
	//client slider
	if($('#slider').length > 0){
		/*
		$('#slider').easySlider({auto:false, speed: 400, prevId: 'prevBtn', nextId: 'nextBtn', controlsShow: false});
		*/
		$('#slider').myGallery({ auto:false, speed: 700, prevId: 'prevBtn', nextId: 'nextBtn'});
		$('#scrollcontrols ul li').each(function(){
			$(this).hover(
			  function () {
			    $(this).addClass("controlhover");
			  },
			  function () {
			    $(this).removeClass("controlhover");
			  }
			 );
		});
		$('#closeliente').click(function(e){
			e.preventDefault();
		});
		
		$('.clientName span').each(function(){
			var newmargin = $(this).parent().height() - $(this).height() - 60;
			$(this).parent().find('h3:first').css('marginBottom', newmargin+'px');
		});
		
	}else{
		$('#bigline').css('z-index', 30);
		$('#bigline').hide();
	}
	
	$('.clientMainImg').each(function(){
		$(this).append($('<div class="clientMainImgDots" />'));
	});
	
	if($('#slider').length > 0){
		$('.clientName:first span').append('<div id="fb-root"></div><div id="shareclient"><fb:share-button class="meta"></fb:share-button></div>');
		$.getScript('http://connect.facebook.net/es_LA/all.js', function(){
			FB.init({appId: '100000841561903', xfbml: true, cookie: true});
		});
	}
	if($('#homeshare').length > 0){
		$('#homeshare').append('<div id="fb-root"></div><fb:share-button class="meta"></fb:share-button>');
		$.getScript('http://connect.facebook.net/es_LA/all.js', function(){
			FB.init({appId: '100000841561903', xfbml: true, cookie: true});
		});
	}

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


function showlink(){
//menu clicks
	var thisurl =  window.location.href;
	if(thisurl.search("&")){
		var subs = thisurl.split("&");
		if(subs[1] != ''){
			if($('#menu'+subs[1]).data('done') === undefined){
				// && ($('#menu'+subs[1]).data('done') != undefined)){
				$('#menu'+subs[1]).click();
				$('#menu'+subs[1]).data('done', 1);
			}
		}
	}
}

function moveBg(){
	$('#realBodyPorftolio').css('backgroundPosition', ' 0px '+(0-$('#realBodyPorftolio').scrollTop())+'px');
}

function controlGrid(){
	return;
	if($('#grid').outerWidth(true) > 1000){
		$('#grid').width(1000);
	}
}
function moveMainLine(){
	
	if($('#bigline2').length > 0){ 
		
		$('#bigline').css({
			'height': $(window).height()+'px',
			'marginRight':($(window).width() - 602)+'px'
		});
		$('#bigline2').css({
			'height': $(window).height()+'px',
			'marginRight':(394)+'px'
		});
		
	}else{
		if($('#bigline').length > 0){ 
			$('#bigline').css({
				'height': $(window).height()+'px',
				'marginRight':($(window).width() - 597)+'px'
			});
		}
	}
}
function updateBg(){
}

