// JavaScript Document

//------ Video File Upload --- //
function checkVidForm(){
var error = '';	
	if(document.getElementById('vid_url').value == '' && document.getElementById('vid_title').value == '' && document.getElementById('vid_reason').value == '' &&document.getElementById('vid_loc').value == '' && document.getElementById('vid_camera').value == '' && document.getElementById('vid_tags').value == ''){
		error += 'All fields are Required.\n';
	}
	if(document.getElementById('vid_url').value == ''){
		error += 'Please Choose a Video File.\n';
	}
	
	if(document.getElementById('vid_title').value == ''){
		error += 'Enter Title.\n';		
	}
	if(document.getElementById('vid_reason').value == ''){
		error += 'Enter Your Reaseon to Smile.\n';		
	}
	if(document.getElementById('vid_loc').value == ''){
		error += 'Enter Video Location.\n';		
	}
	if(document.getElementById('vid_camera').value == ''){
		error += 'Enter Video Camera Used.\n';		
	}	
	if(document.getElementById('vid_tags').value == ''){
		error += 'Enter Video Tags.\n';		
	}
// up	
	if(error!=''){
		alert('Please Enter The Required Fields: \n\n'+error);
	}
	else{
		if(document.getElementById('vid_url').value != '' && error ==''){
			if(document.getElementById('vid_join').checked == true){
				document.getElementById('vid_join').value = 'y';
//				alert(">> "+document.getElementById('vid_join').value);
			}
			else{
				document.getElementById('vid_join').value = 'n';
//				alert(">> "+document.getElementById('vid_join').value);				
			}
			var action = 'videoUpload2.php';			
			startUpload2(action);			
		}
	}
}

function startUpload2(get) {	
		document.vidUpload.action = get;
		document.getElementById('result2').innerHTML = '';
		document.getElementById('f1_upload_process2').style.visibility = 'visible'; 
		return true;
}

function stopUpload2(success) { 
	var result = '';      
	if (success == 1){         
		document.getElementById('result2').innerHTML = '<span class="msg">The file was uploaded successfully!<br>Your reason to smile has been uploaded successfully! <br>Please wait for confirmation email from Canon to validate your submission.<\/span><br/><br/>';      
	}
	else {        
		document.getElementById('result2').innerHTML = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';     
	}      
	document.getElementById('f1_upload_process2').style.visibility = 'hidden';
	document.vidUpload.action = '';	
	document.vidUpload.reset();
	return true;   
}


function urlUpload(success) { 
	var result = '';      
	if (success == 1){    
		document.getElementById('result2').innerHTML = '<span class="msg">The URL was uploaded successfully!<br>Your reason to smile has been uploaded successfully! <br>Please wait for confirmation email from Canon to validate your submission.<\/span><br/><br/>';
	}
	else {        
		document.getElementById('result2').innerHTML = '<span class="emsg">There was an error during URL upload!<\/span><br/><br/>';     
	}      
	document.getElementById('f1_upload_process2').style.visibility = 'hidden';
	document.vidUpload.action = '';	
	document.vidUpload.reset();
	return true;   
}

//-------------------------------------------------------------------
var videoUrl;
var type;
var getUrl = '';
var display ='';
var vidObject = '';
var vidDescription = '';
var vidGetType = '';
var vid='';
var count = 0;
var imageSource = '';

var vid_id;
var vidTitle;
var vidReason;
var vidLoc;
var vidTags;
var custPk;


function screenShot(id){
	count = 0;
// Get Video URL
	xmlHttp = GetXmlHttpObject3();
	if (xmlHttp == null){
		alert ("Your browser does not support AJAX!");
		return;
	}
	  var url ="tryGetVideo.php?id="+id+"&rand="+Math.random();
	  xmlHttp.onreadystatechange=stateChanged3;
	  xmlHttp.open("GET",url,true);
	  xmlHttp.send(null);

//	  setTimeout('displayAvg()',1000);
   
}

function GetXmlHttpObject3(){
	var objXMLHttp=null;
	try{
	  // Firefox, Opera 8.0+, Safari
	  objXMLHttp=new XMLHttpRequest();
	}
	catch (e){
	  // Internet Explorer
	  try{
		objXMLHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	  catch (e){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	}
	return objXMLHttp;
}

function stateChanged3(){
	var count = 0;
   display ='<table cellpadding="2" cellspacing="1" class="tableDesc" align="center" border="0">';
   display +='<tr>';
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		xmlDoc = xmlHttp.responseXML;
		var videos = xmlDoc.getElementsByTagName("videoDetails");
	
		for(var i=0;i<videos.length;i++){
			var size = 'small';
			getUrl = videos[i].getElementsByTagName("videoUrl")[0].childNodes[0].nodeValue;
			urlTitle = videos[i].getElementsByTagName("vidTitle")[0].childNodes[0].nodeValue;
			reason = videos[i].getElementsByTagName("reason")[0].childNodes[0].nodeValue;
			urlLoc = videos[i].getElementsByTagName("loc")[0].childNodes[0].nodeValue;
			urlTags = videos[i].getElementsByTagName("tags")[0].childNodes[0].nodeValue;
			urlPk = videos[i].getElementsByTagName("pk")[0].childNodes[0].nodeValue;			
			memID = xmlDoc.getElementsByTagName("memPk")[0].firstChild.nodeValue;
			vidGetType = videos[i].getElementsByTagName("videoType")[0].childNodes[0].nodeValue;

			var videoTitleFull = htmlspecialchars(urlTitle,'ENT_QUOTES');
			var videoReasonFull = htmlspecialchars(reason,'ENT_QUOTES');			
			var videoLocationFull = htmlspecialchars(urlLoc,'ENT_QUOTES');						
			var videoTagsFull = htmlspecialchars(urlTags,'ENT_QUOTES');									


			 if(getUrl === null){ 
				return ""; 
			  }
			  
			  if(vidGetType == 'youtube'){
				imageSource = 'http://img.youtube.com/vi/'+getUrl+'/2.jpg';
			  }
			  
			  if(vidGetType == 'vimeo'){
				imageSource = 'images/vimeoLogo.jpg';
		
			  }
			  size = (size === null) ? "big" : size;

			  var results;

			  results = getUrl.match("[\\?&]v=([^&#]*)");
			
			  vid = ( results === null ) ? getUrl : results[1];

			  if(size == "small"){
			//  "http://img.youtube.com/vi/"+vid+"/2.jpg" http://www.youtube.com/watch?v=P2ofsvi3MI8
				
				if(count == 4){
					display +='</tr><tr>';
					count=0;
				}
				display +='<td align="center"><a href="javascript:buffVideo(\''+getUrl+'\',\''+videoTitleFull+'\',\''+videoReasonFull+'\',\''+videoLocationFull+'\',\''+videoTagsFull+'\',\''+urlPk+'\',\''+memID+'\',\''+vidGetType+'\');" onclick="javascript:document.getElementById(\'showAll\').style.display=\'none\';"><img src="'+imageSource+'" border=0 id="vidThumb"><br><font color="#FF0000">View Video</font></a></td><br>';
				count++;
			  }

		}
		display +='</tr></table>';
	}
//	 getScreen(getUrl,'small');
	document.getElementById("showAll").innerHTML=display;
}
	 
function buffVideo(videoUrl,urlTitle,reason,urlLoc,urlTags,urlPk,memID,vidGetType){
var videoSrc = '';
vid_id = urlPk;
vidTitle =  htmlspecialchars(urlTitle,'ENT_QUOTES');
vidReason =  htmlspecialchars(reason,'ENT_QUOTES');
vidLoc =  htmlspecialchars(urlLoc,'ENT_QUOTES');
vidTags =  htmlspecialchars(urlTags,'ENT_QUOTES');
custPk = memID;

if(vidGetType == 'youtube'){
	videoSrc = 'http://www.youtube.com/v/'+videoUrl+'&hl=en&fs=1&width=375&height=299';
	vidObject = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/'+videoUrl+'&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+videoUrl+'&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="325" height="244"></embed></object>';
}

else if(vidGetType == 'vimeo'){
	videoSrc = 'http://vimeo.com/'+videoUrl;
	vidObject = '<object width="400" height="344"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value=http://vimeo.com/moogaloop.swf?clip_id='+videoUrl+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id='+videoUrl+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="325" height="244"></embed></object>';
}

//<a href="javascript:void(0)" onclick="document.getElementById(\'vidPlayer\').style.display=\'none\';" style="color:#000000;" >Close </a>
	document.getElementById('vidStatus').style.display = 'block';

	document.getElementById('vidStatus').innerHTML = '<font face="Arial, Helvetica, sans-serif" size="-1">Loading. Please Wait </font> &nbsp;&nbsp;&nbsp; <img src="imgLoader.gif" border="0"/>';
	
	vidDescription = '<font face="Arial, Helvetica, sans-serif" size="-1" color="#FF0000"><b>REASON TO SMILE # '+urlPk+'</b></font><br/><br/><font face="Arial, Helvetica, sans-serif" size="-2">Title: '+urlTitle+'<br><br>Reason: <br>'+reason+'<br><br>Location: '+urlLoc+'<br><br>Tags: <font color="#FF0000">'+urlTags+'</font></font>';

/*	vidDescription += "<br><br><a href='http://www.addthis.com/bookmark.php?v=20' onmouseover=addthis_open(this,'','"+videoSrc+"','Canon'); onmouseout=addthis_close(); onclick=return addthis_sendto();><img src='http://s7.addthis.com/static/btn/sm-share-en.gif' width='83' height='16' alt='Bookmark and Share' style='border:0'/></a><script type='text/javascript' src='http://s7.addthis.com/js/200/addthis_widget.js'></script>&nbsp;&nbsp;&nbsp;<a href='javascript:vidUpdateForm();' style='color:#FF0000; text-decoration:none'><img src='images/edit_btn.jpg' border='0' id='share_btn' align='top'/></a>";*/

vidDescription += "<br><br><a href='http://addthis.com/bookmark.php?pub=small1026&url="+escape(videoSrc)+"&title=Canon' target='_blank'><img src='images/share_btn.gif' alt='Bookmark and Share' style='border:0'/></a>&nbsp;&nbsp;&nbsp;<a href='javascript:vidUpdateForm();' style='color:#FF0000; text-decoration:none'><img src='images/edit_btn.jpg' border='0' id='share_btn' align='top'/></a>";
	
	setTimeout('playVideo()',2000);
	
}


function playVideo(){
//	var videoSrc = 'http://www.youtube.com/v/'+videoUrl+'&hl=en&fs=1&width=375&height=299';
//<a href="javascript:void(0)" onclick="document.getElementById(\'vidPlayer\').style.display=\'none\';" style="color:#000000;" >Close </a>
	document.getElementById('vidStatus').innerHTML = '';
	document.getElementById('vidStatus').style.display = 'none';
	document.getElementById('container').style.display = 'block';
	document.getElementById('vidPlayer').style.display = 'block';
	document.getElementById('videoDesc').style.display='block';
	document.getElementById('videoDesc').innerHTML = vidDescription+'<br><div id="rate2" style="position:absolute;"><div style="position:relative;" id="rateMe" title="Rate Me..."><a id="__1" title="Bad"></a><a id="__2" title="Not Bad"></a><a id="__3" title="Good" ></a><a id="__4" title="Very Good"></a><a id="__5" title="Awesome!"></a><br /><div id="memCount2" align="left" style="position:absolute; left: 5px; top: 20px"></div></div></div>';
	document.getElementById('vidPlayer').innerHTML = vidObject;
	star_up2(vid_id,custPk)
}

function switchTab(tab,id){
	if(tab == 2){
		document.getElementById("showAll").innerHTML='';
		document.getElementById('videoGallery').style.display='block';
		document.getElementById('showAll').style.display='block';		
		document.getElementById('imgGallery').style.display='none';
		document.getElementById('container').style.display='none';
		document.getElementById('description').style.display='none';
		document.getElementById('view').style.display='none';
		document.getElementById('galleryView').style.display='none';
		document.getElementById('vidForm').style.display='none';		
		document.getElementById('imgFolder').style.display='none';		
		document.getElementById('videoDesc').innerHTML = '';
		screenShot(id);		
	}
	else if(tab == 1){
		document.getElementById('imgGallery').style.display='block';
		document.getElementById('imgFolder').style.display='block';				
		document.getElementById('imgView').style.display='none';
		document.getElementById('videoGallery').style.display='none';
		document.getElementById('container').style.display='none';
		document.getElementById("showAll").innerHTML='';
		document.getElementById('view').src='';
	}
}


function vidUpdateForm(){
	document.getElementById('videoDesc').style.display='none';	
	document.getElementById('vidForm').style.display='block';	
	document.getElementById('vid_id').value = vid_id;
	document.getElementById('vid_title').value = vidTitle;
	document.getElementById('vid_location').value = vidLoc;
	document.getElementById('vid_reason').value = vidReason;
	document.getElementById('vid_tags').value =vidTags;
}

function vidUpdate(){
	var error = '';	

	if(document.getElementById('vid_title').value == '' && document.getElementById('vid_reason').value == '' &&	document.getElementById('vid_location').value == '' && document.getElementById('vid_tags').value == ''){
		error += 'All fields are Required.\n';
	}
	if(document.getElementById('vid_title').value == ''){
		error += 'Enter Title.\n';		
	}
	if(document.getElementById('vid_reason').value == ''){
		error += 'Enter Your Reaseon to Smile.\n';		
	}
	if(document.getElementById('vid_location').value == ''){
		error += 'Enter Shot Location.\n';		
	}
	if(document.getElementById('vid_tags').value == ''){
		error += 'Enter Video Tags.\n';		
	}
	
	if(error!=''){
		alert('Please Enter The Required Fields: \n\n'+error);
	}
	
	else{
	document.getElementById('wizardBG').style.opacity=0;
	document.getElementById('wizardBG').style.display="block";
	new Effect.Fade('wizardBG', { duration:0.5, from:0.0, to:0.6 });

	document.getElementById('status').style.display="block";
	new Effect.Fade('status', { duration:0.5, from:0.0, to:1.0, delay: 0.5 }); 		
		var xmlHttp;
		  try{    // Firefox, Opera 8.0+, Safari    
			xmlHttp=new XMLHttpRequest();    
		  }
		  catch (e){    // Internet Explorer    
			try{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
			}
			catch (e){
				try{
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
				}
				catch (e){
				  alert("Your browser does not support AJAX!");        
				  return false;
				}      
			}    
		 }
			xmlHttp.onreadystatechange=function(){
				if(xmlHttp.readyState==4){
					new Effect.Fade('wizardBG', { duration:0.5, from:0.6, to:0.0, delay: 0.5 });
					new Effect.Fade('status', {delay: 0.0});
					document.getElementById('showDiv').innerHTML=xmlHttp.responseText;								
				}
				else{
				}
			}

			var param = 'rand=' + Math.random() + 
						'&vid_title=' + document.getElementById('vid_title').value + 
						'&vid_location=' + document.getElementById('vid_location').value + 
						'&vid_reason=' + document.getElementById('vid_reason').value + 
						'&vid_tags=' + document.getElementById('vid_tags').value + 
						'&vid_id=' + document.getElementById('vid_id').value ;

			xmlHttp.open('POST','vidUpdate.php', true);    
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");    
			xmlHttp.setRequestHeader("Content-length", param.length);    
			xmlHttp.setRequestHeader("Connection", "close");    
			xmlHttp.send(param);
	}
}