$(document).ready(function() {
    if(location.hash != "" && location.hash.substr(1,1) == "/"){
        content.open(location.hash.substr(2));
    }else{
        var p = location.pathname;
        var u = p.substr(p.length-5);
                
        if(u != '.html' && !isMod(p)){
            q.hideComponents(1);
            
            content.open('join.html');
        }
    }
    content.loadSlides();
    displayInfo();
    $.history.init(function(hash) {
        var url = content.pageUrl.split('#',2).pop();
        if(hash == "" || hash.substr(1) == url || hash == url) {
        //content.close();
        } else {
            if(hash.substr(hash.length - 5) == '.html' || isMod(hash)){
                content.open(hash.substr(1), {
                    automated : true
                });
            }
        }
    }, { unescape: true });
	
    $('.more').live('click', function(e) {
        e.preventDefault();
        $(this).css({
            display : 'none'
        });
        var $this = $(this);
        if($(this).hasClass('tog'))
            $('.active.tog').not(this).trigger('click');
        $(this).toggleClass('active');

        var target = $($(this).attr('href'));
        var p = target.parent();
        if(!p.attr('oldh'))
            p.attr('oldh', p.outerHeight());
        if($(this).hasClass('active')) {
            target.toggleClass('hidden');
        }
        if(!p.attr('newh'))
            p.attr('newh', p.outerHeight() + 15);
        if($(this).hasClass('active'))
            p.css({
                overflow : 'hidden',
                height : p.attr('oldh') + 'px'
            }).animate({
                height : p.attr('newh') + 'px',
                overflow : 'auto'
            });
        else
            p.css({
                overflow : 'hidden',
                height : p.attr('newh') + 'px'
            }).animate({
                height : p.attr('oldh') + 'px',
                overflow : 'auto'
            }, function() {
                target.toggleClass('hidden');
            });
        if($this.text() == 'More') {
            $this.text('Less');
            $this.css({
                display : 'block',
                textAlign : 'right'
            });
        } else {
            $this.text('More');
            $this.css({
                display : 'none',
                textAlign : 'right'
            });
        }
        $(this).fadeIn('slow');

    });

    $('input').livequery(function() {
        try{
            $(this).placeholder();
        }catch(e){}
    });

    $('a.go_home').live('click', function(e) {
        e.preventDefault();
        content.close();
    });

    $('a.ajax_link').live('click', function(e) {
        e.preventDefault();
        content.open($(this).attr('href'));
    });
	
    $('a.go_back').live('click', function(e){
        e.preventDefault();
        window.history.go(-1)
    });
    $('a.close_dialog').live('click', function(e){
        e.preventDefault();
        content.closeDialog($(this).parent('.dialog'));
    })

    $('.interest_select .status').live('click', function(e) {
        e.preventDefault();
        $(this).toggleClass('selected');
        $(this).trigger('touchend');
    });
    $('a.show_login').live('click',function(e){
        e.preventDefault();
        $(this).toggleClass('active');
        $('.login_popup').toggle('slide', {
            direction : 'up'
        }, 100);
        $('a').not('.show_login').click(content.closeLogin);
    });


    $('.experience_q').live('click', function(e) {
        e.preventDefault();
        var interests = content.getInterests(); 
        if(!interests.length) {
            content.openDialog("Please select one or more interests to experience what Quintessentially can offer you");
        } else {
            var data = {
                interests : interests,
                name : $('input[name=person]').val(),
                city : $('input[name=country]').val()
            }
            content.open($(this).attr('href'), data);
        }
    });
	
    w.init();
    $('.slides, .nav, .lightbox, .dialog, .loader, .region_selector').disableSelection();
    $("body").livequery(function(){
        content.checkOpen();
    });
	
    $('.contact_submit').live('submit',function(e){
        var data = $(this).serialize();
        var url = $(this).attr('action');
        content.submit(data, url);
        return false;
    });
	
    $('.additional_submit').live('submit',function(e){
        var data = $(this).serialize();
        content.submit(data, '/ajax/additional');
        return false;
    });
	
    $('#email_experience').live('submit',function(e){
        var data = $(this).serialize();
        content.submit(data, '/ajax/emailExperience');
        return false;
    })
	
    $('.login_form').live('submit', function(e){
        try{
            if(this.username.value.length > 0 && this.password.value.length > 0){
                var data = {
                    username : this.username.value , 
                    password : this.password.value
                    }
                user.login(data);
            }else{
                content.openDialog("Please check your username and password");
            }
        }catch(e){}
        return false;
    });
	
	
    $('.show_forgot_pass').live('click', function(e){
        e.preventDefault();
        var form = "<form class='forgot_pass' style='text-align:center;'>Please enter your email address<br/><input type='text' name='email'/><br/><button type='submit' class='big_red little'>Send my password <span class='arrow'>&nbsp;</span></button></form>";
        content.openDialog(form);
    })
	
    $('.country_select_trigger *').live('click',function(e){
        e.preventDefault();
        $(this).blur();
        content.openRegionSelector();
    });
	
    $('.close_region').live('click',function(e){
        e.preventDefault();
        content.closeRegionSelector();
    });
})
$(window).resize(function() {
    w.reset();
});
