/* Copyright 2003-2009 Emergent Music LLC  All rights reserved.
 * $Id$
 */

FLYFI.onClick_coldstartmain_playlist_type = function(event) {
    var isSmart = $('#coldstartmain input:checked').hasClass('smart');
    FLYFI.preferences.setBoolean('coldstart_smart', isSmart);
    
    if (isSmart) {
        $('#coldstartmain #searchform_basic').hide();
        $('#coldstartmain #searchform').show();
    } else {
        $('#coldstartmain #searchform').hide();
        $('#coldstartmain #searchform_basic').show();
    }
};

FLYFI.onSubmit_searchform = function(event) {
    event.preventDefault();

    var form = $('#searchform');
    var artists = form.find('.artists').val();
    if (!artists) {
        alert('Please enter the names of some artists and try again.');
        return;
    }
    
    FLYFI.askServerForNewGroove(artists, artists);
    FLYFI.showPostStart();
    
    if (!FLYFI.isLikeIPhone() && !FLYFI.preferences.getBoolean('dontshow-newgrooveinterstitial', false)) {
        // the new groove dialog shows only the interstitial part since we are in coldstart
        $('#coldstartmain .artistsearch a.thickbox').click();
    }
	FLYFI.trackAjaxCall("createNewGroove");
};

FLYFI.onSubmit_searchform_basic = function(event) {
    event.preventDefault();
    
    var form = $('#searchform_basic');
    var artists = form.find('.artists').val();
    var tracks = form.find('.tracks').val();
    if (!artists || !tracks) {
        alert('Please enter ' + (!artists && !tracks ? 'an artist name and a track name.' : ((!artists) ? 'an artist name.' : 'a track name.')));
        return;
    }
    
    FLYFI.showPostStart();
    FLYFI.askServerForNewPlaylistWithTracks(artists, tracks);
	FLYFI.trackAjaxCall("createNewPlaylist");
};

FLYFI.onStart_ColdStart = function() {
    if (!FLYFI.preferences.getBoolean('coldstart_smart', true)) {
        $('#coldstartmain input.basic').click();
        FLYFI.onClick_coldstartmain_playlist_type();
    }
};

FLYFI.prepareForm_NewGroove_Coldstart = function() {
    // show only the intersticial dialog
    $('#new_groove_form').hide();
    $('#groove_interstitial_dialog').show();
    
    var dialog = $('#new_groove_dialog');
    dialog.find('.groovename').val('');
    dialog.find('.artists').val('').focus();
};

FLYFI.clearColdStart = function() {
    FLYFI.preferences.setBoolean(FLYFI.NOTCOLDSTART, true); // only show cold start once
    FLYFI.setIfPostStart();
};


$(document).ready(function(){
    var coldstartmain = $('#coldstartmain');
    coldstartmain.find('input').click(FLYFI.onClick_coldstartmain_playlist_type);
    coldstartmain.find('label.smart, span.smart').click(function() { $('#coldstartmain input.smart').click(); 
                                                            FLYFI.onClick_coldstartmain_playlist_type(); });
    coldstartmain.find('label.basic, span.basic').click(function() { $('#coldstartmain input.basic').click(); 
                                                            FLYFI.onClick_coldstartmain_playlist_type(); });
    coldstartmain.find('a.thickbox').click(FLYFI.prepareForm_NewGroove_Coldstart);

    var searchForm = $('#searchform');
    searchForm.submit(FLYFI.onSubmit_searchform);
    searchForm.find('.searchsubmit').click(FLYFI.onSubmit_searchform);

    $('#searchform_basic').submit(FLYFI.onSubmit_searchform_basic);
    $('#coldstartmain .lookaround').click(FLYFI.clearColdStart);
    $('#skrim').click(FLYFI.clearColdStart);    
    $(window).bind(FLYFI.MSG_Start, FLYFI.onStart_ColdStart);
});

