// JavaScript Document
var $ = jQuery.noConflict();
$(document).ready(function(){

	
	$(".form").validate();
	$('.form').each(function() {
		$(this).validate();
	});


	$('.dot_big_image').click(function(){
		window.location.href = './';
	});
	
	$('.image_container > div').hover(function(){
		
			$(this).css({ borderColor: '#0f5082'});
		
		},function(){
		
				$(this).css({ borderColor: '#CCC'});
		
			});
	
	$('.header_letters ul li[list_value]').bind("mouseenter",function(){
		$('.keyword > div').html($(this).attr('list_value'));
	});
	
	$('.recent_work1, .recent_work2').click(function(){
		
		var thisID = $(this).attr('p_id');
		var types = $(this).attr('type');
		showPopUp(types,thisID);
	
	});
	
	
	$('.click_work').click(function(e){
		//console.log('clicked');
		var thisIndex = $(this).attr('ti');
		var thisClicked = $('.click_work').index(this);
		var heightContent = $('.scroll-content:eq('+thisIndex+')').css('height').replace('px','');
		var acid = $('.scroll-content:eq('+thisIndex+')').attr('acid');
		var thisID = $(this).attr('p_id');
		var types = $(this).attr('type');
		var gridI = $('.statusBar').html();
		$.post("/library/includes/views/popup_portfolio_i.php", { id: thisID, type: types, grid: gridI } , function(data){
			//if(heightContent < 200 || acid == "true"){
				if(acid == "true"){
					$('.scroll-pane:eq('+thisIndex+') style, .scroll-pane:eq('+thisIndex+') .detail_pp').remove();
					$('.scroll-pane:eq('+thisIndex+')').prepend(data);
				}else{
					$('.content_wrapper').css({
											  display: 'block',
											  overflow: 'visible',
											  height: '675px'
											  });
					$('.scroll-content:eq('+thisIndex+')').attr('acid',"true");
					$(data).prependTo('.scroll-pane:eq('+thisIndex+')');
				}
			//}
			//		console.log(data);
			//$('.detail_pp:eq('+thisIndex+') .pics').html(data);
		});
		//console.log(e.pageX);
		fadeAway(thisClicked);
	});
	
	function fadeAway(id){
		
		$('.click_work:not(:eq('+id+'))').fadeTo('fast',0.2);
		$('.click_work:not(:eq('+id+'))').css('backgroundColor','#000');
		$('.click_work:eq('+id+')').css('backgroundColor','#fff');
		$('.click_work:eq('+id+')').fadeTo('fast',1);
		
		if(id > 0){
			var prev_id = id-1;
		}else{
			var prev_id = $('.scroll-content-item').size()-1;
		}
		
		if(id < $('.scroll-content-item').size()-1){
			var next_id = id+1;
		}else{
			var next_id = 0;
		}
		
		$('.next_btn').attr('sid', next_id);
		$('.prev_btn').attr('sid', prev_id);
		//console.log(next_id)
		
			var posX = Math.round(next_id*100/$('.scroll-content-item').size());
			if(id<2){
				posX = 0+'px';
			}else{
				posX = posX+'%';
			}
			$(".ui-slider-handle").css({ left: posX });
			//$(".scroll-bar").slider("slide", [posX]);
			//console.log(id);
			
			if( $('.scroll-content').width() > $('.scroll-pane').width()){ 
				var itemWidth = $('.scroll-content').width() / $('.scroll-content-item').size();
				var scrollLeft = Math.round( (id * itemWidth - ( $('.scroll-content-item').width() + ( $('.scroll-content-item').width()/2 ) ) ));
				$('.scroll-content').css('margin-left','-'+scrollLeft+'px'); 
				//console.log(scrollLeft);
			} else { 
				$('.scroll-content').css('margin-left', 0); 
			}
			
			
	}
	
	
	
	
	

	$('.close_btn_wrapper').stop().fadeTo('slow', 0.5);
		
	function showPopUp(type_popup,thisID){
		
		var pwidth = $('body').width();
		var pheight = $('body').height();
		
		
		$('.fader').animate({
			width: pwidth,
			height: pheight
		}, 100).stop().fadeTo('slow', 0.5);
		
		//switch(type_popup){
		//	case "identities":
				$('.popup_portfolio').fadeTo('fast', 1);
				$.post("/library/includes/views/popup_portfolio.php", { id: thisID, type: type_popup } , function(data){
					$('.popup_portfolio').html(data);
				});
		//	break;
		//}
	
	}
	
	$('.popup_portfolio').bind("mouseenter", function(){
		$('.fader').stop().fadeTo("slow", 0.5);
		$(this).stop().fadeTo("slow", 1);
		$('.close_btn_wrapper').stop().fadeTo('slow', 0.5);
	});
	
	$('.fader').bind("mouseenter", function(){
		$(this).stop().fadeTo("slow", 0.3);
		$('.popup_portfolio').stop().fadeTo("slow", 0.9);
		$('.close_btn').stop().fadeTo('slow', 1);
	}).click(function(){
		$('.popup_portfolio').hide();
		$(this).hide();
	});
	
	
	$('.scroll-content').each(function(){
	
		var countItems = $('> * .click_work', this).size();
		var widthContent = eval((countItems * 128) + (countItems * 23)) + 'px';  
		$(this).css({ width: widthContent });
	
	});
	
	$('table.cv tr.info').hover(function(){
		
		$(this).css({ backgroundColor: '#f3f3f3' });
		$(this).prev().css({ backgroundColor: '#f3f3f3' });
		$(this).next().css({ backgroundColor: '#f3f3f3' });
	
	},function(){
		
		$(this).css({ backgroundColor: '#fff' });
		$(this).prev().css({ backgroundColor: '#fff' });
		$(this).next().css({ backgroundColor: '#fff' });

	});
	
	var el = $('[spam_protection=true]');
	el.each(function(){
	
		$(this).attr('href',$(this).attr('href').replace('/','mailto:').replace('|','@').replace(';','.'));
		$(this).html($(this).html().replace('|','@').replace(';','.'));
	
	});
	
	

});
