/*
 * pages.js
 *
 * Copyright (c) 2010 http://wwww.music1.com/
 * Dual licensed under the MIT and GPL licenses. 
 * 
 * http://wwww.music1.com/design/js/pages.js
 */

var pageInit = {
    index: function()
    {
        if($.browser.msie && parseInt($.browser.version) <= 6){
            $(document.body).html('<div class="ie6Dead"><h1>Music1.com</h1><p>Your browser is no longer supported.</p><p>Please upgrade to a modern browser:</p><ul><li><a href="http://www.microsoft.com/windows/internet-explorer/default.aspx">Internet Explorer</a></li><li><a href="http://www.google.com/chrome/">Google Chrome</a></li><li><a href="http://www.mozilla.com/firefox/">Mozilla Firefox</a></li><li><a href="http://www.apple.com/safari/">Safari</a></li><li><a href="http://www.opera.com/browser/">Opera</a></li></ul></div>');
            utils.gaTrack('/ie6-dead');
            return;
        }
        // $(document.body).hashProvider();

        var pages = {
            splash: function(){
                var attributes = {
                    name: 'video_player',
                    id: 'video_player',
                    wmode: "transparent"
                }
                
                swfobject.embedSWF(
                    "/design/flash/player.swf", "player", "920", "590", "9.0.0","/design/flash/expressInstall.swf", 
                    {file:'http://static.openfilm.com/m1/1280x720-homepage-video-version-6.mp4',image:'http://openfilm.com.s3.amazonaws.com/static/data/img/vmt/30400/30491/s1_2.jpg'}, 
                    {wmode: "transparent"}, 
                    attributes,
                    function(id, success){
                        if(!success){
                            var html5 = [
                                '<video width="920" height="518" controls="true" autoplay="true">',
                                '<source src="http://static.openfilm.com/test/1280x720-homepage-video-version-6.mp4" type="video/mp4" />',
                                '<source src="http://static.openfilm.com/test/1280x720-music1-homepage-video.ogv" type="video/ogg" />',
                                "<p class='installFlash' style='display: block;'>You need to install Adobe's Flash Player.<br/>",
                                '<a href="http://get.adobe.com/flashplayer/">Click here to download the latest version</a>.</p>',
                                '</video>'
                            ].join('');
                            $('#player')
                                .css('height', '518px')
                                .html(html5);
                        }
                    }
                );

                $('#form_splash').ready(function(){
                    var $form = $('#form_splash'),
                    validator = $form.validate(formActions.splashForm);
                    $form.submitAjaxIndikator();

                    $('#email', $form).bind('click', function(event){
                        $('#success_message').css('display', 'none');
                    });
                });
            },
            deflt: function(){
                
            }
        };
        pages.splash();

        // $.router.init();
        // $.router(/^(\/?)$/, pages.deflt)
        // .router(/^\/(splash)\/$/, pages.splash)
        // ;
        
        // $.router.hashChange = function(hash){
            // if($('#popup_container').attr('id')){
                // $.dialog._hide();
            // }
        // }

        // $.router.change = function(hash){
            // $window
            // .unbind('resize.variableGrid');
            // setTimeout(function(){
                // $window.scrollTo(0, 100);
            // }, 500);
        // }
        
        // $.router.notFound = function(hash){
            // pages.error(i18n.m10, i18n.m11, 2, hash); 
        // }

        // if(!$.router.isFound){
            // var lh = window.location.hash.replace(/^#/,'');

            // if(lh == ''){
                // pages.deflt();
            // }else{
                // pages.error(i18n.m10, i18n.m11, 2, lh); 
            // }
        // }
    }
}

var actions = {
    
};

var formActions = {
    splashForm: {
        submitHandler: function(form)
        {
            var _form = $(form);
            $('#success_message', _form).css('display', 'none');

            var options = {
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    $.ajaxAlert({
                        title: '',
                        text: i18n.m31,
                        styleClass: 'error',
                        buttonClose: true,
                        speedClose: 5000
                    });
                },
                success: function(response)
                {
                    _form.trigger('submitAjaxIndikatorHide');
                    if(response.status) {
                        $('#success_message', _form).css('display', 'inline-block');
                        _form.resetForm();
                    } else {
                        $.ajaxAlert({
                            title: '',
                            text: response.message,
                            styleClass: 'error',
                            buttonClose: true,
                            speedClose: 5000
                        });
                        // utils.responseError('splashForm', 'no response.error', _form, response.error);
                    }
                },
                url: '/',
                dataType: "json",
                type: 'POST',
                cache: false
            };
            _form.trigger('submitAjaxIndikatorShow');
            _form.ajaxSubmit(options);
            return false;
        },
        //invalidHandler: utils.formErrorAlert, // TODO: содрать с опенфилма,
        ignoreTitle: true,
        focusInvalid: false,
        focusCleanup: true,
        rules: {
            email: {
                required: true,
                maxlength: config.fields.email.max,
                email: true,
                remote: {
                    url: '/a/standalone-validate/',
                    type: 'post'
                }
            }
        },
        errorPlacement: function(error, element) {
            $('#success_message').css('display', 'none');
            error.appendTo(element.parents('form').find('.error-' + element.attr('name')));
        }
    }
}

