// JavaScript Document
if (typeof(jQuery)=='function'){
	jQuery.ajaxSetup({cache:true});
	jQuery.getScript('/js/menu.js');				
}
else {
	console.log('error loading jquery');	
}

jQuery(document).ready(function (){
jQuery.extend({
		 NvGaleria:{
			 	 
				 data:[],
				 
				 index:0,
				 
				 limit:0,
				 
				 start:function(){
					this.loadData();
					this.addClickAction();
					this.isPositionSet();
				 },
				 
				 loadData:function(){
					 if (typeof(jsObj) == 'object'){
						 this.data=jsObj;
						 this.limit=this.data.length -1;
					 }
				 },
				 
				 isPositionSet:function(){
				 	var tmp=[];
					if (document.location.href.indexOf('#') >0){
						tmp=document.location.href.split('#',2);
						if (tmp[1].length >0 ){
							var newIndex=0;
							jQuery(this.data).each(function(index){															
								if (decodeURIComponent(this.title.toLowerCase()) == decodeURIComponent(tmp[1].toLowerCase())){							
									newIndex=index;
									return false;
								}
							});
							if (0!=newIndex){
								this.index=newIndex;
							    this.change();
							}
						}
					}
				 },
				 
				 addClickAction:function (){
					 jQuery('#prev').click(function(){
						jQuery.NvGaleria.prev();
						return false;				
					  });
					 
 					 jQuery('#next').click(function(){
                        jQuery.NvGaleria.next();
						return false;				
					  });
					 
					 jQuery('.nv_trigger').click(function(){
						var id=jQuery(this).attr('id').split('_');
						 jQuery.NvGaleria.set(id[1]);
						 return false;
					});
				 },
				 
				 change:function (){
					 
					 var id_b=this.data[this.index].big;
					 var id_balt=decodeURIComponent(this.data[this.index].bigalt);
					 
					 var id_s=this.data[this.index].small;
					 var id_salt=decodeURIComponent(this.data[this.index].smallalt);
					 
					 var id_title=decodeURIComponent(this.data[this.index].title);
					 var id_text=decodeURIComponent(this.data[this.index].text);
					 
					 if (id_balt==''){id_balt=id_title;}
 					 if (id_salt==''){id_salt=id_title;}
					 
					  jQuery('<img>').load(function(){
					  	jQuery('<img>').load(function (){
																									   
							jQuery('#nv_bigimg').fadeOut("slow",function (){ 
								jQuery('#nv_bigimg').attr({'src':'/multimedia/'+ id_b +'/475x400/','title':id_balt,'alt':id_balt}).fadeIn("slow");
							});
							
							jQuery('#nv_smallimg').fadeOut("slow",function (){ 
								jQuery('#nv_smallimg').attr({'src':'/multimedia/'+ id_s +'/200x115/','title':id_salt,'alt':id_salt}).fadeIn("slow");
							});
							
							
							jQuery('#nv_title').fadeOut("slow",function (){ 
								jQuery("#nv_title").html(id_title).fadeIn("slow");
							});
						    
						
							jQuery('#nv_text').fadeOut("slow",function (){ 
								jQuery("#nv_text").html(id_text).fadeIn("slow");
							});
							 							
						}).attr({'src':'/multimedia/'+ id_s +'/200x115/'});
					   }).attr({'src':'/multimedia/'+ id_b +'/475x400/'});
					 
					 if (typeof(pageTracker)=='object'){pageTracker._trackPageview();}				     
				 },
				 
				 next:function (){
					 this.index++;
					 if (this.index>this.limit){this.index=0;}
					 this.change();
				 },
				 
				 prev:function (){
 					 this.index--;
					 if ( this.index<0){ this.index= this.limit;}
					 this.change();
				 },
				 
				 set:function (i){					 
					  if (( i>=0 ) && (i<=this.limit)){ this.index=i;}
					  this.change();
				 }
				 
			 }
		 });	

jQuery.NvGaleria.start();
});
