/*
  yourshot-submit
    Backing code for the YourShot - submit image page

    JS dependencies:
      Set ys.extra_steps to # of steps between agreeing to rules and uploading photo.
      Set ys.uploads_remaining to # of upload the user has left in this cycle.
      Set ys.upload_limit to max # of upload the user may make in this cycle
      Set ys.upload_url to URL of uploader script
      Set ys.thanks_url to URL of thanks, you've reached quota page

    HTML dependencies:
      upload_box - Area in which to append additional upload boxes.
      another_upload_button
      upload_count - container, contents will be replaced with
        "N images" where N is number of successfully uploaded images
*/

var ys = new Object;
var yui = YAHOO.util;

//----------------------------------------------------------------
//                       Page Init
//----------------------------------------------------------------

ys.initialize = function() {
    ys.setupLoginSignup();
    ys.setupMultiUpload();
    ys.updateUploadCount();
};
form.safeInit(ys.initialize);

//----------------------------------------------------------------
//                       Identify yourself
//----------------------------------------------------------------

ys.setupLoginSignup = function() {

    var loginRadio  = new yui.Element('id_login');
    loginRadio.addListener('click',  ys.loginModeClicked);
    if (loginRadio.get('checked')) { ys.loginModeClicked(); }

    var signupRadio = new yui.Element('id_signup');
    signupRadio.addListener('click',  ys.signupModeClicked);
    if (signupRadio.get('checked')) { ys.signupModeClicked(); }

};

ys.loginModeClicked = function(evt) {
    var loginFrame  = new yui.Element('login_frame');
    var signupFrame = new yui.Element('signup_frame');
    signupFrame.setStyle('display', 'none');
    loginFrame.setStyle('display', 'block');

};

ys.signupModeClicked = function(evt) {
    var loginFrame  = new yui.Element('login_frame');
    var signupFrame = new yui.Element('signup_frame');
    loginFrame.setStyle('display', 'none');
    signupFrame.setStyle('display', 'block');

};

//----------------------------------------------------------------
//                       Agree to Rules
//----------------------------------------------------------------

ys.noMoreLegalDocs = function() {
    var legalFrame = new yui.Element('legal_frame');
    legalFrame.setStyle('display', 'none');
    var legalAgreed = new yui.Element('legal_agreed');
    legalAgreed.setStyle('display', 'block');

    var step2 = new yui.Element('step2header');
    step2.replaceClass('ys_step_active', 'ys_step_inactive');

    //ys.thirdStepHandler();
    ys.nextStep(false);
};

ys.noLegalDocs = function() {
    var legalFrame = new yui.Element('legal_frame');
    legalFrame.setStyle('display', 'none');
    var legalNone = new yui.Element('legal_none');
    legalNone.setStyle('display', 'block');

    var step2 = new yui.Element('step2header');
    step2.replaceClass('ys_step_active', 'ys_step_inactive');

    //ys.thirdStepHandler();
    ys.nextStep(false);
};

//----------------------------------------------------------------
//                       Arbitrary Steps
//----------------------------------------------------------------

ys.nextStep = function(hide) {
    // hide old iframe/div
    if( hide == true ) {
        var oldFrame = new yui.Element('step'+ys.current_step);
        oldFrame.setStyle('display','none');
    }
    // make old step title inactive
    var oldStepTitle = new yui.Element('step'+ys.current_step+'header');
    oldStepTitle.replaceClass('ys_step_active','ys_step_inactive');
    // increment current_step
    ys.current_step++;
    // make new step title active
    var newStepTitle = new yui.Element('step'+ys.current_step+'header');
    newStepTitle.replaceClass('ys_step_inactive','ys_step_active');
    // show new iframe
    var newFrame = new yui.Element('step'+ys.current_step);
    newFrame.setStyle('display','block');
}

ys.thirdStepHandler = function() {
    //TODO probably a better check than this
    if ( ys.extra_steps > 0 ) {
        ys.nextStep();
    }
    else {
        var step3 = new yui.Element('step3');
        step3.replaceClass('ys_step_inactive', 'ys_step_active');
        ys.displayUpload();
    }
}


ys.displayUpload = function() {
    var upload = new yui.Element('image_upload');
    upload.setStyle('display', 'block');
}

//----------------------------------------------------------------
//                  Multi-Upload Form Builder
//----------------------------------------------------------------
ys.setupMultiUpload = function() {
    if (!ys.uploads_remaining) { return; }

    ys.upload_forms_remaining = ys.uploads_remaining;

    yui.Event.addListener('another_upload_button', 'click', ys.showAnotherUpload);
    yui.Event.addListener('finished_button', 'click', ys.finishedUploading);

    // Always display at least one form
    ys.showAnotherUpload();
};

ys.finishedUploading = function() {
     window.location = ys.thanks_url;
}

ys.showAnotherUpload = function() {
    if (ys.upload_forms_remaining == 0) { return; }
    ys.makeUploadForm(ys.upload_forms_remaining);
    ys.upload_forms_remaining--;

    if (ys.upload_forms_remaining == 0 ) {
        yui.Dom.setStyle('another_upload_button', 'display', 'none');
    }
};

ys.uploadFormTemplate = ''
    + '<div class="ys_upload_form" id="ys_upload_form_TEMPLATE">'
    + '  <iframe border="0" frameborder="0" class="ys_upload_frame" id="ys_upload_frame_TEMPLATE" name="upload_TEMPLATE" width="100%" height="500"></iframe>'
    + '  <div class="ys_upload_sending" id="ys_upload_sending_TEMPLATE" style="display:none">'
    + '    <table cellspacing="2"><tr><td>'
    + '      <img src="/img/wait.gif" alt="Please wait" style="background-color:white"/>'
    + '    </td><td valign="middle">'
    + '      Uploading your image titled "<strong id="ys_upload_title_sending_TEMPLATE"></strong>"...'
    + '    </td></tr></table>'
    + '  </div>'
    + '  <div class="ys_upload_success" id="ys_upload_success_TEMPLATE" style="display:none">'
    + '    <strong id="ys_upload_title_success_TEMPLATE"></strong>&nbsp; was uploaded successfully.'
    + '  </div>'
    + '</div>';

ys.makeUploadForm = function(upload_id) {
    var html = ys.uploadFormTemplate;
    html = html.replace(/TEMPLATE/g, upload_id);

    var newForm = document.createElement('div');
    var uploadList = yui.Dom.get('upload_box');
    uploadList.appendChild(newForm);

    newForm.innerHTML = html;

    // Set iframe source, causing it to load
    var frame = yui.Dom.get('ys_upload_frame_' + upload_id);
    frame.src = ys.upload_url + '?rand=' + Math.random(); // Needed to defeat IE6 caching
};


//----------------------------------------------------------------
//                    Multi-Upload Interframe Link
//----------------------------------------------------------------

ys.resizeUploadFrame = function(upload_id, h) {
    var frame = yui.Dom.get('ys_upload_frame_' +  upload_id);
    //alert("Adjust frame heigth request with height: " + h);
    frame.height = h + 10;
};

ys.updateUploadCount = function() {
    var elem = yui.Dom.get('upload_count');
    if (!elem) { return; }
    var count = ys.upload_limit - ys.uploads_remaining;
    var msg = count + " image" + (count == 1 ? '' : 's');
    elem.innerHTML = msg;
};


ys.uploadBegan = function(upload_id, title) {

    // Hide frame
    // Note: we now assume the frame will hide its contents
    //yui.Dom.setStyle('ys_upload_frame_' +  upload_id, 'display', 'none');

    // Set image title
    yui.Dom.get('ys_upload_title_sending_' + upload_id).innerHTML = title;

    // Show loading
    yui.Dom.setStyle('ys_upload_sending_' + upload_id, 'display', 'block');

    // Show another automatically?
    //if (ys.uploads_remaining > 0) {
    //    ys.showAnotherUpload();
    //}

};

ys.uploadSucceeded = function(upload_id, title, html) {
    ys.uploads_remaining--;


    // If that was the last, redir to thanks
    if (ys.uploads_remaining == 0) {
        window.location = ys.thanks_url;
    } else {
        // Hide loading
        yui.Dom.setStyle('ys_upload_sending_' + upload_id, 'display', 'none');

        // Show success
        // TODO - custom, per-contest success message?

        // Set image title, if available
        var titleElem = yui.Dom.get('ys_upload_title_success_' + upload_id);
        if (titleElem) { titleElem.innerHTML = title; }

        yui.Dom.setStyle('ys_upload_success_' + upload_id, 'display', 'block');

        // Hide the upload form frame
        yui.Dom.setStyle('ys_upload_frame_' +  upload_id, 'display', 'none');

        ys.updateUploadCount();
    }
};


ys.uploadFailed = function(upload_id) {
    // Hide loading
    yui.Dom.setStyle('ys_upload_sending_' + upload_id, 'display', 'none');

    // Show frame
    // (Not needed if frame is never hidden)
    //yui.Dom.setStyle('ys_upload_frame_' +  upload_id, 'display', 'block');

};


/*

*/

