function isEmail(Mail) {
    Mail=Mail.toLowerCase();
    return (Mail.search(/^([a-z]+)([a-z0-9\-\_\.]{1,100})([a-z0-9]+)\@([a-z0-9]+)([a-z0-9\-\.]*)([a-z0-9]+)\.([a-z]{2,6})$/) != -1);
}

var errors = [];
var cancelledForm = false;

function replace_static_bg() {
    $('#flash-wrapper').hide();
    $('#wrapper').show();
    $('#background').show();
    $('#content').show();
}

function remove_static_bg() {
    $('#about').hide();
    $('.no-flash').hide();
    $('#background').hide();
    $('#flash-wrapper').show();
    $('#content').hide();
    $('#lightbox-button').hide();
    $('#gallery-button').hide();
    $('#about-button').hide();
}

function hide_static_pages() {

    $('#background-layer').fadeOut(500);
    $('#gallery').fadeOut(800);
    $('#about').fadeOut(800);

    setTimeout( function() {
                    if( !($.browser.msie && $.browser.version < 8) )
                        $('#wrapper, #bottom').animate({ opacity : 1 }, 500);
                }, 1000);
}

function show_content(){

    $('#content').show();
    $('#lightbox-button').show();
    $('#flash-wrapper object').remove();
    $('#background').show();    
    $('#gallery-button').show();
    $('#about-button').show();
}

function showError(id) {
    $( '#' + id ).addClass('error');
    $( '#' + id + '_err' ).show();

    errors.push(id);
}

function hideError(id) {
    $( '#' + id ).removeClass('error');
    $( '#' + id + '_err' ).hide();
}

$(document).ready(function (){

    var playerVersion = swfobject.getFlashPlayerVersion();
	if( playerVersion.major ) // if we have flash
        remove_static_bg();
    /*
     * Modal windows
     */
     
     $('#gallery').bind('contextmenu', function(e){
        return false;
     });
     
   	$.nyroModalSettings({ closeButton 		: '<a href="#" class="nyroModalClose" id="closeBut" title="close"><img src="/static/img/close.png" alt="close" /></a>',
   						  minHeight			: 50,
   						  showContent       : showContent,
   						  hideContent       : hideContent,
   						  showTransition    : resizeModal,
   						  processHandler	: function(settings) {
								var url = settings.url;
								var formatting = url.split('.');
								var format = formatting[formatting.length-1];

								if( is_video( format ) > -1 ) {

                                    var modalHeight = 295;
                                    var modalWidth  = 480;

                                    if( settings.title == 'high') {
                                        modalHeight = 363;
                                        modalWidth  = 600;
                                    }

									$.nyroModalSettings({
										type: 'swf',
										height: modalHeight,
										width : modalWidth,
										url: url.replace(new RegExp("watch\\?v=", "i"), 'v/')
									});
						
									
									setTimeout($.bind(function(){

                                        LbVideoPlace('nyroModalContent', {videoFile   : url, autoplay  : true, height : modalHeight, width : modalWidth});
                                        
                                        if( url == '/static/videos/LOW.flv' || url == '/static/videos/HIGH.flv')
                                            $('#nyroModalWrapper .wrapperSwf').append('<div id="change-quality"><a href="/static/videos/HIGH.flv" title="high" class="high">watch in high quality</a><a href="/static/videos/LOW.flv" class="normal">watch in normal quality</a></div>');
                                        
                                        if( modalWidth == 480 )
                                            $('#change-quality .normal').hide();
                                        else {
                                            $('#change-quality .high').hide();
                                        }


                                        $('#change-quality a').nyroModal();
						
						            }, this), 1500);

								}else if( format == 'png' || format == 'jpg' || format == 'gif' ){
                                    // do nothing if an image
						        }else if( format == 'swf' ){
									$.nyroModalSettings({
							            type: 'swf',
										width: 512,
										url: url
									});						
						        } else if ( format == 'pdf' ){
									$.nyroModalSettings({
							            type: 'iframe',
							            height	: 512,
										width: 512,
										url: url
									});						
						        }						        	

							}
						});

	$('.modal').nyroModal();

	var playerVersion = swfobject.getFlashPlayerVersion(); // returns a JavaScript object

	if( playerVersion.major ) {

        $('#sign-up-form').addClass('minimise');
        $('#terms_and_conditions').addClass('minimise');
        $('#content').css({ height : '24px' });

        var flash_params = {
          scale: 'noscale',
          width: '100%',
          height: '100%',
          salign: 'lt',
          wmode:'opaque'
        };
    
        swfobject.embedSWF('/static/flash/flythrough.swf', 'flash-content', '100%', '100%', '8', align="middle", {}, flash_params, {align:'middle'});
    }
   
    $('#sign-up').click(function (event){

        event.preventDefault();

        if( $('#thankyou').hasClass('form-submitted') && !$('#sign-up-form').hasClass('visible') ) {

            $('#sign-up-form').animate({ height: '65px' });
            $('#sign-up-form').addClass('visible');
            $('#content').css({ height : 'auto' });

        } else if( $('#thankyou').hasClass('form-submitted') ) {

            $('#sign-up-form').animate({ height: '0px' });
            $('#sign-up-form').removeClass('visible');

            $('#terms_and_conditions').animate({ height: '0px' });            
            $('#terms_and_conditions').removeClass('visible');
            
            $('#content').animate({ height : '24px' });
        
        } else if ( !$('#sign-up-form').hasClass('visible') ){

            $('#content').css({ height : 'auto' });
            $('#sign-up-form').addClass('visible');
            $('#sign-up-form').animate({ height: '248px' });

        } else {
            $('#sign-up-form').animate({ height: '0px' });
            $('#sign-up-form').removeClass('visible');
            
            $('#terms_and_conditions').animate({ height: '0px' });
            $('#terms_and_conditions').removeClass('visible');
            $('#content').animate({ height : '24px' });
        }
    });
    
    $('#thankyou-terms').click(function (event){
        event.preventDefault();
        if (!$('#terms_and_conditions').hasClass('visible')){
            $('#content').css({ height : 'auto' });
            $('#terms_and_conditions').animate({ height: '128px' });
            $('#terms_and_conditions').addClass('visible');
        } else {
            $('#terms_and_conditions').animate({ height: '0px' });
            $('#terms_and_conditions').removeClass('visible');
        }
    });
    
    $('#form-terms').click(function (event){

        event.preventDefault();

        if (!$('#terms_and_conditions').hasClass('visible')){
            $('#terms_and_conditions').animate({ height: '145px' });
            $('#terms_and_conditions').addClass('visible');
        } else {
            $('#terms_and_conditions').animate({ height: '0px' });
            $('#terms_and_conditions').removeClass('visible');
        }
    });
    
    $('#close').click(function (event){
        event.preventDefault();
        $('#terms_and_conditions').animate({ height: '0px' });
        $('#terms_and_conditions').removeClass('visible');
    });
    $.History.bind(function(state){
    	// Our Generic Handler - Always runs last.
    	if( $('#background-layer').css('display') == 'block')
        	hide_static_pages();

        if( $('#flash-wrapper object').length > 0 )
            show_content();
    });

    $.History.bind('gallery',function(state){

        if( !($.browser.msie && $.browser.version < 8) )
            $('#wrapper, #bottom').animate({ opacity : 0 }, 500);        

        $('#background-layer').animate({opacity: 1.0}, 500).fadeIn(500);
        $('#gallery').animate({opacity: 1.0}, 500).fadeIn(800);

//                                        div ID,   imagesbank,             horizon,size, zoom, border, autoscroll_start, autoscroll_interval
        setTimeout(function(){
                        imf.create("carousel", '/static/xml/gallery.xml', 1, 0.15, 1.5, 10);
                    }, 1300);
    });

    $.History.bind('about',function(state){

        if( !($.browser.msie && $.browser.version < 8) )
            $('#wrapper, #bottom').animate({ opacity : 0 }, 500);

        $('#background-layer').animate({opacity: 1.0}, 500).fadeIn(500);
        $('#about').animate({opacity: 1.0}, 600).fadeIn(800);

    });

    $.History.bind('close-statics',hide_static_pages);

    $(window).bind('keydown', function(e){
        if( e.keyCode == 27 && $('#nyroModalFull').length < 1 ) {
            location.href = '#close';
            hide_static_pages();
        }
    });
    
    $('#sign-up-form-form').submit(function(event){
        event.preventDefault();

        errors = [];

        if ( !$('#name').val() ) {
            showError('name');
        } else {
            hideError('name');
        }

        if ( !isEmail($('#email').val()) ) {
            showError('email');
        } else {
            hideError('email');
        }

        var data = { 
            name: $('#name').val(), 
            email: $('#email').val() 
        };
        
        if (errors.length > 0)  // there is an error so stop checking
            return false;

        if (errors.length == 0 ) {

            $.post("/signup.json", 
                                data,
                                function(){},
                                "json");

            $('#sign-up-form-form').hide();
            $('#thankyou').addClass('form-submitted').show();
            $('#sign-up-form').animate({ height: '65px' });
          
        } 
        
    });
});


function showContent(elts, settings, callback) {
    elts.contentWrapper
    .css({ // Reset the CSS at the start position
        marginTop: (-100/2 + settings.marginScrollTop)+'px',
        marginLeft: (-100/2 + settings.marginScrollLeft)+'px',
        height: '100px',
        width: '100px',
        opacity: 0
    })
    .show()
    .animate({ // Set the width
        width: settings.width+'px',
        marginLeft: (settings.marginLeft)+'px',
        opacity: 0.5
    }, {duration: 300})
    .animate({ // Set the height
        height: settings.height+'px',
        marginTop: (settings.marginTop)+'px',
        opacity: 1
    }, { complete: callback, duration: 400 });

    setTimeout(function(){
		        if( $('h1#nyroModalTitle') && settings.title != 'high'){
		          $('h1#nyroModalTitle').fadeIn();
		        }
		      }, 800);
}

function hideContent(elts, settings, callback) {
    elts.contentWrapper
    .animate({ // Set the height
        opacity: 0
    }, { complete: callback, duration: 300 });
}

function resizeModal(elts, settings, callback) {
    elts.contentWrapper
    .show()
    .animate({ // Set the width
        width: settings.width+'px',
        marginLeft: (settings.marginLeft)+'px'
    }, {duration: 300})
    .animate({ // Set the height
        height: settings.height+'px',
        marginTop: (settings.marginTop)+'px'
    }, { complete: callback, duration: 400 });
}

function lightbox_from_gal(url, text) {
    $title = typeof(text) == 'undefined' ? '' : ' title="' + text + '"';
    $('#temp_lightbox').remove();
    $('body').append('<div id="temp_lightbox" style="display:none"><a href="' + url + '"' + $title + '>clicky</a></div>');
    $('#temp_lightbox a').nyroModal();
    $('#temp_lightbox a').click();
}
