$.fn.showBox = function(opt){
    opt = $.extend({
        speed: 250
    },opt);
    
    var $t = $(this);
    // asignando datos de iniciales
    $t.each(function(){
        var content = $(this).find('.content');
        content.data('h',content.height());
        $(this).addClass('box-active');
        content.data('openh',content.height());
        $(this).removeClass('box-active');
    });
    
    // cierre
    $t.hover(
		function(){
            var $this = this;
            //if( this.anim == undefined || this.anim == false ){
                $(this).addClass('box-active').find('.content').css({
                    height: $(this).find('.content').data('h'),
                    overflow: 'hidden'
                });
                
                //$this.anim = true;
                $(this).find('.content').animate({
                    height: $(this).find('.content').data('openh')
                },opt.speed,function(){
                    //$this.anim = false;
                });
            //} else {
                //$this.anim = false;
            //}
		},
		function(){
            var $this = this;
            //if( this.anim == undefined || this.anim == false ){
                $(this).removeClass('box-active');
                $(this).find('.content').animate({
                    height: $(this).find('.content').data('h')
                },opt.speed,function(){
                    //$this.anim = false;
                });
            //} else {
                //$this.anim = false;
            //}
		}
	);
};
function ___getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) {
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else {
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {
        if (document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
    if (xScroll < windowWidth) {
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
    return arrayPageSize;
}
function showAlertMsg(title,description){
	$.ajax({
        type: 'get',
        dataType: 'html',
        url: 'alert.php',
        success: function(data){
		 	var pagesize = ___getPageSize();
		  	
		 	if( $('#alert_box').length > 0){
		  		$('#alert_box').remove();
		  	}
            $('body').append(data);
            
            $('#alert_box .alert_title').html(title);
            $('#alert_box .alert_description').html(description);
            var auxh = $('#alert_box').height();
           
            
            $('#alert_box').css({
            	 position: 'absolute',
                 //top: 100+$(document).scrollTop(),
                 top: (((parseInt(pagesize[3])-auxh)/2)+$(document).scrollTop())+"px",
                 left: ( (parseInt(pagesize[2])-410)/2)+"px",
                 'z-index': 101
            });
          
            $('#alert_box').hide();
          
           
            $('body').append('<div id="overlay"></div>');
            $('#overlay').css({
                position: 'absolute',
                top: 0,
                left: 0,
                width: pagesize[0],
                height: pagesize[1],
                opacity: 0,
                background: '#000000',
                'z-index': 100
            }).animate({
                opacity: 0.90
            },function(){
                $('#alert_box').fadeIn();
            });
            
            $('#alert_box .close').bind('click',function(){
                //$(this).unbind('click');
                $('#alert_box').fadeOut('fast',function(){
                    $('#overlay').animate({
                        opacity: 0
                    },function(){
                        $(this).remove();
                    });
                });
                return false;
            });
        }
    });
	
	
}
(function($){
	$.fn.scrollPlaces = function(opt){
		opt = $.extend({
            upCls: 'up',
            downCls: 'down',
			targetCls: 'mv',
			move: 247,
			speed:1500
        },opt);
		
		var anim = false;
		// down
		$(this).find('.'+opt.downCls).click(function(){
			
			var mv = $(this).parent().find('.'+ opt.targetCls);
			var marginTop = parseFloat(mv.css('marginTop'));
			var d = mv.height() + marginTop;
			
            var $this = $(this);

			if( anim == false && (d - opt.move)  > 0 ){
            
				anim = true;
				
				mv.animate({
					'marginTop': '-='+ opt.move +'px'
				},opt.speed,function(){
					anim = false;
					
                    if( (d - opt.move) < mv.parent().height() ) $this.fadeOut();
                });
			} 
            $this.parent().find('.'+opt.upCls).fadeIn();
			return false;
		});
		// up
		$(this).find('.'+opt.upCls).click(function(){
			var mv = $(this).parent().find('.'+ opt.targetCls);
			var marginTop = parseFloat(mv.css('marginTop'));
			var d = mv.height() - marginTop;
            var $this = $(this);
			
			if( anim == false &&  mv.height() < d ){
				anim = true;
				mv.animate({
					'marginTop': '+='+ opt.move +'px'
				},opt.speed,function(){
					anim = false;
                   if( parseFloat(mv.css('marginTop')) >= 0 ) $this.fadeOut();
                });
            }
            $this.parent().find('.'+opt.downCls).fadeIn();
           
            return false;
		});
        // se oculta el link de que mueve el cursor para arriba 
		var mv = $(this).parent().find('.'+ opt.targetCls);
        $(this).find('.'+opt.upCls).hide();
        if(mv.height()<=mv.parent().height()){
        	$(this).find('.'+opt.downCls).hide();
        }
	};
    $.fn.scrool = function(opt){
		opt = $.extend({
            upCls: 'up',
            downCls: 'down',
			targetCls: 'mv',
			move: 247,
			speed:1500
        },opt);
		if(!$(this).hasClass("scrool")){
			$(this).addClass("scrool");
			var anim = false;
			// down
			$(this).find('.'+opt.downCls).click(function(){
				var mv = $(this).parent().find('.'+ opt.targetCls);
				var marginTop = parseFloat(mv.css('marginTop'));
				var d = mv.height() + marginTop;
	            var $this = $(this);
	
				if( anim == false && (d - opt.move)  > 0 ){
	            
					anim = true;
					
					mv.animate({
						'marginTop': '-='+ opt.move +'px'
					},opt.speed,function(){
						anim = false;
	                    if( (d - opt.move) < mv.parent().height() ) $this.fadeOut();
	                });
				} 
	            $this.parent().find('.'+opt.upCls).fadeIn();
				return false;
			});
			// up
			$(this).find('.'+opt.upCls).click(function(){
				var mv = $(this).parent().find('.'+ opt.targetCls);
				var marginTop = parseFloat(mv.css('marginTop'));
				var d = mv.height() - marginTop;
	            var $this = $(this);
				
				if( anim == false &&  mv.height() < d ){
					anim = true;
					mv.animate({
						'marginTop': '+='+ opt.move +'px'
					},opt.speed,function(){
						anim = false;
	                   if( parseFloat(mv.css('marginTop')) >= 0 ) $this.fadeOut();
	                });
	            }
	            $this.parent().find('.'+opt.downCls).fadeIn();
	           
	            return false;
			});
			$(this).find('.'+opt.upCls).hide();
		}	
        // se oculta el link de que mueve el cursor para arriba 
      
	};
	/*
    $.fn.inputSelect = function(opt){
        opt = $.extend({
            upCls: 'up',
            downCls: 'down',
            valText: 'val-text',
            val: 'val',
            autovalueCls: 'autovalue',
			listCls: 'inputSelect_list'
        },opt);
        
        var toggleArrows=function(el){
        	var $t = el.parent(); // contenedor
        	var maxScrool = 0;
        	// calculamos el alto maximo de scrool
			$t.find('ul').children().each(function(){
                maxScrool += $(this).height() + 0;
            });
			
			//$(this).find('.'+opt.upCls).show();
			//$(this).find('.'+opt.downCls).show();
			$t.find('.'+opt.upCls).each(function(){
				var ul = $(this).parent().find('ul');
                var x = parseFloat( ul.find('li:first').css('marginTop') );
                var r = maxScrool - x;
                if( maxScrool < r ){
                	$(this).show();
                }
                else{
                	$(this).hide();
                }
			 });
        };
		var init = function(el){
			var anim = false;
            var scrool = 80;
            var maxScrool = 0;
			var inputText = el;
			var $t = el.parent(); // contenedor
			
            // marcar campo como activo
            el.parents('.inputSelect').addClass('input-open');

			// ocultar listado
			var finish = function(el,input){
                el.fadeOut('fast');
                input.removeAttr('disabled');
                el.parents('.inputSelect').removeClass('input-open');
            };
            
            $t.find('.'+opt.listCls).each(function(){
            	
					$(this).find('.'+opt.upCls).hide();
					$(this).find('.'+opt.downCls).hide();
				
            });
            
            // mostramos el listado
            $t.find('.'+opt.listCls).fadeIn('fast',function(){
				var $this = $(this);
				// bloqueamos el input text
				inputText.attr('disabled','disabled');
				
				// calculamos el alto maximo de scrool
				$(this).find('ul').children().each(function(){
	                maxScrool += $(this).height() + 0;
	            });
				
				// Asignar data de la lista a los campos
				$(this).find('li').bind('click',function(){
					var ul = $(this).parent();
	                ul.find('li.selected').removeClass('selected');
	                $(this).addClass('selected');
	                $t.find('.'+opt.val).val( $(this).attr('id') );
	                $t.find('.'+opt.valText).val( $(this).text() ).removeClass(opt.autovalueCls);
					finish($this,inputText);
	            });
				
                // hover
                $(this).find('li').hover(function(){
                    $(this).addClass('hover');
                },function(){
                    $(this).removeClass('hover');
                });
                
				// navegacion para abajo en la lista
				$(this).find('.'+opt.downCls).live('click',function(){
	                var ul = $(this).parent().find('ul');
					var x = parseFloat( ul.find('li:first').css('marginTop') );
	                var r = maxScrool + x;
					
	                if( anim == false && (r - scrool)  > 0 ){
	                    anim = true;
	                    ul.find('li:first').animate({
	                        'marginTop': '-='+(scrool*2)+'px'
	                    },'fast',function(){
	                       anim = false;
	                    });
	                    $t.find('.'+opt.upCls).show();
	                }
	                else{
	                	if(anim==false){
	                		$t.find('.'+opt.downCls).hide();
	                	}
	                }
	                toggleArrows(el);
	                return false;
	            });
				
				// navegacion para arriba en la lista
				$(this).find('.'+opt.upCls).live('click',function(){
					var ul = $(this).parent().find('ul');
	                var x = parseFloat( ul.find('li:first').css('marginTop') );
	                var r = maxScrool - x;

	                if( anim == false && maxScrool < r ){
	                	
	                    anim = true;
	                    ul.find('li:first').animate({
	                        'marginTop': '+='+(scrool*2)+'px'
	                    },'fast',function(){
	                       anim = false;
	                       
	                    });
	                    $t.find('.'+opt.downCls).show();
	                }
	                else{
	                	
	                	$t.find('.'+opt.upCls).hide();
	                }
	                toggleArrows(el);
	                
	                return false;
	            });
				
				if(maxScrool > $t.find("ul").height()){
					$(this).find('.'+opt.downCls).show();
				}
				
				
				
				
				
			});
			
		};
		
		$(this).find('.'+opt.valText).focus(function(){
			init( $(this) );
		});
		$(this).find('.go').click(function(){
			init( $(this) );
			return false;
		});
		return this;
    };
	*/
	 $.fn.inputSelect = function(opt){
	        opt = $.extend({
	            upCls: 'up',
	            downCls: 'down',
	            valText: 'val-text',
	            val: 'val',
	            autovalueCls: 'autovalue',
				listCls: 'inputSelect_list'
	        },opt);
	        
	        var toggleArrows=function(el){
	        	var $t = el.parent(); // contenedor
	        	var maxScrool = 0;
	        	// calculamos el alto maximo de scrool
				$t.find('ul').children().each(function(){
	                maxScrool += $(this).height() + 0;
	            });
				
				//$(this).find('.'+opt.upCls).show();
				//$(this).find('.'+opt.downCls).show();
				$t.find('.'+opt.upCls).each(function(){
					var ul = $(this).parent().find('ul');
	                var x = parseFloat( ul.find('li:first').css('marginTop') );
	                var r = maxScrool - x;
	                if( maxScrool < r ){
	                	$(this).show();
	                }
	                else{
	                	$(this).hide();
	                }
				 });
	        };
			var init = function(el){
				
				var anim = false;
	            var scrool = 80;
	            var maxScrool = 0;
				var inputText = el;
				var $t = el.parent(); // contenedor
				var list= $t.find(".inputSelect_list");
	            // marcar campo como activo
	            el.parents('.inputSelect').addClass('input-open');
	         
				// ocultar listado
				var finish = function(el,input){
	                el.fadeOut('fast');
	                input.removeAttr('disabled');
	                el.parents('.inputSelect').removeClass('input-open');
	            };
	            
	            list.find(".list_box").scrool({move:42,speed:300,targetCls:'mv'});
	            
	            if(list.find(".list_box ul li").length<=4){
	            	list.find(".up,.down").hide();
	            }
	            // mostramos el listado
	            $t.find('.'+opt.listCls).fadeIn('fast',function(){
					var $this = $(this);
					// bloqueamos el input text
					inputText.attr('disabled','disabled');
					
					// calculamos el alto maximo de scrool
					$(this).find('ul').children().each(function(){
		                maxScrool += $(this).height() + 0;
		            });
					
					// Asignar data de la lista a los campos
					$(this).find('li').bind('click',function(){
					
						var ul = $(this).parent();
		                ul.find('li.selected').removeClass('selected');
		                $(this).addClass('selected');
		                $t.find('.'+opt.val).val( $(this).attr('id') );
		                $t.find('.'+opt.valText).val( $(this).text() ).removeClass(opt.autovalueCls);
						finish($this,inputText);
		            });
					
	                // hover
	                $(this).find('li').hover(function(){
	                    $(this).addClass('hover');
	                },function(){
	                    $(this).removeClass('hover');
	                });
	               
				});
				
			};
			
			$(this).find('.'+opt.valText).focus(function(){
				init( $(this) );
			});
			$(this).find('.go').click(function(){
				init( $(this) );
				return false;
			});
			return this;
    };
    $.fn.carrusel = function(opt){
        opt = $.extend({
            prevId: 'prev',
            nextId: 'next',
            itemsContId: 'mainThumbs',
            itemsContWidth: 427
        },opt);

        var $t = $(this);
        var $itemsCont = $('#'+opt.itemsContId);
        var newItemsContWidth = 0;
        var x = 0;
        // calculo el ancho del contenedor
        $itemsCont.children().each(function(){
            newItemsContWidth += $(this).width() + 16;
        });

        $itemsCont.width( newItemsContWidth );
       
        // oculto control izquierdo
        $('#'+opt.prevId).hide();

        $('#'+opt.nextId).bind('click',function(){
            var $this = $(this);
            x = 0;
            $itemsCont.animate({
                'marginLeft': '-=444px'
            },'slow',function(){
                x = newItemsContWidth + parseFloat( $itemsCont.css('marginLeft') );
                if( x <=444 ){
                    $this.hide();
                }
                $('#'+opt.prevId).show();
            });
            return false;
        });

        $('#'+opt.prevId).bind('click',function(){
            var $this = $(this);
            x = 0;
            $itemsCont.animate({
                'marginLeft': '+=444px'
            },'slow',function(){
                x = newItemsContWidth + parseFloat( $itemsCont.css('marginLeft') );
                if( x >= newItemsContWidth ){
                    $this.hide();
                }
                $('#'+opt.nextId).show();
            });
            return false;
        });

    };

    $.fn.tab = function(opt){
        opt = $.extend({
            navCls: 'tab-link',
            itemCls: 'item',
            hideCls: 'tab-hide',
            navActiveCls: 'tab-active'
        },opt);

        var $t = $(this);
        var $navs = $t.find('.'+opt.navCls);
        var $targets = $t.find('.'+opt.itemCls);
        var targets = [];
      
        $targets.each(function(){
        	
        	
            $(this).data('h',$(this).height()).css({
                'overflow':'hidden',
                'height': 0
            });
        });
        
        $t.find('.'+opt.navCls).each(function(){
            targets.push(
                '#' + $(this).attr('href').split('#')[1]
            );
        });
        
        // ocultando items
        /* $navs.filter(':first').addClass(opt.navActiveCls);*/
        $targets.addClass(opt.hideCls); 

        // activando tabs
        $navs.bind('click',function(){
        	
            // activeCls
        	if($(this).css("cursor")!='default'){
        		var el = $(this).attr('href').split('#')[1];
        		if(el=="t-2"){
        			$("#t-1").hide(1);
        		}
        		if( $targets.filter('#'+$(this).attr('href').split('#')[1]).hasClass(opt.hideCls)){
		            $t.find('.'+opt.navActiveCls).removeClass(opt.navActiveCls);
		            $(this).parent().parent().addClass(opt.navActiveCls);
		            // show item
		            $targets.not('.'+opt.hideCls).addClass(opt.hideCls).hide().css('height',0);
	            
		            $targets.filter('#'+$(this).attr('href').split('#')[1])
		            .animate({
		                height: $targets.filter('#'+$(this).attr('href')).data('h')
		            },'fast',function(){
		            	
		            	if($("#amenities_list").length > 0 && !$("#t-1").hasClass("tab-hide")){
			            	$("#t-1").css("height",($("#amenities_list").height()+20)+"px");
	                    	$("#t-1").css("overflow","hidden");
		            	}
		            	
		            	
		            }).removeClass(opt.hideCls);
		            
        		}
        		else{
        			 
        			$targets.filter('#'+$(this).attr('href').split('#')[1]).addClass(opt.hideCls);
        			
        			$targets.filter('#'+$(this).attr('href').split('#')[1]).animate({height:0},function(){
        				$t.find('.'+opt.navActiveCls).removeClass(opt.navActiveCls);	
            			
        			});
        			
        		}
            }
           
            return false;
        });
        return this;
    };

    $.fn.cycle = function(opt){
        opt = $.extend({
            time: 3,
            showCls: 'cycle-show',
            contCls: 'img'
        },opt);
        var $t = $(this);
        var iterator;
        
        var showNext = function(){
            var el = $t.filter('.'+opt.showCls);
            if( el.next().is('.'+opt.contCls) ){
                el.find('img').fadeOut();
                el.find('.desc').fadeOut();
                el.removeClass(opt.showCls);
                
                el.next().find('img').fadeIn();
                el.next().addClass(opt.showCls);
            }
            else {
                el.find('img').fadeOut();
                el.find('.desc').fadeOut();
                el.removeClass(opt.showCls);
                
                $($t[0]).find('img').fadeIn();
                $($t[0]).addClass(opt.showCls);
            }
        };
        
        var play = function(){
            setTimeout(
                function(){
                    showNext();
                    play();
                }
            ,parseFloat(opt.time * 1000));
        };
        
        var init = function(){
            $($t[0]).addClass(opt.showCls);
            play();
        };
        
        init();
        return this;
    };

    $(document).ready(function(){
        
        // credits
        $('.subnav .credits').click(function(){
            if( $('#credits').length == 0 )
            {
                $.ajax({
                    type: 'get',
                    dataType: 'html',
                    url: $(this).attr('href'),
                    success: function(data){
                        $('body').append(data);
                        $('#credits').hide().css({
                            position: 'absolute',
                            top: 100,
                            left: '50%',
                            marginLeft: -$('#credits').width()*0.5,
                            'z-index': 101
                        });
                        var pagesize = ___getPageSize();
                        $('body').append('<div id="overlay"></div>');
                        $('#overlay').css({
                            position: 'absolute',
                            top: 0,
                            left: 0,
                            width: pagesize[0],
                            height: pagesize[1],
                            opacity: 0,
                            background: '#000000',
                            'z-index': 100
                        }).animate({
                            opacity: 0.90
                        },function(){
                            $('#credits').fadeIn();
                        });
                        
                        $('#credits .close').bind('click',function(){
                            $(this).unbind('click');
                            $('#credits').fadeOut('fast',function(){
                                $('#overlay').animate({
                                    opacity: 0
                                },function(){
                                    $(this).remove();
                                });
                            });
                            return false;
                        });
                    }
                });
            } else {
                var pagesize = ___getPageSize();
                $('body').append('<div id="overlay"></div>');
                $('#overlay').css({
                    position: 'absolute',
                    top: 0,
                    left: 0,
                    width: pagesize[0],
                    height: pagesize[1],
                    opacity: 0,
                    background: '#000000',
                    'z-index': 100
                }).animate({
                    opacity: 0.90
                },function(){
                    $('#credits').fadeIn();
                });
                
                $('#credits .close').bind('click',function(){
                    $(this).unbind('click');
                    $('#credits').fadeOut('fast',function(){
                        $('#overlay').animate({
                            opacity: 0
                        },function(){
                            $(this).remove();
                        });
                    });
                    return false;
                });
            }
            
            return false;
        });
        
        
        $('.inputSelect').each(function(){
            $(this).inputSelect();
        });


        $('.arriba_button').click(function () { 
          window.scrollTo(0,0);
          return false;
        });
        
        $('#nav li').hover(function(){
            var t = $(this);
            var anim = false;
            
            if( anim == false ){
                anim = true;
                t.addClass('hover');
                t.children('a').addClass('hover');
                
                t.find('.subnavCont .subnav').css({
                    top: 128
                });
                t.find('.subnavCont').show();
                t.find('.subnavCont .subnav').animate({
                    top: 0
                },'fast',function(){
                    anim = false;
                });
            }
        },function(){
            var t = $(this);
            var anim = false;
            if( anim == false ){
                anim == true;
                $(this).find('.subnavCont .subnav').animate({
                    top: 128
                },'fast',function(){
                    t.removeClass('hover');
                    t.children('a').removeClass('hover');
              
                    $(this).parents('.subnavCont').hide();
                    anim= false;
                });
            } else {
                t.removeClass('hover');
                t.children('a').removeClass('hover');
          
                $(this).parents('.subnavCont').hide();
            }
        });
        
        if ($.browser.safari) {
            $('textarea').css('resize', 'none');
            $('input.text').css('resize', 'none');
        }
        
        // cloud-language
        $('#cloud-language').css('opacity',0);
        $('#cloud-language').css('display','block');
        $('#language').hover(function(){
            $('#cloud-language').animate({'opacity': 1});
        },
        function(){
            $('#cloud-language').animate({'opacity': 0});
        });
        
        //Frase superior
        $('#topToolbar').css({
            'top': -50,
            'opacity': 0
        });
        setTimeout(function(){
        	$('#topToolbar').css('display','block');
            $('#topToolbar').animate({
            	
                top: 0,
                opacity: 1
            },'slow');
        },3000);
        setTimeout(function(){
            $('#topToolbar').animate({
                top: -50,
                opacity: 0
            },'slow');
        },15000);
        
        /*
        // show-graph
        $('.show-graph .imgCont .desc').css({
            'opacity': 0,
            'display': 'none'
        });
        $('.show-graph .imgCont').hover(
            function(){
                $(this).find('.bg-desc, .desc').show();
                $(this).find('.desc').not('.bg-desc').animate({
                    'opacity': 1
                },'fast');
                $(this).find('.bg-desc').animate({
                    'opacity': .75
                },'fast');
            },
            function(){
                $(this).find('.desc').not('.bg-desc').animate({
                    'opacity': 0
                },'fast',function(){
                    $(this).hide();
                });
                $(this).find('.bg-desc').animate({
                    'opacity': 0
                },'fast',function(){
                    $(this).hide();
                });
            }
        );
        
        $('.show-graph .img img').not(':first').hide();
        $('.show-graph .img').cycle();
		*/
		

        // hover button
        $('button').hover(function(){ $(this).addClass('hover'); },function(){ $(this).removeClass('hover'); });
    });
    
   
    

})(jQuery);
