// JavaScript Document
var variavelControl = 0 ;

jQuery.extend({
		 ArtigoGaleria:{
			 	 
				 data:[],				 
				 index:0,				 
				 limit:0,

				 Amp: 0,
			 	 Ah:0,
				 images:[],
				 
				 free:false,

				 start:function(){
					if (typeof(ObjectArtigoFotosPlus) == 'object'){
						this.loadData();
                                                this.loadImages();
                                                this.configLabel();
                                                this.addClickAction();
					  }
				 },
				 
				 loadData:function(){
					 if (typeof(ObjectArtigoFotosPlus) == 'object'){
						 this.data=ObjectArtigoFotosPlus;
						 this.limit=this.data.length -1;
					 }
				 },

                                 loadImages: function (){
                                     var src = '';
                                     var i=0;
                                     for(i=0; i < this.limit ;i++){
                                        this.images[this.data[i].image_id]=false;
                                        src ='/galeria/'+this.data[i].image_id+'/500x550/';
                                        $('<img>').load(function(){
                                            var tmp = $(this).attr('src').split('/galeria/');
                                            var id = tmp[1].replace('/500x550/','');                                            
                                            $.ArtigoGaleria.images[id]=true;
					}).attr({'src':src});
                                     }
                                 },
				
				 configCursor : function(){							
                                                    var h= parseInt($('#ph-gallery-image').height());
                                                    var ch= parseInt($('#ph-gallery-left img').height());
                                                    var newh= Math.ceil(( h / 2 ) - ( ch / 2 ));
                                                    newh = newh+'px';
                                                    $('#ph-galeria-right img').css({'margin-top': newh});
                                                    $('#ph-gallery-left img').css({'margin-top': newh});                                                    
				 },

                                 isLoaded : function(){
									 		//console.log(typeof (console));
											 if (typeof (console) == 'object'){
												 console.log('index '+this.index);
											 }
														 
                                             return this.images[this.data[this.index].image_id];
                                 },

				 configLabel:function (){
					 		if (variavelControl == 0) {
					 				 var controlW = parseInt($('#ph-gallery-image img').width());
									  
                                     if ( controlW > 0 ) {
									 	variavelControl = 1;
                                        this.configCursor();
                                        var w = parseInt($('#ph-gallery-image img').width());
                                        var pdl = parseInt($('#ph-gallery-label').css('padding-left').replace('px',''));
                                        var pdr = parseInt($('#ph-gallery-label').css('padding-right').replace('px',''));
                                        var ml = parseInt($('#ph-gallery-board').width());
                                        ml = Math.ceil( (ml- w) / 2 ) ;
                                        ml = ml + 'px';
                                        var wpx = w - pdl - pdr;
                                        wpx= wpx+'px';
                                        $('#ph-gallery-label').css({'width': wpx});
                                        var hl =  parseInt($('#ph-gallery-label').css('padding-bottom').replace('px',''));
                                        hl += parseInt($('#ph-gallery-label').css('padding-top').replace('px',''));
                                        hl += $('#ph-gallery-label').height();
                                        var hlpx = '-'+hl+'px';

                                        this.Ah = $('#ph-gallery-label').height();
                                        this.Amt=  '-'+hl;
                                        $('#ph-gallery-label').css({'margin-top': hlpx,'margin-left':ml});
										$('#ph-gallery-label').show();
                                        this.free = true;
                                     } else {                                       
									 	 var controlSRC = $('#ph-gallery-image img').attr('src');	 											
										 $('<img>').load(function(){
										   variavelControl =1;
										   $.ArtigoGaleria.configLabel();
										 }).attr({'src':controlSRC});                                        
                                     }
								} else {
									 this.configCursor();
                                        var w = parseInt($('#ph-gallery-image img').width());
                                        var pdl = parseInt($('#ph-gallery-label').css('padding-left').replace('px',''));
                                        var pdr = parseInt($('#ph-gallery-label').css('padding-right').replace('px',''));
                                        var ml = parseInt($('#ph-gallery-board').width());
                                        ml = Math.ceil( (ml- w) / 2 ) ;
                                        ml = ml + 'px';
                                        var wpx = w - pdl - pdr;
                                        wpx= wpx+'px';
                                        $('#ph-gallery-label').css({'width': wpx});
                                        var hl =  parseInt($('#ph-gallery-label').css('padding-bottom').replace('px',''));
                                        hl += parseInt($('#ph-gallery-label').css('padding-top').replace('px',''));
                                        hl += $('#ph-gallery-label').height();
                                        var hlpx = '-'+hl+'px';

                                        this.Ah = $('#ph-gallery-label').height();
                                        this.Amt=  '-'+hl;
                                        $('#ph-gallery-label').css({'margin-top': hlpx,'margin-left':ml});
										$('#ph-gallery-label').show();
                                        this.free = true;
								}
				 },
				 
				 showLabel:function(){										
					$('#ph-gallery-label').css({height:0,marginTop:-20});
				 	$('#ph-gallery-label').animate(
                                            { height:$.ArtigoGaleria.Ah, marginTop: $.ArtigoGaleria.Amt},
                                            { duration: 250,
                                             complete: function () {$(this).show();}
                                            }
                                        );				 	
				 },
				 
				 hideLabel:function(){				 						
					$('#ph-gallery-label').animate(
                                            { height: 0,marginTop:-20 },
                                            { duration: 250,
                                             complete: function () {$(this).hide();}
                                            }
                                        );
				 },				 
				 
				 addClickAction:function (){
					 $('#ph-gallery-left').click(function(){
						$.ArtigoGaleria.prev();
						return false;				
					  }).css({'cursor':'pointer'});
					 
 					 $('#ph-galeria-right').click(function(){
                                                $.ArtigoGaleria.next();
						return false;				
					  }).css({'cursor':'pointer'});
					 
					 $('#ph-gallery-board').mouseenter(function(){
						$.ArtigoGaleria.hideLabel();
					  }).mouseleave(function(){
						$.ArtigoGaleria.showLabel();
					  });
				 },
				 
				 change:function (){			
				    if (this.free){
				 	 this.free = false;
					 $('#ph-gallery-label').hide();
                                         $('#ph-gallery-label').css({'height':'auto','width':'auto'});
                                         
					 var src='/galeria/'+this.data[this.index].image_id+'/500x550/';
					 var title=decodeURIComponent(this.data[this.index].image_description);
					 var desc=decodeURIComponent(this.data[this.index].image_title);
                                         
 					 $('#ph-gallery-image img').attr({'src':src});			
					 $('#ph-gallery-label-title').html(title);
					 $('#ph-gallery-label-resume').html(desc);
		 			 this.configLabel();
                                         
					 if (typeof(pageTracker)=='object'){pageTracker._trackPageview();}
	 				 if (typeof(w_counter)=='object'){w_counter.count();}	

					 var random_number=(Math.round(Math.random()*10000000000));
					 var url =decodeURIComponent('%2Fads%2F300x250%2F'); 
					 $('#iframemrec').attr({'src': url+random_number});
					 
					}
				     
				 },
				 
				 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();
				 }
			 }
});

jQuery(document).ready(function($){
   $.ArtigoGaleria.start();
});
