// JavaScript Document
//łśćłżź
var DebugQueryMode=false; //Show Alerts
//var IE=false;

/* ================================================================================== */
/* =============================== jquery.ready [START] ============================= */
/* ================================================================================== */
/* =============================== page [START] ===================================== */
var GlobalPagePerm=1;
var GlobalPageAct='';
var GlobalPageRun='';

function jUnlockPage(ID){
	//alert('finished');
}

function jPageUnlockHome(){
	GlobalPageAct='home';
	GlobalPagePerm=1;
	$JQ('#topul_1').attr('class','active');
}
function jPageLock(){
	GlobalPageAct='';
	GlobalPagePerm=1;
	$JQ('#topul_1').attr('class','');
	$JQ('#topul_2').attr('class','');
	//path1
	if(GlobalPageRun=='info'){ 
		$JQ('#home').css('overflow','hidden');
		$JQ('#info').css('overflow','visible'); 
	}
	if(GlobalPageRun=='home'){
		$JQ('#home').css('overflow','visible');
		$JQ('#info').css('overflow','hidden'); 
	}
	//path1
	
	if(GlobalPageRun=='info'){  jPageShow('','info'); GlobalPageRun=''; }
	if(GlobalPageRun=='home'){ jPageShow('','home'); GlobalPageRun=''; }
	
	
	
}

function jPageUnlockInfo(){
	GlobalPageAct='info';
	GlobalPagePerm=1;
	$JQ('#topul_2').attr('class','active');
}




function jPageShow(This, ID){
	try{							 
		if(!ID || ID==''){ return false; }
		if(GlobalPagePerm!=1){ return false; }
		//var height=parseInt($JQ('#home').css('height'));
		//var display=$JQ('#'+ID).css('display');
		$JQ('#topul_1').attr('class','');
		$JQ('#topul_2').attr('class','');
		if(ID=='home'){ $JQ('#topul_1').attr('class','active'); }
		if(ID=='info'){ $JQ('#topul_2').attr('class','active'); }
		var height1=parseInt($JQ('#home').css('height'));
		var height2=parseInt($JQ('#info').css('height'));
		
		if(GlobalPageAct==''){
			if(ID=='home'){
				GlobalPagePerm=0;
				$JQ('#home').css('height','0');
				$JQ('#home').animate({'height': '+=200'},500,"swing",jPageUnlockHome);
				//$JQ('#topul_1').attr('class','active');
			}
			
			if(ID=='info'){
				GlobalPagePerm=0;
				$JQ('#info').css('height','0');
				$JQ('#info').animate({'height': '+=200'},500,"swing",jPageUnlockInfo);
				//$JQ('#topul_2').attr('class','active');
			}
			
			return;
		}
		
		if(GlobalPageAct=='home'){ $JQ('#home').animate({'height': '-=200'},500,"swing",jPageLock); if(ID!='home'){ GlobalPageRun=ID; } }
		if(GlobalPageAct=='info'){ $JQ('#info').animate({'height': '-=200'},500,"swing",jPageLock); if(ID!='info'){ GlobalPageRun=ID; } }
		
	}catch(err){ if(DebugQueryMode==true){ alert('jShowPage: '+err); }}
}
/* =============================== page [END] ======================================= */
/* =============================== .slider [START] ================================== */
var GlobaljDescAdd = new Array()
function jDescAdd(){
	try{
		var c_id;
		var elements=$JQ('.desc');
		for(i=0; i<elements.length; i++){
			c_id=$JQ('.desc').eq(i).attr('id');
			if(!c_id || c_id==''){
				$JQ('.desc').eq(i).attr('id','desc_'+i);
				$JQ('#desc_'+i).click(function () { 
					//return false; 
					var links=$JQ(this).find('.desc_img');
					if(links.length!=1){ return false; }
					var hrefs=$JQ(links).eq(0).attr('href');
					if(!hrefs){ return false; }
					var sign=hrefs.charAt(hrefs.length-1);
					//alert(sign);
					if(sign=='#'){ return false; }
					$JQ(links).eq(0).attr('target','_blank');
					$JQ(links).eq(0).onclick(); 
					//return false;
				});
				//alert($JQ('#desc_'+i+' .desc_ct').html());
				
				$JQ('#desc_'+i).hover(
				  function () {
					$JQ(this).find('.desc_ct').css('opacity','0');
					$JQ(this).find('.desc_ct').css('display','block');
					
					var elem=this.getElementsByTagName('div');
					var desc_height=parseInt(elem[0].offsetHeight);
					var img_height=parseInt(this.offsetHeight);
					
					var new_top=parseInt(img_height-desc_height);
					new_top=parseInt(new_top/2);
					
					$JQ(this).find('.desc_ct').css('top',new_top);
					$JQ(this).find('.desc_ct').animate({ opacity: 1 }, 300 );
				  }, 
				  function () { 
					$JQ(this).find('.desc_ct').animate({ opacity: 0 }, 200 );  
				  }
				);
			}//if
		}//for
	}catch(err){ if(DebugQueryMode==true){ alert('jDescSet: '+err); }}
}


function jDescLoad(ID){
	try{ 
		if(!ID || ID==''){ return false; }
		if(GlobalLinks.length<=0){ return false; }
		
		$JQ('#'+ID).empty();
		$JQ('#'+ID).html('<ul class="rideboxul" id="rideboxul"></ul>');
		
		for(i=0;i<GlobalLinks.length;i++){
			if(!GlobalLinks[i]){ alert('Error: #20'); return false; }
			$JQ('#rideboxul').append('<li id="rideboxul_'+i+'">&nbsp;</li>');
			GlobalLinksLoaded[i]=0;
		}
		$JQ('#rideboxul').css('width',GlobalLinks.length*1560);
		jDescLoadPageNext();
				
	}catch(err){ if(DebugQueryMode==true){ alert('jDescLoad: '+err); }}
}


function jDescLoadPageNext(){
	try{
		for(i=0;i<GlobalLinksLoaded.length;i++){
			if(GlobalLinksLoaded[i]==0){ jDescLoadPage(i);  break; }
		}
	}catch(err){ if(DebugQueryMode==true){ alert('jDescLoadPageNext: '+err); }}
}

function jDescLoadPagePrev(){
	try{
		for(i=GlobalLinksLoaded.length-1;i>=0;i--){
			if(GlobalLinksLoaded[i]==0){ jDescLoadPage(i);  break; }
		}
	}catch(err){ if(DebugQueryMode==true){ alert('jDescLoadPagePrev: '+err); }}
}

function jDescLoadPage(PageNum){
	try{
		PageNum=parseInt(PageNum);
		var Total=0;
		for(i=0;i<GlobalLinksLoaded.length;i++){
			if(GlobalLinksLoaded[i]==1){ Total+=1; }
		}
		//alert(Total);
		if(PageNum<0){ return false; }
		$JQ('#rideboxul_'+PageNum).html('<div class="ridebox_loading"></div>');
		
		
			$JQ.ajax({
			   url: GlobalLinks[PageNum],
			   //url: "portfolio/p_amica.html",
			   //cache: true,
			   success: function(msg){
					$JQ('#rideboxul_'+PageNum).html(msg);
					jDescAdd(); //load next item
					GlobalLinksLoaded[PageNum]=1;
					
					if(GlobalLinksActive==-1){
						var c_height=parseInt(document.getElementById('rideboxul_'+PageNum).offsetHeight);
						GlobalLinksActive=0;
						jDescActivator();
						jDescLoadPageNext(); //to delete
						jDescLoadPagePrev(); //to delete
						if(c_height==0){ return true; }
						$JQ('#ridebox').animate({ height: c_height }, 300 );
					}
			   },
			   error: function(msg) {
					$JQ('#rideboxul_'+PageNum).html('<div class="ridebox_error"><p style="padding-top:200px;">Item not found.</p></div>');
					GlobalLinksLoaded[PageNum]=1;
					return false;
			   }
			 });
		
		 
	}catch(err){ if(DebugQueryMode==true){ alert('jDescLoadNext: '+err); }}
}


var GlobalLinksActive=-1;
var GlobalLinksLoaded=new Array();
var GlobalLinks=new Array();
/*
GlobalLinks[0]="portfolio/p_amica.php";
GlobalLinks[1]="portfolio/p_maximasque.php";
GlobalLinks[2]="portfolio/p_test2.php";
GlobalLinks[3]="portfolio/p_test1.php";
GlobalLinks[4]="portfolio/p_osem.php";
*/



var global_jslider_perm=1;

function jDescItemDirection(direction, status){
	try{
		
		jDescLoadPageNext(); //to delete
		jDescLoadPagePrev(); //to delete
		
		if(!status){ global_jslider_perm=0; }
		direction=direction.toLowerCase();
		
		var slide, slides_total;
		var slide_width=0;
		var slide_active=-1;
		
		var left=parseInt($JQ('#rideboxul').css('left'));
		
		for(var i=1; i<=GlobalLinksLoaded.length; i++){
			slide=parseInt($JQ('#rideboxul li:nth-child('+i+')').css('width'));
			if(slide>0 && i==1){ slide_width=slide; }
			if(slide_width*(i-1)==left*(-1)){ slide_active=i; }
			if(slide.toString().toLowerCase()=='nan'){ break; }
		}
		slides_total=i-1;
		
		if(slide_active==-1){ return false; }
		//alert(slides_total + ' ' +slide_active);
		if(slides_total==slide_active && direction=='next'){ 
			$JQ("#rideboxul").animate({ left: "0"}, 200, "swing",  function () { 
				/*
				var c_height=parseInt(document.getElementById('rideboxul_0').offsetHeight);
				//jDescArrows(c_height);
				$JQ("#ridebox").animate({ height: c_height}, 100, "linear");
				*/
			}); 
			return true; 
		}
		if(slide_active==1 && direction=='prev'){ 
			$JQ("#rideboxul").animate({ left: (-1)*slide_width*(slides_total-1)}, 200, "swing",  function () {
				/*
				var slide_nextnum=slides_total-1;
				var c_height=parseInt(document.getElementById('rideboxul_'+slide_nextnum).offsetHeight);
				//jDescArrows(c_height);
				$JQ("#ridebox").animate({ height: c_height}, 100, "linear");
				*/
			});  
			return true;
		}
		else{
			if(direction=='next'){
				var position=slide_width*(slide_active)*(-1);
				$JQ("#rideboxul").animate({ left: position}, 200, "swing",  function () {  
      				//var position=slide_width*(slide_active)*(-1);
					//$JQ("#rideboxul").animate({ left: position}, 350 ); 
				/*	
					var slide_nextnum=slide_active;
					var c_height=parseInt(document.getElementById('rideboxul_'+slide_nextnum).offsetHeight);
					//jDescArrows(c_height);
					$JQ("#ridebox").animate({ height: c_height}, 100);
					*/
					
    			});
			}
			if(direction=='prev'){
				var position=slide_width*(slide_active-2)*(-1);
				if(position>0){return false; }
				
				$JQ("#rideboxul").animate({ left: position}, 200, "swing",  function () {
					 /*
					var slide_nextnum=slide_active-2;
					var c_height=parseInt(document.getElementById('rideboxul_'+slide_nextnum).offsetHeight);
					//jDescArrows(c_height);
					$JQ("#ridebox").animate({ height: c_height}, 100);
					*/
    			}); 
			}
			return true; 
		}
		
	}catch(err){ if(DebugQueryMode==true){ alert('jDescItemDirection: '+err); }}
}


function jDescArrows(Height){
	try{
		var c_pgtop=parseInt($JQ('#ride').css('padding-top'));
		var c_height=parseInt($JQ('#ride_prev').css('height'));
		
		var new_top=parseInt((Height-c_height)/2);
		new_top+=c_pgtop;
		
		$JQ("#ride_prev").animate({ top: new_top}, 150);
		$JQ("#ride_next").animate({ top: new_top}, 150);
		/*var c_pgbottom=$JQ('#ride').css('padding-bottom');*/
			
		//var c_top=$JQ('#ride_prev').css('top');
		//alert(c_pgtop + ' - ' +c_pgbottom);
	}catch(err){ if(DebugQueryMode==true){ alert('jDescArrows: '+err); }}
}



function jDescActivator(){
	try{ 
		var left=parseInt($JQ('#rideboxul').css('left'));
		for(var i=1; i<=GlobalLinksLoaded.length; i++){
			slide=parseInt($JQ('#rideboxul li:nth-child('+i+')').css('width'));
			if(slide>0 && i==1){ slide_width=slide; }
			if(slide_width*(i-1)==left*(-1)){ slide_active=i-1; }
			if(slide.toString().toLowerCase()=='nan'){ break; }
		}
	
		var c_height=parseInt(document.getElementById('rideboxul_'+slide_active).offsetHeight);
		//alert(c_height);
		//alert('slide_active:'+slide_active+ ' c_height:'+c_height);
		if(c_height==0){ c_height=530; }
		jDescArrows(c_height);
		$JQ("#ridebox").animate({ height: c_height}, 150, "swing",  function () {
				self.setTimeout("jDescActivator()", 50);
		}); 
		
		
	}catch(err){ if(DebugMode==true){ alert('DebugQueryMode: '+err); }}
}



/* =============================== .slider [END] ==================================== */

$JQ('.jhover').hover(
  function () {
	$JQ(this).animate({ opacity: 0.7 }, 200 );
  }, 
  function () {
	$JQ(this).animate({ opacity: 1 }, 200 );
  }
);
/* ================================================================================== */
/* =============================== jquery.ready [END] =============================== */
/* ================================================================================== */

