/**
 * flowplayer.playlist.js 3.0.1. Flowplayer JavaScript plugin.
 * 
 * This file is part of Flowplayer, http://flowplayer.org
 *
 * Author: Tero Piirainen, <support@flowplayer.org>
 * Copyright (c) 2008 Flowplayer Ltd
 *
 * Dual licensed under MIT and GPL 2+ licenses
 * SEE: http://www.opensource.org/licenses
 * 
 * Version: 3.0.1 - Mon Dec 15 2008 14:01:40 GMT-0000 (GMT+00:00)
 */
(function($) {
	
	$f.addPlugin("playlist", function(wrap, options) {
	
		// self points to current Player instance
		var self = this;	
		
		var opts = {
			playingClass: 'playing',
			pausedClass: 'paused',
			progressClass:'progress',
			template: '<a href="${url}">${title}</a>',
			loop: false,
			playOnClick: true
		};		
		
		$.extend(opts, options);
		wrap = $(wrap);		
		var manual = self.getPlaylist().length < 2; 
		var els = null;
		

		/* setup playlists with onClick handlers */ 
		
		// template based playlist
		if (!manual) {
			
			var template = wrap.is(":empty") ? opts.template : wrap.html(); 
			wrap.empty(); 
				
			$.each(self.getPlaylist(), function() {
					
				var clip = this;
				
				var el = template;	
				
				$.each(clip, function(key, val) {				
					el = el.replace("$\{" +key+ "\}", val).replace("$%7B" +key+ "%7D", val);			
				});				 
				
				el = $(el);	
				
				el.click(function() {				
					return play(el, clip.index);
				});			
				
				wrap.append(el);		
			});
			
			els = wrap.children();		
			
			
		// HTML based playlist
		} else {
			
			els = wrap.children();
			if (els.eq(0).hasClass("__scrollable")) { els = els.children(); }
			
			els.each(function() {
				var el = $(this);
				el.click(function() {
					return play(el, el.attr("href"));
				});
			});						 
					
			// setup player to play first clip
			var clip = self.getClip(0);
			if (!clip.url && opts.playOnClick) {
				clip.update({url: els.eq(0).attr("href")});		
			}   
			
		}
		
		function play(el, clip)  {
		
			if (el.hasClass(opts.playingClass) || el.hasClass(opts.pausedClass)) {
				self.toggle();
				
			} else {
			 
			   el.addClass(opts.progressClass);	
			   setClipLoadAndPlay(clip);		    
			   //$f(0).play(clip);				
			   /*
			    if(clip.indexOf("http://")==-1)
			    {
			    	$(function() {
			    	$f("player", {src: imgPathPrefix+'swf/flowplayer-3.0.3.swf',wmode:"opaque"},
					{				
						clip: {autoplay:true,baseUrl: 'http://'+baseURLVal+imgPathPrefix}	
					// playlist plugin 
					}).play(clip)
					}); 
			    }   
			    else
			    {   
			        
			    	clip = removeRelativePrefix(clip);
			    	$(function() {
			    	$f("player", {src:imgPathPrefix+'swf/flowplayer-3.0.3.swf',wmode:"opaque"},
					{				
						clip: {autoplay:true,baseUrl: ''}	
					// playlist plugin 
					}).play(clip)
					});
					//$f("player");
					
			    }
			    */
				//$f("player").play(clip); 		
				 			
				var tmp_var=el.attr("id");			     
			    tmp_var = tmp_var.split('_');
			    if(tmp_var.length>2)
			    {
					var index = tmp_var[1];
					var index1 = tmp_var[2];
					if(index=='f')
					{
						document.getElementById('video_name').innerHTML=fetVideoArray[index1][2];
		    			document.getElementById('video_shortdsc').innerHTML=fetVideoArray[index1][3];
		    			document.getElementById('video_longdsc').innerHTML=fetVideoArray[index1][6];
		   		       document.getElementById('video_length').innerHTML=fetVideoArray[index1][4]+' | '+fetVideoArray[index1][5];
		   			//    document.getElementById('videoRaR').src='ScheelsVideoReviewAndRatingsView?productId='+fetVideoArray[index1][1]+'&partNumber='+fetVideoArray[index1][9]+'&catName='+fetVideoArray[index1][12];
					}
					else if(index=='ex')
					{
						document.getElementById('video_name').innerHTML=prodVidArray[2];
		    			document.getElementById('video_shortdsc').innerHTML=prodVidArray[3];
		    			document.getElementById('video_longdsc').innerHTML=prodVidArray[6];
		   			    document.getElementById('video_length').innerHTML=prodVidArray[4]+' | '+prodVidArray[5];
		   			  //  document.getElementById('videoRaR').src='ScheelsVideoReviewAndRatingsView?productId='+prodArray[index][index1][1]+'&partNumber='+prodArray[index][index1][9]+'&catName='+prodArray[index][index1][12];
	   			    }
					else
					{
						document.getElementById('video_name').innerHTML=prodArray[index][index1][2];
		    			document.getElementById('video_shortdsc').innerHTML=prodArray[index][index1][3];
		    			document.getElementById('video_longdsc').innerHTML=prodArray[index][index1][6];
		   			    document.getElementById('video_length').innerHTML=prodArray[index][index1][4]+' | '+prodArray[index][index1][5];
		   			 //   document.getElementById('videoRaR').src='ScheelsVideoReviewAndRatingsView?productId='+prodArray[index][index1][1]+'&partNumber='+prodArray[index][index1][9]+'&catName='+prodArray[index][index1][12];
	   			    }
   			    }	
			}			
			
			return false;
		}	
		
		
		function clearCSS() {
			els.removeClass(opts.playingClass);
			els.removeClass(opts.pausedClass);
			els.removeClass(opts.progressClass);			
		}
		
		function getEl(clip) {		
			return (manual) ? els.filter("[@href=" + clip.url + "]") : els.eq(clip.index);	
		}
		
		// onBegin
		self.onBegin(function(clip) {
			clearCSS();		
			getEl(clip).addClass(opts.playingClass);
		});	
		
		// onPause	
		self.onPause(function(clip) {
			getEl(clip).removeClass(opts.playingClass).addClass(opts.pausedClass);		
		});	
		
		// onResume
		self.onResume(function(clip) {
			getEl(clip).removeClass(opts.pausedClass).addClass(opts.playingClass);		
		});		
		
		// what happens when clip ends ?
		if (!opts.loop && !manual) {
			
			// stop the playback exept on the last clip, which is stopped by default
			self.onBeforeFinish(function(clip) {
				if (clip.index < els.length -1) {
					return false;
				}
			}); 
		}
		
		// on manual setups perform looping here
		if (manual && opts.loop) {
			self.onBeforeFinish(function(clip) {
				var el = getEl(clip);
				if (el.next().length) {
					el.next().click();	 		
				} else {
					els.eq(0).click();	
				} 
				return false;				
			}); 
		}
		
		// onUnload
		self.onUnload(function() {
			clearCSS();		
		});
		
		
		return self;
		
	});
		
	
	$f.addPlugin("playlist1", function(wrap, options) {
	
		// self points to current Player instance
		var self = this;	
		
		var opts = {
			playingClass: 'playing',
			pausedClass: 'paused',
			progressClass:'progress',
			template: '<a href="${url}">${title}</a>',
			loop: false,
			playOnClick: true
		};		
		
		$.extend(opts, options);
		wrap = $(wrap);		
		var manual = self.getPlaylist().length < 2; 
		var els = null;
		

		/* setup playlists with onClick handlers */ 
		
		// template based playlist
		if (!manual) {
			
			var template = wrap.is(":empty") ? opts.template : wrap.html(); 
			wrap.empty(); 
				
			$.each(self.getPlaylist(), function() {
					
				var clip = this;
				
				var el = template;	
				
				$.each(clip, function(key, val) {				
					el = el.replace("$\{" +key+ "\}", val).replace("$%7B" +key+ "%7D", val);			
				});				 
				
				el = $(el);	
				
				el.click(function() {				
					return play(el, clip.index);
				});			
				
				wrap.append(el);		
			});
			
			els = wrap.children();		
			
			
		// HTML based playlist
		} else {
			
			els = wrap.children();
			if (els.eq(0).hasClass("__scrollable")) { els = els.children(); }
			
			els.each(function() {
				var el = $(this);
				el.click(function() {
					return play(el, el.attr("href"));
				});
			});						 
					
			// setup player to play first clip
			var clip = self.getClip(0);
			if (!clip.url && opts.playOnClick) {
				clip.update({url: els.eq(0).attr("href")});		
			}   
			
		}
		
		function play(el, clip)  {
		
			if (el.hasClass(opts.playingClass) || el.hasClass(opts.pausedClass)) {
				self.toggle();
				
			} else {
				el.addClass(opts.progressClass);
				 setClipLoadAndPlay(clip);
				//self.play(clip); 	
				/*
				if(clip.indexOf("http://")==-1)
			    {
			    	$(function() {
			    	$f("player", {src:imgPathPrefix+'swf/flowplayer-3.0.3.swf',wmode:"opaque"},
					{				
						clip: {autoplay:true,baseUrl: 'http://'+baseURLVal+imgPathPrefix}	
					// playlist plugin 
					}).play(clip)
					}); 
			    }   
			    else
			    {
			    	clip = removeRelativePrefix(clip); 
			    	$(function() {
			    	$f("player", {src:imgPathPrefix+'swf/flowplayer-3.0.3.swf',wmode:"opaque"},
					{				
						clip: {autoplay:true,baseUrl: ''}	
					// playlist plugin 
					}).play(clip)
					});
					//$f("player");
					
			    }
			    */
			    		    
			 
				var tmp_var=el.attr("id");			     
			    tmp_var = tmp_var.split('_');
			    if(tmp_var.length>2)
			    {
					var index = tmp_var[1];
					var index1 = tmp_var[2];
					
					if(index=='f')
					{
						document.getElementById('video_name').innerHTML=fetVideoArray[index1][2];
		    			document.getElementById('video_shortdsc').innerHTML=fetVideoArray[index1][3];
		    			document.getElementById('video_longdsc').innerHTML=fetVideoArray[index1][6];
		   			    document.getElementById('video_length').innerHTML=fetVideoArray[index1][4]+' | '+fetVideoArray[index1][5];
		   			  //   document.getElementById('videoRaR').src='ScheelsVideoReviewAndRatingsView?productId='+fetVideoArray[index1][1]+'&partNumber='+fetVideoArray[index1][9]+'&catName='+fetVideoArray[index1][12];
					}
					else if(index=='ex')
					{
						document.getElementById('video_name').innerHTML=prodVidArray[2];
		    			document.getElementById('video_shortdsc').innerHTML=prodVidArray[3];
		    			document.getElementById('video_longdsc').innerHTML=prodVidArray[6];
		   			    document.getElementById('video_length').innerHTML=prodVidArray[4]+' | '+prodVidArray[5];
		   			  //  document.getElementById('videoRaR').src='ScheelsVideoReviewAndRatingsView?productId='+prodArray[index][index1][1]+'&partNumber='+prodArray[index][index1][9]+'&catName='+prodArray[index][index1][12];
	   			    }
					else
					{
						document.getElementById('video_name').innerHTML=prodArray[index][index1][2];
		    			document.getElementById('video_shortdsc').innerHTML=prodArray[index][index1][3];
		    			document.getElementById('video_longdsc').innerHTML=prodArray[index][index1][6];
		   			    document.getElementById('video_length').innerHTML=prodArray[index][index1][4]+' | '+prodArray[index][index1][5];
		   			  //  document.getElementById('videoRaR').src='ScheelsVideoReviewAndRatingsView?productId='+prodArray[index][index1][1]+'&partNumber='+prodArray[index][index1][9]+'&catName='+prodArray[index][index1][12];
	   			    }
   			    }						
			}			
			
			return false;
		}	
		
		
		function clearCSS() {
			els.removeClass(opts.playingClass);
			els.removeClass(opts.pausedClass);
			els.removeClass(opts.progressClass);			
		}
		
		function getEl(clip) {		
			return (manual) ? els.filter("[@href=" + clip.url + "]") : els.eq(clip.index);	
		}
		
		// onBegin
		self.onBegin(function(clip) {
			clearCSS();		
			getEl(clip).addClass(opts.playingClass);
		});	
		
		// onPause	
		self.onPause(function(clip) {
			getEl(clip).removeClass(opts.playingClass).addClass(opts.pausedClass);		
		});	
		
		// onResume
		self.onResume(function(clip) {
			getEl(clip).removeClass(opts.pausedClass).addClass(opts.playingClass);		
		});		
		
		// what happens when clip ends ?
		if (!opts.loop && !manual) {
			
			// stop the playback exept on the last clip, which is stopped by default
			self.onBeforeFinish(function(clip) {
				if (clip.index < els.length -1) {
					return false;
				}
			}); 
		}
		
		// on manual setups perform looping here
		if (manual && opts.loop) {
			self.onBeforeFinish(function(clip) {
				var el = getEl(clip);
				if (el.next().length) {
					el.next().click();	 		
				} else {
					els.eq(0).click();	
				} 
				return false;				
			}); 
		}
		
		// onUnload
		self.onUnload(function() {
			clearCSS();		
		});
		
		
		return self;
		
	});
	
})(jQuery);		


function removeRelativePrefix(vid)
{

	if(vid.indexOf(imgPathPrefix)!=-1)
	{
		//vid = replaceSubstring(vid,imgPathPrefix,'');
		vid = stripContentBeforeAbsoluteURL(vid);
	}
	return vid;
}

 // Ends the "replaceSubstring" function

function remove(s, t) {
  /*
  **  Remove all occurrences of a token in a string
  **    s  string to be processed
  **    t  token to be removed
  **  returns new string
  */
  i = s.indexOf(t);
  r = "";
  if (i == -1) return s;
  r += s.substring(0,i) + remove(s.substring(i + t.length), t);
  return r;
  }
function stripContentBeforeAbsoluteURL(inputString)
{
	var index = inputString.indexOf('http://');
	if(index==-1)
	{
		return inputString;
	}	 
	var str = '';
	for(var i = index;i<inputString.length;i++)
	{
		str = str+inputString.charAt(i);
	}	 
	return str;
}
function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
  
   return temp; // Send the updated string back to the user
}

 