function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function deleteCookie(name) {
    setCookie(name,"",-1);
}

						   
$(document).ready(function() {	   

	$('.inside').hide();					   
	$('.bioimg').hide();					   
	$( '.contactimg').hide();	
	
	var splash_imgs = new Array();
	
	splash_imgs[0] ='<img src="http://nicocamargo.com/images/installation_fullscreen.jpg" />';	
	splash_imgs[1] ='<img src="http://nicocamargo.com/images/highway6_fullscreen.jpg" />';
	splash_imgs[2] ='<img src="http://nicocamargo.com/images/space_fullscreen.jpg" />';
	splash_imgs[3] = '<img src="http://nicocamargo.com/images/europa_fullscreen.jpg" />';	
	splash_imgs[4] = '<img src="http://nicocamargo.com/images/map2_fullscreen.jpg" />';	
	splash_imgs[5] = '<img src="http://nicocamargo.com/images/pinkcity_fullscreen.jpg" />';	
	splash_imgs[6] = '<img src="http://nicocamargo.com/images/mars_fullscreen.jpg" />';	

	var rand =  Math.floor ( Math.random() * splash_imgs.length );

	$('.splash_holder').html( splash_imgs[ rand ] );	
	$('.splash')	.fadeIn('slow',  'swing');
	
	$('.zoom-out-btn').mouseover( function(){		
		$(this).attr('src', 'http://nicocamargo.com/images/zoom_out_btn_over.gif');
	});
	
	$('.zoom-out-btn').mouseout( function(){		
		$(this).attr('src', 'http://nicocamargo.com/images/zoom_out_btn.gif');
	});

	$('.enter').mouseover( function(){	
		$(this).find('img').attr('src', 'http://nicocamargo.com/images/enter_over.png');	
	});
	
	$('.enter').mouseout( function(){		
		$(this).find('img').attr('src', 'http://nicocamargo.com/images/enter.png');
	});
	
	$('.inicie').mouseover( function(){		
		$(this).find('img').attr('src', 'http://nicocamargo.com/images/inicie_over.png');
	});
	
	$('.inicie').mouseout( function(){		
		$(this).find('img').attr('src', 'http://nicocamargo.com/images/inicie.png');
	});
		
		
	$('.enter, .inicie, .enter_name').click( function(){
			$('.splash')	.fadeOut(1000);
			$('.inside').fadeIn(4000);
			setCookie('hidesplash', 1, 1);		
	});
	
	
	/////////////// tooltip
	
	var zoomtooltip = function(){
	//Select all anchor tag with rel set to tooltip
	$('.tooltip img').mouseover(function(e) {
		
	if(zoomed!=true){		
		var testvb = (e.pageX-$(this).parent().offset().left);
	
		$('#tooltip').css('top', e.pageY -150);
		$('#tooltip').css('left',  testvb  );
		
		//Append the tooltip template and its value
		$(this).parent().append('<div id="tooltip"><img src="../images/zoom_hover_btn.png"></div>');		
				
		//Show the tooltip with faceIn effect
		$('#tooltip').fadeIn(800);
	}
	}).mousemove(function(e) {
		if(zoomed!=true){		
		var testvb = (e.pageX-$(this).parent().offset().left);
		//Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse
		$('#tooltip').css('top', e.pageY-150 );
		$('#tooltip').css('left', testvb  );
		}
	}).mouseout(function() {	
		$('#tooltip').fadeOut(500);	
	});	
	//////end tooltip	
	}
	
	
	
	
	
	
	
	
	
	
		setTimeout(function(){ zoomtooltip(); }, 5000);
		setTimeout(function(){ $( '.text').fadeIn('slow', 'swing');	}, 5000);
						   
		/////global vars
		var winwidth = $(window).width();
		var winheight =$(window).height();
		var zoomed = false;
		var zoomed_img;
		var orig_width;
		var orig_height;
		var orig_left;
		var left_pos;
		var top_pos;
		var info_div ;
		var next_img ;
		var martop;
		var marleft;
		var orig_src;
		var lg_src;
		var mask_width;
		var not_docready=false;
		var current_piece = '#item1';	
		
		//$('#wrapper').scrollTo($(current_piece).attr('href'), 100);	
		
		var resizePanel = function(){
			winwidth = $(window).width();
			winheight = $(window).height();
		
			mask_width = winwidth * $('.item').length;
				
			$('#wrapper, .item').css({width: winwidth, height: winheight});
			$('#mask').css({width: mask_width, height: winheight});
			
			if( not_docready==true){
				$('#wrapper').scrollTo($('a.selected').attr('href'), 0);		
			}			
		
		}/////// end resize panel func
		
		
	resizePanel();
	
	$('a.navitem').mouseover(function () {
			$('.hover_title_box').fadeIn('slow', 'swing');
			$('.hover_title_box').html( $(this).attr('hover_title'));		 								 
	});
	
	$('a.navitem').mouseout(function () {
			$('.hover_title_box').fadeOut('slow', 'swing');
			$('.hover_title_box').html('');		 								 
	});
	
	
	// nav squares click to advance to certain piece			
	$('a.panel').click(function () {
		$( '.piece').show();				
		$( '.contactimg').hide();
		$( '.bioimg').hide();
		
		current_item = $(this).attr('href');
		zoomOut();
		
		$('.selected').removeClass('selected');  				
		$(this).addClass('selected');
		$('a.biopanel').removeClass('selected');
		$('a.contactpanel').removeClass('selected');
		
		$('#wrapper').scrollTo( current_item, 400);	
		return false;
	});///////////// END PANEL CLICK
	
	
	
	// ABOUT PAGE LINK	
	$('a.biopanel').click(function (e) {
		e.preventDefault();	
		
		$( '.contactimg').hide();
		$( '.bioimg').hide();
		zoomOut();
		
		$('a.biopanel').removeClass('selected');
		$('a.contactpanel').removeClass('selected');
		$('.selected').removeClass('selected');  				
		$(this).addClass('selected');

		current_item = $(this).attr('href');
		$('#wrapper').scrollTo( current_item, 400);
		
		setTimeout(function(){ $( '.bioimg').fadeIn('slow', 'swing');	}, 1500);		
		return false;
	});///////////// END  CLICK
	
	
	
		// ABOUT PAGE LINK	
	$('a.contactpanel').click(function (e) {
		e.preventDefault();	
		
		$( '.bioimg').hide();
		$( '.contactimg').hide();
		zoomOut();
		
		$('a.biopanel').removeClass('selected');
		$('.selected').removeClass('selected');  				
		$(this).addClass('selected');

		current_item = $(this).attr('href');
		$('#wrapper').scrollTo( current_item, 400);		
		setTimeout(function(){ $( '.contactimg').fadeIn('slow', 'swing');	}, 1500);
		
		return false;
	});///////////// END  CLICK
	
	
		
		
	
	
	$(window).resize(function () {
		not_docready=true;
		resizePanel();
	});
	
		
	$('.palette').each(
		function(){
			$(this).hide();
		}
	);



	
	var zoomOut = function(){	
		if(zoomed==true){		
			info_div.fadeOut(300);
			$(zoomed_img).css('cursor', 'pointer');			
			$(zoomed_img).animate({
				height: orig_height+'px',
				width: orig_width+'px',
				'left': 0+'px',
				'top': '0px',
				'margin-top': '0px',
				'margin-left': '0px',
				'z-index': 0
			}, 700, 'linear');		
			$(zoomed_img).unbind('draggable');
			
			zoomed=false;
			zoomed_img= '';
		}//end if zoomed	
	}/////////////end zoomOut function




	//// palette strip zoom out button
	$('.zoom-out-btn').click( function(){
		zoomOut();	
	}); //end click


  // all portfolio images: zoom function
  $('.image_zoom').each( function(){
	$(this).click( function(){
		 zoomed_img = this;	 
		 
		 
		if(zoomed==false){
			$(this).css('cursor', 'move');	
			$('#tooltip').fadeOut();
			
			orig_width = $(this).attr('width');
			orig_height = $(this).attr('height');
			orig_left = $(this).attr('left');		
	
			zoomWidth = (orig_width*2);
			zoomHeight = (orig_height*2);
			
			var piece_num = $(this).attr('id');			
			info_div = $('.info_'+piece_num);
			next_img = $('.img_'+(parseInt(piece_num)+1));
	
			$(this).animate({
				height: zoomHeight+'px',
				width: zoomWidth+'px',
				'margin-top': '-'+(orig_height/2)+'px',
				'margin-left': '-'+(orig_width/2)+'px'
				}, 900, 'linear');
			  $(this).draggable( { cursor: 'move'});
			  info_div.fadeIn('slow');		  
			  zoomed = true;
			
		}else{
			$(this).unbind('draggable');
			//$(this).unbind('mouseover');
			//$(this).unbind('mouseout');
		} //end if zoomed or not	
		}); //end click
   }); //end each image
  
  
}); ///end doc ready
