// JavaScript Document
var clipList = [];

/**
 * Function called by the Playoff Moments SWF file.
 * Jumps the user down the page to the correct box anchor
 * @param	type	String	"send" or "create" depending on the button clicked
 */
function swf_jumpTo (box)
{
         if(box == "send"){
			//PlayoffMoments.onClick_toggleForm(null, box);
			       $$('#emailbox .header')[0].scrollTo();
        }else if(box == "create"){
			//PlayoffMoments.onClick_toggleForm(null, box);
			       $('widget').scrollTo();
			
        }
        PlayoffMoments.resetForm();
}

/**
 * Function called by the Playoff Moments SWF file.
 * Updates the on-page forms to use the current video clip list
 * @param	list	String	Comma delimited list of video clip IDs.
 */
function swf_setClipIDList (list)
{

       var lister  = new String(list);
       clipList = lister.split(",");

	// Regenerate "Create a Widget" code using new list
	var fixedList = "";

	//create a fixed position id list from the comma delimited version
	for(var i=0;i < clipList .length; i++){
		fixedList += clipList [i];
	}

	// Update hidden field in "Send to a Friend" form with new list
	//document.getElementById("#ids#").value = fixedList ;
     $("#ids#").value = fixedList;


	$('message').update().hide();
	//if list is empty clear existing embed code
	if(fixedList.blank()){
               updateWidget();       
	}else{
               updateWidget();       
	}


}




function updateWidget(){
      var objId =  'W4aad22a6fd7d28de';

       
	// Regenerate "Create a Widget" code using new list
	var fixedList = "";

	//create a fixed position id list from the comma delimited version
	for(var i=0;i < clipList .length; i++){
		fixedList += clipList[i];
	}
       var template = new Template('http://widgets.clearspring.com/o/4aad2290465bc8b1/4aad22a6fd7d28de/4aad2290465bc8b1/a683427e/playlist/#{playlist}/title/#{title}');
       //var template = new Template('http://widgets.clearspring.com/o/49cd05a77c2c89af/49e3b1e7603db36f/49cd05a77c2c89af/c8b53c9a/playlist/#{playlist}/title/#{title}');

       var c2 = $F('category_2')
        var title = $F('category_1');
        if(!title.blank() && !c2.blank()){
              title += ' ';
        } 
        title += c2;
        title = encodeURIComponent(title);

       var content = {'playlist': fixedList ,'title': title};
       var newValue = template.evaluate(content );
      
	var flashvars = {};
	var params =  { 
                       wmode: 'transparent', 
                       allowscriptaccess: 'always', 
                       allownetworking: 'all',
                       type: 'movie',
                       no_flash: 'Sorry, you need to install flash to see this content.' 
        };
	var attributes = { id: objId, type: 'application/x-shockwave-flash'};
       swfobject.embedSWF(newValue, objId, "404", "709", "9.0.0", "/expectgreat/expressInstall.swf", flashvars, params, attributes);

}



var PlayoffMoments = {

	init : function(){

		//set initial view state
		$('formbox').isDown = false;


		//add event handlers

		$('sendEmail').observe('click', PlayoffMoments.onClick_sendEmail);

		$('category_1').observe('change', updateWidget);
		$('category_2').observe('change', updateWidget);


		//enable form validation
		PlayoffMoments.validator = new Validation('emailform', {immediate : true, onSubmit : false});
		Validation.add('validate-message', 'Please enter a valid message', {
			 minLength: 1,
			 maxLength : 100,
			 pattern : new RegExp("^[\\w\\s\.\\?]+$"),
			 include : ['required']


		});
          

/** Initialize the Playoff Moments SWF Object **/
var flashvars = {};
var params = {};
params.play = "true";
params.loop = "false";
params.menu = "false";
params.quality = "best";
params.scale = "exactfit";
params.wmode = "opaque";
params.allowscriptaccess = "always";
params.allownetworking = "true";
params.allowfullscreen = "true";
var attributes = {};
attributes.id = "PlayoffMomentsSWF";
attributes.name = "PlayoffMomentsSWF";
swfobject.embedSWF("/expectgreat/PlayoffMoments2009.swf", "PlayoffMoments", "908", "603", "9.0.0", "/expectgreat/expressInstall.swf", flashvars, params, attributes);


	},
       

       resetForm: function(){
	      $('emailform').reset();
	      PlayoffMoments.validator.reset();
	      $('message').hide();
       },

	onClick_toggleForm : function(event, box){
               //alert('toggling form');
		if($('formbox').isDown){
			$('formbox').isDown = false;
                       
			Effect.SlideUp('formbox', {
										queue:  { position: 'end', scope: 'formbox-scope', limit: 2 },
										duration: 1.5
									  }
			);
          

		}else{
			$('formbox').isDown = true;
          
			Effect.SlideDown('formbox',{
										queue:  { position: 'end', scope: 'formbox-scope', limit: 2 },
										duration: 1.5
									  }
			);
          
                     	if(Prototype.Browser.IE){
			       $('sendEmail').scrollTo();
                        }else{
                            window.scrollTo(0,1200);
                        }
		}

	},





	onClick_sendEmail : function(event){
	// Regenerate "Create a Widget" code using new list
	var fixedList = "";

	//create a fixed position id list from the comma delimited version
	for(var i=0;i < clipList .length; i++){
		fixedList += clipList[i];
	}


		$('message').hide();

		if(fixedList ==''){
			$('message').addClassName('error').update('You have not created a playlist').show();
		}else{

			var senderName = $F('#name#');
			var senderEmail  = $F('emailFrom');
			var subject = 'WNBA Players of Playoffs from '+senderName;
			if(PlayoffMoments.validator.validate()){
				$('emailform').request({
					method: 'post',
					parameters: {'subject': subject, '#sender#': senderName, '#senderEmail#': senderEmail, '#ids#': fixedList},
					onSuccess: function(){
						$('message').addClassName('success').update('Thanks! Your email has been sent').show();
					},
					onFailure: function(){
						$('message').addClassName('error').update('Sorry! Your email cannot be sent').show();
					}
				});
	                        $('emailform').reset();
	                        PlayoffMoments.validator.reset();
			}
		}

		Event.stop(event); // stop the form from submitting

	}

};

document.observe("dom:loaded", PlayoffMoments.init);



