/**
 * @author Matt
 * 
 * SWFUpload Initialization file for uploading video and pictures to pluck
 * 
 * uses the MIT license and source from http://swfupload.org
 */
var swfu;
//var swfuPrefix;

// these are used if we use the 'SWFObject.js' include.
function swfuploadPreLoad() {};
function swfuploadLoadFailed() { 
document.getElementById("flup_Note").innerHTML = "<div id=\"flup_requirements\"> Your browser doesn't meet the minimums required to upload videos. Please install Flash and/or Enable Javascript to upload videos.</div>";
};

// quick cookie checks to see that the current user is Logged In
// and returns the userId from either the at or hd cookie
function isPluckLoggedIn() {
	var userKey = null;
	var cookie = function(name){
		var dc = document.cookie;// this returns/reads all cookies for the domain separated by a semicolon
		var prefix = name + "=";// creates "hd=" ("hd" is passed in to the function)
		var begin = dc.toLowerCase().indexOf("; " + prefix);// returns a -1 or whole number for position of "; hd"
		if (begin == -1) { // "; hd" string does not exist - meaning that hd is the first cookie in the collection of cookies for the domain
			begin = dc.indexOf(prefix); //begin is 0 since prefix ("hd") is at the 0 position
			if (begin != 0) {
				return null;
			} //begin should be at index 0 - fail if not
		}
		else {
			begin += 2; // add 2 to current begin -- not sure why. perhaps this is useful if hd is not the first cookie for the domain
		}
		var end = document.cookie.indexOf(";", begin); // search the cookie collection for ";", start the search at begin
		if (end == -1) { // end does not exist so there is only one cookie for the domain
			end = dc.length; // only one cookie for the domain so the end is the length
		}
		return unescape(dc.substring(begin + prefix.length, end)); // return the values between the end of "hd=" and the end of the cookie
	}
	var atCookieValue = function(query,param){
	      var keyValuePairs = new Array();
	        for(var i=0; i < query.split("&").length; i++) {
	          keyValuePairs[i] = query.split("&")[i];
	        }
	      var a = new Array(keyValuePairs.length);
	      for(var j=0; j < keyValuePairs.length; j++) {
	         a[j] = keyValuePairs[j].split("=")[0];
	      }
	      for(var j=0; j < keyValuePairs.length; j++) {
	        if(keyValuePairs[j].split("=")[0] == param)
	           return( keyValuePairs[j].split("=")[1]);
	        }
		}

	var hd = cookie("hd");
	var at = cookie("at");
	if (hd != null)  // we have an HD cookie
		userKey = unescape(hd.split("|")[0]);
	if (at != null) { // we have an AT cookie
		userKey = atCookieValue(at,"u");
	}
    return userKey;
}

	
function uploadInitialize(galleryKey, parentElement, plkSection, plkTags, isVideo){
	var fileTypes = "*.mpg;*.mpeg;*.mp4;*.mpg4;*.mpeg4;*.vob;*.3gp;*.avi;*.asf;*.wmv;*.mov;*.flv";
	var fileDescription = "All Video Files";
	if (isVideo == false) {
		fileTypes = "*.png;*.jpg;*.gif;*.bmp;*.tif";
		fileDescription = "All Image Files";
	}
//	swfuPrefix = parentElement;
	var loggedInUser = isPluckLoggedIn();
	if (loggedInUser != null) {
		var settings = {
			flash_url: "http://www.bullsconnect.com/videos/scripts/swfupload.swf",
			upload_url: "http://pluckcb.bullsconnect.com/ver1.0/Video/Upload", // Relative to the SWF file
			post_params: {
				"galleryKey": galleryKey 	
				,
				"userKey": loggedInUser  
			}, 
			minimum_flash_version: "9.0.28",
			swfupload_pre_load_handler: swfuploadPreLoad,
			swfupload_load_failed_handler: swfuploadLoadFailed,
			
			button_image_url: "http://www.bullsconnect.com/videos/images/button.gif",
			button_width: "80",
			button_height: "22",
			button_placeholder_id: "swfuploadContainer",
			button_text: '<span class="theFont"></span>',
			button_text_style: ".theFont { font-size: 13; }",
			button_text_left_padding: 12,
			//button_text_top_padding: 3,

      			
			file_size_limit: "100 MB",
			file_types: fileTypes,
			file_types_description: fileDescription,
			file_upload_limit: 10,
			file_queue_limit: 10,
			custom_settings: {
				progressTarget: parentElement + "UploadProgress",//"fsUploadProgress",
				enableBatchButton: false,     // enable this for the batch button upload - need some more work 
				isVideo: isVideo,
				plkSection: plkSection,
				plkTags: plkTags
			},
			// enable debug if something isn't working correctly
			debug: false,
			use_query_string: true,
			
			// The event handler functions are defined in handlers.js
			file_queued_handler: fileQueued,
			file_queue_error_handler: fileQueueError,
			file_dialog_complete_handler : fileDialogComplete,
			upload_start_handler: uploadStart,
			upload_progress_handler: uploadProgress,
			upload_error_handler: uploadError,
			upload_success_handler: uploadSuccess,
			upload_complete_handler: uploadComplete,
			queue_complete_handler: queueComplete // Queue plugin event
		};
		swfu = new SWFUpload(settings);
		swfu.debug("Pluck user: " + loggedInUser);
	}
	else 
		document.getElementById("flup_Note").innerHTML = "<div id=\"flup_signin\"> Must Login to Submit Video</div>";
}


