function decorate(){
    $('.sprite-button').spriteButton();
    $('.sprite-button, .m, .sb, .rbut').simpleButton();

    $('div.message').each(function(){
        $(this).append('<a href="#" class="button-close-message">Close</a>');
        $('a.button-close-message', $(this)).click(function(event){
            event.preventDefault();
            $(this).parents('div.message').fadeOut();
        });
    });
	
    $('.disabled').click(function(event){
        event.preventDefault();
    });
	
    $('tr[class!=header]').live('mouseover', function(){
        $(this).addClass('hover');
    });
	
    $('tr').live('mouseout', function(){
        $(this).removeClass('hover');
    });
	
    $(':text[class!=nofocus], :password, textarea, select').focus(function(event){
        $(this).addClass('ffocus');
    });
	
    $(':text[class!=nofocus], :password, textarea, select').blur(function(event){
        $(this).removeClass('ffocus');
    });
	
    $('a#crowdsound-activate-popup').click(function(event){
        $this = $(this);
        if($this.text() == 'Close Window'){
            $this.addClass('hover');
        } else {
            $this.removeClass('hover');
        }
    });
	
    $('textarea.textarea').autogrow({
        spaceHeight: 50
    });
}

(function($){
    $.fn.spriteButton = function(){
        this.addClass('sprite-button-full').append('<span class="right">&nbsp;</span>');
		
        return this;
    };
	
    $.fn.simpleButton = function(){
        this.mousedown(function(event){
            $(this).addClass('mousedown');
        })
		
        this.mouseup(function(event){
            $(this).removeClass('mousedown');
        });
	
        this.mouseleave(function(event){
            $(this).removeClass('mousedown');
        });
	
        this.mouseover(function(event){
            $(this).removeClass('mousedown');
        });
		
        return this;
    }
})(jQuery);

function onBlurInput(){
    $(this).removeClass('active');
};

function onFocusInput(){
    $(this).addClass('active');
};

function initTourTypeHelp(){
    if($('.tourtype-help').length){
		
        $('.tourtype-help').bt({
            ajaxCache: false,
            ajaxLoading: '<p><img src="/img/loading.gif" /> Loading help</p>',
            ajaxPath: ["$(this).attr('href')"],
            cssClass: 'supertip',
            cssStyles: {
                padding: '10px 10px 0 10px'
            },
            fill: '#feffb5',
            strokeStyle: '#e5e6ab',
            width: '250px'
        });
	
        $('.tourtype-help').click(function(event){
            event.preventDefault();
        });
	
    }
};

function initRequestForm(){	
    $('.button-request').click(function(event){
        event.preventDefault();
        $('.request-block').slideDown(500, function(){
            $('.request-block input[name=name]').focus();
        });
        //$(this).animate({opacity: 0.3}, 500, function(){$('.request-block input[name=name]').focus();});
        $(this).addClass('dimmed');
    });
	
    $block = $('.request-block');
    $form = $block.find('form');
    $result = $block.find('.result');
    $block.find('a.cancel').click(function(event){
        event.preventDefault();
        $form.get(0).reset();
        closeRequestForm();
    });
	
    $block.find('a.submit').click(function(event){
        event.preventDefault();
        $result.html('<img src="/img/loading.gif" />');
        $form.disable();
        var error = false;
        var focus = false;
        var mandatoryFields = ['name', 'phone', 'email'];
        $.each($form.find(':input'), function(){
            if($.inArray($(this).attr('name'), mandatoryFields) > -1 && !$(this).val()){
                if(!error) error = $(this);
            }
            if ($(this).attr('name') == 'email' && $(this).val() && !validate_email($(this).val())) {
                alert('Please check the email field');
                focus = $(this);
            }
        });
        if(!focus){
            if(error){
                alert('Please fill in all starred fields.');
                $form.enable();
            } else {
                $result.slideDown();
                var data = $form.serializeFields();
                data.from = 'callrequest';
                $.post($form.attr('action'), data, function(data){
                    if(data.ok){
                        $form.enable();
                        $form.get(0).reset();
                        $block.find('.result').text('Thank you for contacting Tour De Force travel company. ' +
                            'We will get back to you shortly.');
                    }
                }, 'json');
            }
        }
		
        var field = focus||error;
        if(field) {
            $form.enable();
            field.focus();
        }
    });
};

function closeRequestForm(){
    $block = $('.request-block');
    $button = $('.button-request');
	
    $block.slideUp();
    $block.find('.result').slideUp();
    $button.removeClass('dimmed');
};

function initWhyHelp(){
    if($('.icon-why').length){
        $('.icon-why').click(function(event){
            event.preventDefault();
        });
        $('.icon-why').each(function(){
            positions = $(this).hasClass('top') ? ['top'] : ['most'];
            $(this).bt({
                cssClass: 'supertip',
                fill: '#feffb5',
                strokeStyle: '#e5e6ab',
                width: '300px',
                positions: positions
            });
        });
    }
};

/* Textarea autogrow */
//(function(jQuery){var self=null;jQuery.fn.autogrow=function(o){return this.each(function(){new jQuery.autogrow(this,o)})};jQuery.autogrow=function(e,o){this.options=o||{};this.dummy=null;this.interval=null;this.line_height=this.options.lineHeight||parseInt(jQuery(e).css('line-height'));this.min_height=this.options.minHeight||parseInt(jQuery(e).css('min-height'));this.max_height=this.options.maxHeight||parseInt(jQuery(e).css('max-height'));this.space_height=this.options.spaceHeight||this.line_height;this.textarea=jQuery(e);if(this.line_height==NaN)this.line_height=0;this.init()};jQuery.autogrow.fn=jQuery.autogrow.prototype={autogrow:'1.2.2'};jQuery.autogrow.fn.extend=jQuery.autogrow.extend=jQuery.extend;jQuery.autogrow.fn.extend({init:function(){var self=this;this.textarea.css({overflow:'hidden',display:'block'});this.textarea.bind('focus',function(){self.startExpand()}).bind('blur',function(){self.stopExpand()});this.checkExpand()},startExpand:function(){var self=this;this.interval=window.setInterval(function(){self.checkExpand()},400)},stopExpand:function(){clearInterval(this.interval)},checkExpand:function(){if(this.dummy==null){this.dummy=jQuery('<div></div>');this.dummy.css({'font-size':this.textarea.css('font-size'),'font-family':this.textarea.css('font-family'),'width':this.textarea.css('width'),'padding':this.textarea.css('padding'),'line-height':this.line_height+'px','overflow-x':'hidden','position':'absolute','top':0,'left':-9999}).appendTo('body')}var html=this.textarea.val().replace(/(<|>)/g,'');if($.browser.msie){html=html.replace(/\n/g,'<BR>new')}else{html=html.replace(/\n/g,'<br>new')}if(this.dummy.html()!=html){this.dummy.html(html);if(this.max_height>0&&(this.dummy.height()+this.line_height>this.max_height)){this.textarea.css('overflow-y','auto')}else{this.textarea.css('overflow-y','hidden');if(this.textarea.height()<this.dummy.height()+this.space_height||(this.dummy.height()<this.textarea.height())){this.textarea.animate({height:(this.dummy.height()+this.space_height)+'px'},100)}}}}})})(jQuery);
(function(jQuery){
    var self=null;
    jQuery.fn.autogrow=function(o){
        return this.each(function(){
            new jQuery.autogrow(this,o);
        })
    };

    jQuery.autogrow=function(e,o){
        this.options=o||{};

        this.dummy=null;
        this.interval=null;
        this.line_height=this.options.lineHeight||parseInt(jQuery(e).css('line-height'));
        this.min_height=this.options.minHeight||parseInt(jQuery(e).css('min-height'));
        this.max_height=this.options.maxHeight||parseInt(jQuery(e).css('max-height'));
        this.space_height=this.options.spaceHeight||this.line_height;
        this.textarea=jQuery(e);
        if(this.line_height==NaN)this.line_height=0;
        this.init();
    };

    jQuery.autogrow.fn=jQuery.autogrow.prototype={
        autogrow:'1.2.2'
    };

    jQuery.autogrow.fn.extend=jQuery.autogrow.extend=jQuery.extend;
    jQuery.autogrow.fn.extend({
        init:function(){
            var self=this;
            this.textarea.css({
                overflow:'hidden',
                display:'block'
            });
            this.textarea.bind('focus',function(){
                self.startExpand();
            }).bind('blur',function(){
                self.stopExpand();
            });
            this.checkExpand();
        },
        startExpand:function(){
            var self=this;
            this.interval=window.setInterval(function(){
                self.checkExpand()
            },400)
        },
        stopExpand:function(){
            clearInterval(this.interval);
        },
        checkExpand:function(){
            if(this.dummy==null){
                this.dummy=jQuery('<div></div>');
                this.dummy.css({
                    'font-size':this.textarea.css('font-size'),
                    'font-family':this.textarea.css('font-family'),
                    'width':this.textarea.css('width'),
                    'overflow-x':'hidden',
                    'position':'absolute',
                    'top':0,
                    'left':-9999
                });
                if(this.line_height)this.dummy.css({
                    'line-height':this.line_height+'px'
                });
                try{
                    if(this.textarea.css('padding'))this.dummy.css({
                        'padding':this.textarea.css('padding')
                    });
                }catch(e){}
                this.dummy.appendTo('body')
            }
            var html=this.textarea.val().replace(/(<|>)/g,'');
            if($.browser.msie){
                html=html.replace(/\n/g,'<BR>new');
            }else{
                html=html.replace(/\n/g,'<br>new');
            }
            if(this.dummy.html()!=html){
                this.dummy.html(html);
                if(this.max_height>0&&(this.dummy.height()+this.line_height>this.max_height)){
                    this.textarea.css('overflow-y','auto');
                }else{
                    this.textarea.css('overflow-y','hidden');
                    if(this.textarea.height()<this.dummy.height()+this.space_height||(this.dummy.height()<this.textarea.height())){
                        this.textarea.animate({
                            height:(this.dummy.height()+this.space_height)+'px'
                        },100)
                    }
                }
            }
        }
    })
})(jQuery);

function initRequestForm(){	
    $('.button-request').click(function(event){
        event.preventDefault();
        $('.request-block').slideDown();
        $(this).animate({
            opacity: 0.3
        }, 500, function(){
            $('.request-block input[name=name]').focus();
        });
    });
	
    $block = $('.request-block');
    $form = $block.find('form');
    $result = $block.find('.result');
    $block.find('a.cancel').click(function(event){
        event.preventDefault();
        $form.get(0).reset();
        closeRequestForm();
    });
	
    $block.find('a.submit').click(function(event){
        event.preventDefault();
        $result.html('<img src="/img/loading.gif" />');
        $form.disable();
        var error = false;
        var focus = false;
        $.each($form.find(':input'), function(){
            if(!$(this).val()){
                if(!error) error = $(this);
            } else if ($(this).attr('name') == 'email' && !validate_email($(this).val())) {
                alert('Please check the email field');
                focus = $(this);
            }
        });
        if(!focus){
            if(error){
                alert('Please fill in all fields.');
            } else {
                $result.slideDown();
                var data = $form.serializeFields();
                data.from = 'callrequest';
                $.post($form.attr('action'), data, function(data){
                    if(data.ok){
                        $block.find('.result').text('Thank you for contacting Tour De Force travel company. ' +
                            'We will get back to you shortly.');
                    }
                }, 'json');
            }
        }
        $form.enable();
		
        var field = focus||error;
        if(field && field.focus) field.focus();
    });
}

function closeRequestForm(){
    $block = $('.request-block');
    $button = $('.button-request');
	
    $block.slideUp();
    $block.find('.result').slideUp();
    $button.animate({
        opacity: 1
    }, 500);
}

$(function(){
    decorate();
    initRequestForm();
    initTourTypeHelp();
    initWhyHelp();
});

var blackPopup = {
		
    $container: null,
    $content: null,
    $fade: null,
    config: {
        title: ''
    },
    current_url: '',
    self: null,
	
    close: function(){
        this.$fade.hide();
        this.$container.hide();
    },
		
    createBlack: function(){
        if(!$('#fade').length)
            $('body').append('<div id="fade"></div>');
        $fade = $('#fade');
        this.$fade = $fade;
        if($.fn.bgiframe) $fade.bgiframe();
        
    },

    createContainer: function(){
        if(!$('#litebox').length){
            $('body').append('<div id="litebox"><div class="top"></div><div class="center"><div class="header">'
                +'<a href="#" class="button-close d">Close</a><h2 class="title"></h2></div>'
                +'<div class="content"></div></div><div class="bottom"></div></div>');
        }

        $c = $('#litebox');
        if($.fn.bgiframe) $c.bgiframe();
        this.$container = $c;
        this.$content = $('.content', $c);
        $('a.button-close', $c).click(bind(this, 'onClose'));
		
    },
	
    init: function(){
        this.self = this;
		
        this.createBlack();
        this.createContainer();
    },
	
    onClose: function(event){
        event.preventDefault();
        this.close();
        if(this.config.onClose) this.config.onClose();
    },
	
    onSuccess: function(event){
        event.preventDefault();
        this.close();
        if(this.config.onSuccess) this.config.onSuccess();
    },
	
    show: function(settings){
        if (settings) $.extend(this.config, settings);

        if(!$.fn.bgiframe) this.$fade.show();
        this.$container.center();
		
        $('h2.title', this.$container).text(this.config.title);
		
        this.$container.show();
		
    /*
		if($.fn.bgiframe) {
			var height = document.documentElement.scrollTop + (document.documentElement.clientHeight / 2) - 230;
			$.fn.litebox.container.css('top', height + 'px');
			$.fn.litebox.container.css('marginTop', 0);
		}
		*/
		
    },
	
    load: function(url){
        if(this.current_url != url){
            this.current_url = url;
            this.$content.html('<img src="/img/loading.gif" /> Loading...');
            this.$content.load(url);
        }
    }
}
