/*actions for work page*/


$(document).ready(function() {

	
	//menu actions
	
	$('a img, form img').hover(
	 			
	 			function()
	 			{ this.src = this.src.replace("_off","_on"); },
				
				function() 
				{ this.src = this.src.replace("_on","_off"); }
	
	);
	
	
	/*--------changing things---------------*/
	
	
	/*---- hiding checkboxes ----*/
	
	$('[type^="checkbox"]').hide();
	
	/*---- hiding submit button ----*/
	
	$('#show').hide();
		
	/*---- creating the info box and things for lightbox ----*/
	
	$('body').prepend('<div id="viewer"></div><div id="overlay"></div><div id="lightbox"><div id="close-lightbox"><img src="images/cancel_tag.png" alt="close"/><p>click anywhere to close</p></div><img src="" alt="" id="work"/></div>');
	
	$('#viewer').hide().css({position: 'absolute', top: '200px', left: '50%'});

	$('#overlay, #lightbox').fadeTo(0, 0);
	
	/*---- behaviour for hiding lightbox ----*/

	$('#overlay, #lightbox').click(function() {

		$('#overlay, #lightbox').fadeTo("fast", 0); 
		
		$('#overlay').css({width: '1px', height:'1px' });
		
		$('#work').attr('src', '').attr('alt', '');
	
	});

	
	/*array for tags status*/

	var tag_array = new Array();
	
	var tag_amount = $('.tag').length;
	
	for (var i=1; i<=tag_amount; i++){
	
		tag_array[i]=false;
	
	};
	
	//function for getting the mouse position

	document.onmousemove = mouseMove;
	
	var mousePos = '';

	var winW = document.body.clientWidth;
	
	var winH = document.body.clientHeight;
		
	function mouseMove(ev){
		ev = ev || window.event;
		mousePos = mouseCoords(ev);
		
		if (mousePos.x < winW/2){
		
			left = mousePos.x -100;
		
		}else{
		
			left = mousePos.x -300;
		
		}
		
		$('#viewer').css({top: mousePos.y + 10, left: left});
	}
	
	function mouseCoords(ev){
	
		ev = ev || window.event;
	
		if(ev.pageX || ev.pageY){
			return {x:ev.pageX, y:ev.pageY};
		}
		return {
			x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
			y:ev.clientY + document.body.scrollTop  - document.body.clientTop
		};
	}
	
	/*---- behaviours for thumbs ----*/
	
	$('.thumb').livequery(
	
		function(){
		
			/* styling thumbs */
		
			$('.thumb').css({width: '100px', height: '100px', overflow: 'hidden'});
	
			$('.img-info').hide();
			
			/* hovering thumbs */
			
			$(this).hover(
				
				function(){
					
					$('#viewer').empty().append('<div id="thumb-info"><div class="text"></div><div class="clear"><div></div></div></div>');
					
					$(this).find('img').clone().prependTo('#thumb-info');
					
					$(this).find('h4, span, p').clone().appendTo('#thumb-info .text');
					
					$('#viewer').show().find('.img-info').show();
				
				},
				
				function(){
				
					$('#viewer').empty().hide();

				}
			
			);
			
		}
	
	);
	
	/* clicking thumbs */
	
	$('.thumb a').livequery ('click', function () {
			
			if($(this).parents('.thumb').find('.img-info').find('a').size() > 0){
			
				var url = $(this).parent().parent().find('p').find('a').attr('href');
				
				window.location.href= url;
									
				return false;
			
			}else{
			
				var img_url = $(this).attr('href');
		
				overlayIn(img_url);
				
				return false;
			
			}
			
		}
	
	);
	
	$('.thumb a').find('a')

	/* event for loading complete lightbox */
	
	$('#work').load(function() {
	
		var left = -( $('#work').width() / 2 );
		
		var top = getScrollTop();
		
		$('#lightbox').css({ 'margin-left': left + 'px', 'top': top+'px' });
		
		$('#lightbox').fadeTo(300, 1);
	
	});
				
	/* setting variables */
	
	var loading = false;

	var selected_tags = 0;
	
	/* behaviours for tags */
	
	$('.tag').live('click', function () {

		var tag_id = $(this).attr('id');
		
		var tag_id = tag_id.substring(3);
			
		if (!$(this).hasClass('on')){
		
			$('.tag').removeClass('on').css('background', 'none');
		
			$(this).addClass('on');
							
			loadingWorks (tag_id);
			
			$(this).css('background','url(images/tick.png) 10px center no-repeat');
					
		}

	});
	
	/* decorando el scroll */
	
	$('#showcase-cont').jScrollPane({
	
		showArrows : true,
	
	});
	
$('a.legal').click(function(){

$('.popup').load('legal.php', function(){$('.popup').css('visibility','visible')});

return false;

});

$('.popup').click(function(){ $(this).css('visibility','hidden')});
	
	
});//here ends document.ready

/*---- lightbox function ----*/

function overlayIn (img) {

	var scroll_top = getScrollTop ();

	$('#overlay').css({'width': $('html').width(), 'height': $('html').height()}).fadeTo("slow", 0.66, 

	function(){
	
		$('#work').attr('src', img).attr('alt', img);
		
		
	}); 

}

/*loading works*/

function loadingWorks (tag) {

	$('#showcase').empty();
		
	$('#showcase').ajaxStart(function (){
	
		loading = true;
	
		$(this).fadeTo(300, 0.5).after('<div id="loader"><img src="images/clock.gif"/></div>');
	
	});
		
	$('#showcase').ajaxStop(function (){
	
		loading = false;
		
		$(this).next().remove();
		
		$(this).fadeTo(200, 1);
		
	});
			
	$.post('work_loader.php', {'selected-tag': tag}, function (data) {
		
		$('#showcase').html(data);
			
	});

};

/* function for detecting scrollTop */

function getScrollTop(){
    if(typeof pageYOffset!= 'undefined'){
        //most browsers
        return pageYOffset;
    }
    else{
        var B= document.body; //IE 'quirks'
        var D= document.documentElement; //IE with doctype
        D= (D.clientHeight)? D: B;
        return D.scrollTop;
    }
}
