
function changeCelebration(key){
    //alert('dd ' + key);
    $("input#celebration-entry").val(key)
}

/* 
 * Site specific javascript functions
*/
function showGrid(){
    var overlay = '<div id="grid"></div>';
    $('body').append(overlay);
    $('#grid').click(function(){
        $(this).remove();
    })
}

/**
 *Embed movie on different pages with their own params
 */
function embedMovie(flashtitle,flashwidth,flashmovie,flashbuster){
    /* On page load set info for document */
    var pageVars = { /*{/literal}*/
        title:     flashtitle,
        width:     flashwidth,
        movie:     flashmovie,
        expressinstall: asset_url+"/swf/expressinstall.swf"
    /*{literal}*/ };
    
    var gallery_flash_vars = {
        filterType:"winners",
        swfURL:base_url+"/swf/gallery.swf" + flashbuster,
        amfURL:base_url+'/api',
        language: site_lang
    };

    var flash_vars = {
        language: site_lang
    };

    var gallery_params = {
        menu: "false",
        bgcolor: "#FFFFFF",
        wmode: "opaque",
        allowscriptacces: "always",
        allowfullscreen: "true"
    }

    var gallery_attributes = {
        id: "swf",
        name: "swf"
    }
    movie_url = pageVars.movie + flashbuster;

    var itemSwitch = pageVars.title;

    switch( itemSwitch ){
        case "Index":
            hideNav();
            swfobject.embedSWF( movie_url,"flash-index",pageVars.width,"650px","9.0.0",null, null,{
                wmode:'opaque'
            });
            break;
        case "Splash":
            hideNav();
            swfobject.embedSWF( movie_url,"flash-index",pageVars.width,"650px","9.0.0",null, null,{
                wmode:'opaque'
            });
            break;
        case "Home":
            swfobject.embedSWF( movie_url,"flash-index",pageVars.width,"650px","9.0.0",null, null,{
                wmode:'opaque'
            });
            break;
        case "Gallery":
            gallery_flash_vars.filterType = "all";
        case "Winners":
            swfobject.embedSWF( movie_url,"flash-image",pageVars.width,"575px","9.0.0",null,gallery_flash_vars, gallery_params, gallery_attributes );
            break;
        case "Design":
            swfobject.embedSWF( movie_url,"flash-image",pageVars.width,"600px","9.0.0",null, {
                amf_url: base_url+"/api", swf: "/swf/", localeURL: "/locale/",
                language: site_lang

            },{
                wmode:'opaque'
            });
            break;
    }
}
/** Emtry page js */
function entryPage( base,showIfYes, showIfRegister){
    var showYes = function(){
        $('#ifYes').show();
    }
    var hideYes = function(){
        $('#ifYes').hide();
    }

    var showRegister = function(){
        $('#ifRegister').show();
    }
    var hideRegister = function(){
        $('#ifRegister').hide();
    }

    $('input#submit').attr('src',asset_url+'/img/form_submit.png');
    $('input#submitbut').attr('src',asset_url+ '/img/signin_button.jpg');

    if(!showIfYes)
        hideYes();

    $('#q4-yes').click(function(){
        showYes();
    });

    $('#q4-no').click(function(){
        hideYes();
    });

    if(!showIfRegister)
        hideRegister();

    $('#entry-register-registeryes').click(function(){
        showRegister();
    });

    $('#entry-register-registerno').click(function(){
        hideRegister();
    });


}

/* Hide navigation */
function hideNav(){
    $("#hd").hide();
    $("#bd").hide();
    $("#ft").hide();
}
/* Show nav */
function showNav(){
    $("#hd").fadeIn(4500);
    $("#bd").fadeIn(4500);
    $("#ft").fadeIn(4500);

}
 
/* Set nav active for winner/gallery  */
function setType(param){
    switch( param ){
        case "winners":
            $("li#tab5 a").addClass( 'active' );
            $("li#tab4 a").removeClass( 'active' );
            $("p.weekly-winner-disclaimer").show();
            break;
        case "all":
            $("li#tab5 a").removeClass('active');
            $("li#tab4 a").addClass( 'active' );
            $("p.weekly-winner-disclaimer").hide();
            break;
    }
}

/* terms popup */
function getTerms(submit){
      
    switch( submit ){
        case "submit":
            openMyModal('/modal/submit');
            break;
        case "photo":
            openMyModal('/modal/photo');
            break;
    }
}

/** Modal Window **/
var modalWindow = {
    parent:"body",
    windowId:null,
    content:null,
    width:null,
    height:null,
    close:function()
    {
        $(".modal-window").remove();
        $(".modal-overlay").remove();
    },
    open:function()
    {
        var modal = "";
        modal += "<div class=\"modal-overlay\"></div>";
        modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";
        modal += this.content;
        modal += "</div>";

        $(this.parent).append(modal);

        $(".modal-window").append("<a class=\"close-window\"></a>");
        $(".close-window").click(function(){
            modalWindow.close();
        });
        $(".modal-overlay").click(function(){
            modalWindow.close();
        });
    }
};

var openMyModal = function(source)
{
    modalWindow.windowId = "myModal";
    modalWindow.width = 460;
    modalWindow.height = 250;
    modalWindow.content = "<iframe width='480' height='405' frameborder='0' scrolling='no' allowtransparency='true' src='" + source + "'></iframe>";
    modalWindow.open();
};

function loginAction(is_auth,user_info){
    $("#link-logout").hide();
    $("form#form-signin").hide();
    $("#form-reset-pass").hide();
    if( is_auth ){
        showAuth( user_info );
    }
}

function showAuth( user_info ){
    $("form#form-signin").hide();
    $("#link-login").hide();
    $("#link-logout").fadeIn( 1000 );
    $("span#user-first").replaceWith('<span id="user-first">'+ user_info + '</span>');
    $("div#choose-lang").show();
}

function signUserOut(){
    $.get(base_url + "/users/logout/format/json/", null, null, null, null);
    return false;
}



function showLogin(){
    $("#link-login").hide();
    $("div#choose-lang").hide();
    $("#form-signin").fadeIn( 1000 );

    $('input#username').inputDefaultState({
        text: enteremail
    });
    $('input#password').inputDefaultState({
        text: enterpass
    });
}

var registerEvents = function(){
    $('input#loginsubmit, input#resetsubmit').each(
        function(){
            $(this).attr('src', base_url + $(this).attr('src'));
        }
        );
	
    $("a#login-signin").click( function(){
        showLogin();
        return false;
    } );

    $("a#login-signout").click( function(){
        $("#link-logout").hide();
        $("#link-login").fadeIn( 1000 );
        signUserOut();
        loginAction();
        return false;
    });

    $("a#forgot-password").click(
        function(){
            forgotPassword();
            return false;
        });

    $("input#loginsubmit").click( function(){
        var loginusername = $("input#username").val();
        var loginpassword = $("input#password").val();
        if( (('enter email' == loginusername) ||( null == loginusername) ) ||( ('enter password' == loginpassword) || ('' == loginpassword) )){
            alert( loginsubmit );
            //showPassMessage();
            return false;
        }
        passwordSubmit( loginusername, loginpassword);
        return false;
    });

    $("input#resetsubmit").click( function(){
        var resetemail = $("input#resetemail").val();
        resetSubmit( $.trim( resetemail )  );
        return false;
    });

}

function forgotPassword(){
    $("#form-signin").hide();
    $("#form-reset-pass").fadeIn(1000);
    $('input#resetemail').inputDefaultState({
        text: enteremail
    });
    return false;
}

function passwordSubmit(loginusername,loginpassword){

    $.post(base_url + "/users/login/format/json/", {
        username: loginusername,
        password: loginpassword
    }, function(data){
        //showPassSubmit( data )
        if(data.status) {
            if( current_page.toLowerCase() == 'entry'){
                location.reload( true );
            }
            showAuth(data.message);

        }
        else{
            alert(data.message);
        }
    } ,'json');
    return false;

}

function showPassSubmit( data ){
    
    if( 1 < data.length ){
        showAuth( data );
    }
    return false;
}

function resetSubmit( resetemail){
    $.post(base_url + "/users/resetpass/format/json/", {
        resetemail: resetemail
    }, function( data ){
        //showResetPassMessage( data )
        if(!data.status){
            alert(data.message);
        }
        else{
            showResetPassMessage( data.message );
        }
    }, "json");
    return false;
}


function showResetPassMessage( resetmessage ){
    $("input#resetsubmit").hide();
    $("input#resetemail").hide();
    $("p.retrieve-password").replaceWith('<p id="reset-message">'+ resetmessage + '</p>');
    return false;
}

function showPassMessage(){
    $("form#form-signin").hide();
    $("div#login-top").append('<p id="reset-message">'+probmessage+'</p><a id="userpass-reset" href="">Back</a>');
    $("a#userpass-reset").click(function(){
        $("p#reset-message").remove();
        $("a#userpass-reset").remove();
        showLogin();
        return false;
    }
    );
    return false;
}

$.fn.inputDefaultState = function(options){
    var options = jQuery.extend( {
        text: 'Enter Your Access Code'
    },options);
 
    $(this)
    .val(options.text)
    .bind('focus', function(){
        if($(this).val() == options.text)
            $(this).val('');
    });
	
}

// handle modal for leaving site

var leavingDialog = function(){
    var curLink = undefined;
	
    $('#leaving').dialog({
        modal: true ,
        title: before_you_go,
        autoOpen:false,
        bgiframe: true
    });
    $('#leaving-proceed').click(function(){
        $('#leaving').dialog('close');
        window.open(curLink, 'newWindow');
        return false;
    });

    $('#leaving-stay').click(function(){
        $('#leaving').dialog('close');
        return false;
    });

    $('#footer-box-admission a, #footer-box-dvd a').click(function(){
        curLink = $(this).attr('href');
        $('#leaving').dialog('open');
        return false;
    });
}