			//main image url
			var _mainImageUrl_str = '';
			//the ids of the options avalible with submenus
			var menuDivs = '#galerias, #fotografos, #ofertas';
			//slideshow nterval
			var intervalID;
			//our host
			var host = 'http://blackphotostudio.com/';
			//title for facebook shares
			var sharingTitleText = 'Black Photo Studio Bilbao';
			//current width of thumbnails
			var _thumbnailsWidth = null;
			//current screen width
			var _screenWidth = 0;
			//number of thumbnails to use as a scrolling proportion
			var _thumbnailsNumber = 0;
			
			//boot it up!
			$(document).ready(function(){
				//bind the three buttons
				$('#btnGalerias').bind('click',function(){hideSubMenus();	showSubmenus('galerias');});
				$('#btnFotografos').bind('click',function(){hideSubMenus();showSubmenus('fotografos');});
				$('#btnOfertas').bind('click',function(){hideSubMenus();	showSubmenus('ofertas');});
				//bind facebook icon
				updateFbUrl ($('.image img').attr('src'));
				//quiero esta foto update:
				_mainImageUrl_str = $('.image img').attr('src');
			});
			
			
			
			function hideImages(){ $('#main').animate({'opacity':0},function(){});  }
			function hideSubMenus(){ $(menuDivs).css('display','none'); $('#galerias, #fotografos').css('opacity',1);  }
			function fadeSubmenus(){ $(menuDivs).animate('opacity',0); hideSubMenus(); }
			function showSubmenus(menu){ $('#'+menu).css('display','block'); }
			function raiseSubmenus(){ $(menuDivs).animate({'bottom':75}); } 
			function hideTexts(){ $('.texto').css('display','none'); $('.texto').html('');}
			function showTexts(){ $('.texto').css('display','block');}
			function unbindMain(){$('#main').unbind();}
			
			
			
			function galerias(id){
				images_arr = [];
				timed_current_image = 0;
				
				//$('#ram').load('index.php?view=bfsFiles&id='+id+'&from='+current_file_pointer,function(){
				$('#ram').load('index.php?view=bfsFiles&id='+id,function(){
					raiseSubmenus();
					hideSubMenus();
					$('.thumbs').animate({'height':52},function(){
						thumbnails($('#ram').text());
					});
				});
				
			}
			
			
			
			
			var images_arr = [];
			var timed_current_image = 0;
			
			
			
			
			function timed_thumb(){
			    
			   
			    arr_thumbUrl = images_arr[timed_current_image].replace('files/', 'index.php?view=i&f=files/');
			    $('.thumbs div').append( $('<a>').addClass('thumb').attr('href','#').attr('onclick','image(\''+images_arr[timed_current_image]+'\');').attr('title', images_arr[timed_current_image]).append($('<img />').attr('src',arr_thumbUrl).load(function(){
				if(images_arr.length>timed_current_image){
				    //setTimeout(function() {
					timed_thumb();
				    //}, 1000);
				}
			    }))  );
			    timed_current_image ++;
			}
			
			
			function thumbnails(imagesArrayString){
				
				$('.thumbs').html(' ');
				thumbs = $('<div>');
			
				//only fails in IE, thats why the condition
				if($('#btnPlay').attr('href') != '#'){
					$('.options').append( $('<a>').attr('id','btnPlay').attr('href','#').html('reproducir slideshow') );
				}
				
				images_arr =imagesArrayString.split('|');
				$Slideshow.img_arr = images_arr;
				
				//var ttimeout = [];
				if(images_arr.length > 1){
					firstImageLoaded = false;
					for (var i=0; i < images_arr.length; i++) {
						 arr_thumbUrl = images_arr[i].replace('files/', 'index.php?view=i&f=files/');
						 //loads first image as it has loaded
						if(!firstImageLoaded){ 	image(images_arr[0]);  firstImageLoaded=true; }
						
						
						//ttimeout[i] = setTimeout(function() {
						    //timed_thumb();
						    //thumbs.append( $('<a>').addClass('thumb').attr('href','#').attr('onclick','image(\''+images_arr[i]+'\');').attr('title', images_arr[i]).append($('<img />').delay(2000).attr('src',arr_thumbUrl).load(function(){}))  );
						//}, 1000);
						
					}
					
					
					
					timed_thumb();
					
					
					//height value for .thumbsHolder
					$('.thumbsHolder').css('height','auto');
					
					$('.thumbs').append(thumbs);
					
					//reset the values for scrollLogically(whereTo)
					scrollLogically('reset');
					
					//detect the size needed for the thumbnail div
					if( detectIfScrollIsNeeded(images_arr.length) ){
						addScrollForThumbs();
					}else{
						removeScrollForThumbs();
					}

								
					$('.thumbs a').live('mouseenter',function(){
						$(this).addClass('selected');
					});
					
					$('.thumbs a').live('mouseleave',function(){
						$(this).removeClass('selected');
					});
					
					//for now
					/*
					$('#main').bind('click',function(){
						$Slideshow.next();
					});
					*/
					$('#btnPlay').bind('click',function(){
						play();
					});
					
					stop();
				}
				
					
			}
			
			

			function image (url){
				_mainImageUrl_str = url;
				
				$('#main').addClass('gallery');
				hideSubMenus();
				hideTexts();
				updateFbUrl(url);
				
				$('#main').animate({'opacity':.5},3000,'linear',function(){} );
				
				$('<img />').attr('src', url).load(function(){
				//update fb link
				imageNow = $(this);
						$('#main').animate({'opacity':0},function(){
						        $('#main').html( imageNow  );
						        
						        //explorer puts its own width and height, dont respect the 90%, strange
						        if(jQuery.browser.msie){
						        	msie_fixImageSize();
						        }
						        //mozilla 7.0.1 puts its own width and height, dont respect the 90%, strange
						        if(jQuery.browser.mozilla){
						        	msie_fixImageSize();
						        }
						});
											
						$('#main').animate({'opacity':1},function(){
							
						});
				});
				
				$('.thumb').each(function(){
							
							if( $(this).attr('title') == url ){
								$(this).addClass('selected');
							}else{
								$(this).removeClass('selected');
							}
				});
				
			}			
			
			function play() {
				$('#main').unbind();
				$('#btnPlay').addClass('clicked').html('parar slideshow');
				$('#btnPlay').unbind();
				$('#btnPlay').bind('click',function(){ stop(); });
				$Slideshow.next();
				  intervalID = setInterval($Slideshow.next, 5000);
			}
			function stop() {
				/*
				$('#main').bind('click',function(){
							$Slideshow.next();
				});
				*/
				$('#btnPlay').removeClass('clicked').html('reproducir slideshow');
				$('#btnPlay').unbind();
				$('#btnPlay').bind('click',function(){ play(); });
				  clearInterval(intervalID);
			}
			
			$Slideshow = {
				curr_int:0,
				img_arr:[],
				next:function(){
					if($Slideshow.isLast()){
						$Slideshow.curr_int = 0;
					}else{
					$Slideshow.curr_int ++;
					}
					image($Slideshow.img_arr[$Slideshow.curr_int]);
					
				},
				prev:function(){
					$Slideshow.curr_int --;
					image($Slideshow.img_arr[$Slideshow.curr_int]);
				},
				
				isLast:function(){ if($Slideshow.curr_int == ($Slideshow.img_arr.length - 1)){ return true; }else{ return false; } },
				isFirst:function(){ if(curr_int == 0){return true;}else{return false;}}
				
				
			};
			
			function noticias_get_id (id){
				stop();
				unbindMain();
				dir = 'index.php?view=noticias&id='+id;
				$('#main').animate({'opacity':0},function(){
					$('#texto').load(dir, function(){
						showTexts();
						fadeSubmenus();
					});
				});
			}
			
			function contacto(){
				stop();
				unbindMain();
				hideSubMenus();
				hideTexts();
				hideImages();
				$('#texto').load('html/contact.html', function(){
                                //$('#texto').load('index.php?view=forms&id=1', function(){
						showTexts();
						fadeSubmenus();
				});
			}
			
			
			function updateFbUrl (currentImage){
				prefix = 'http://www.facebook.com/sharer/sharer.php?src=bm&t='+sharingTitleText+'&v=3&u='+host+'?view=fb&foto=';
				$('#fbLink').attr('href',prefix + currentImage);
			}


			function loveThisPhoto(){
				stop();
				unbindMain();
				dir = 'html/quieroFoto.html';
				$('#main').animate({'opacity':0},function(){
					$('#texto').load(dir, function(){
						showTexts();
						fadeSubmenus();
						//replace all the values needed
						$('#usuario_quiere_foto_img').attr('src',_mainImageUrl_str);
						$('#usuario_quiere_foto').attr('value',_mainImageUrl_str);
						
					});
				});
			}
			
			function checkFormLoveThisPhoto(){
				foto 		= 	host + $('#usuario_quiere_foto').attr('value');
				nom 	= 	$('#f_nombre').attr('value');
				eml 		= 	$('#f_email').attr('value');
				
				if (nom == '' || eml == '') {
					alert('Necesitamos sus datos para poder enviarle la fotografia' + "\n" + 'por favor rellene el formulario');
					document.location.href += '#?';
				}else{
					sendFormLoveThisPhoto(foto,nom,eml);
				}
			}

			function sendFormLoveThisPhoto(f,n,e){
				//index.php?view=quieroFoto
				$.post("index.php?view=quieroFoto", { foto: f, nombre: n, email:e  }, function(data){
					$('#texto').html(data);
				} );
			}



			function detectIfScrollIsNeeded(numberOfImages_int){
				widthOfScreen = $(window).width();
				_screenWidth = widthOfScreen;
				_thumbnailsNumber = numberOfImages_int;
				thumbnailSize =  50; 
				//set the global thumbnail width tho use later
				_thumbnailsWidth = thumbnailSize * numberOfImages_int;
				if(widthOfScreen < thumbnailSize * numberOfImages_int){
					return true;
				}else{
					return false;
				}
			}
			
			
			function addScrollForThumbs(){
				$('.thumbs').css('width','90%');
				$('.prev, .next').animate({'width':40, 'height':39});
			}
			
			function removeScrollForThumbs(){
					$('.prev, .next').animate({'width':0, 'height':0},function(){
						$('.thumbs').css('width','98%');
					});
			}
			
			
			function pxToNumber(px_str){
				px_str_arr = px_str.split('px');
				px_str = px_str_arr[0];
				return Number(px_str);
			}
			
			function scrollThumbsLeft(){scrollLogically('left');}
			
			function scrollThumbsRight(){scrollLogically('right');}
			
			
			var currentMove = 0;
			var maxMoves = 0;
			var horizon = 0;
			var horizonDistance = 6;	//six thumbs every move
			var scrollAnimationType = 'slow';
			function scrollLogically(whereTo){
				horizon = $('.thumbs a.thumb').outerWidth() * horizonDistance;
				maxMoves = Math.ceil($('.thumbs div').width() / horizon);
				
				if(whereTo=='left'){
					if( (currentMove - 1) > 0){
						//can move left
						currentMove --;
						$('.thumbs div').animate({'left':'+='+horizon}, scrollAnimationType);
						//if we moved left we can move right
						$('div.next').css({ opacity: 1 });
					}else{
						//cant move left
						$('div.prev').css({ opacity: 0.5  });
						$('div.next').css({ opacity: 1 });
					}
				}
				
				if(whereTo=='right'){
					if( (currentMove + 1) < maxMoves){
						//can move right
						currentMove ++;
						$('.thumbs div').animate({'left':'-='+horizon}, scrollAnimationType);
						//if we moved right we can move left
						$('div.prev').css({ opacity: 1  });
					}else{
						//cant move to the right
						$('div.prev').css({ opacity: 1    });
						$('div.next').css({ opacity: 0.5 });
					}
				}
				//we need to reset these values when we load a new folder of images, call it from thumbnails()
				if(whereTo=='reset'){
					currentMove = 0;
					maxMoves = 0;
					//we cant move left for now
					$('div.prev').css({ opacity: 0.5  });
				}
				
			}
			
						

