
jQuery.noConflict();




//
// Flash Player Version Detection - Rev 1.6
// Detect Client Browser type
// Copyright(c) 2005-2006 Adobe Macromedia Software, LLC. All rights reserved.
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

function ControlVersion()
{
	var version;
	var axo;
	var e;

	// NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry

	try {
		// version will be set for 7.X or greater players
		axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		version = axo.GetVariable("$version");
	} catch (e) {
	}

	if (!version)
	{
		try {
			// version will be set for 6.X players only
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
			
			// installed player is some revision of 6.0
			// GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,
			// so we have to be careful. 
			
			// default to the first public version
			version = "WIN 6,0,21,0";

			// throws if AllowScripAccess does not exist (introduced in 6.0r47)		
			axo.AllowScriptAccess = "always";

			// safe to call for 6.0r47 or greater
			version = axo.GetVariable("$version");

		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 4.X or 5.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = axo.GetVariable("$version");
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 3.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = "WIN 3,0,18,0";
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 2.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			version = "WIN 2,0,0,11";
		} catch (e) {
			version = -1;
		}
	}
	
	return version;
}

// JavaScript helper required to detect Flash Player PlugIn version information
function GetSwfVer(){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	var flashVer = -1;
	
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			var descArray = flashDescription.split(" ");
			var tempArrayMajor = descArray[2].split(".");			
			var versionMajor = tempArrayMajor[0];
			var versionMinor = tempArrayMajor[1];
			var versionRevision = descArray[3];
			if (versionRevision == "") {
				versionRevision = descArray[4];
			}
			if (versionRevision[0] == "d") {
				versionRevision = versionRevision.substring(1);
			} else if (versionRevision[0] == "r") {
				versionRevision = versionRevision.substring(1);
				if (versionRevision.indexOf("d") > 0) {
					versionRevision = versionRevision.substring(0, versionRevision.indexOf("d"));
				}
			}
			var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	else if ( isIE && isWin && !isOpera ) {
		flashVer = ControlVersion();
	}	
	return flashVer;
}

// When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
{
	versionStr = GetSwfVer();
	if (versionStr == -1 ) {
		return false;
	} else if (versionStr != 0) {
		if(isIE && isWin && !isOpera) {
			// Given "WIN 2,0,0,11"
			tempArray         = versionStr.split(" "); 	// ["WIN", "2,0,0,11"]
			tempString        = tempArray[1];			// "2,0,0,11"
			versionArray      = tempString.split(",");	// ['2', '0', '0', '11']
		} else {
			versionArray      = versionStr.split(".");
		}
		var versionMajor      = versionArray[0];
		var versionMinor      = versionArray[1];
		var versionRevision   = versionArray[2];

        	// is the major.revision >= requested major.revision AND the minor version >= requested minor
		if (versionMajor > parseFloat(reqMajorVer)) {
			return true;
		} else if (versionMajor == parseFloat(reqMajorVer)) {
			if (versionMinor > parseFloat(reqMinorVer))
				return true;
			else if (versionMinor == parseFloat(reqMinorVer)) {
				if (versionRevision >= parseFloat(reqRevision))
					return true;
			}
		}
		return false;
	}
}

function AC_AddExtension(src, ext)
{
  if (src.indexOf('?') != -1)
    return src.replace(/\?/, ext+'?'); 
  else
    return src + ext;
}

function AC_Generateobj(objAttrs, params, embedAttrs) 
{ 
    var str = '';
    if (isIE && isWin && !isOpera)
    {
  		str += '<object ';
  		for (var i in objAttrs)
  			str += i + '="' + objAttrs[i] + '" ';
  		str += '>';
  		for (var i in params)
  			str += '<param name="' + i + '" value="' + params[i] + '" /> ';
  		str += '</object>';
    } else {
  		str += '<embed ';
  		for (var i in embedAttrs)
  			str += i + '="' + embedAttrs[i] + '" ';
  		str += '> </embed>';
    }

    document.write(str);
}

function AC_FL_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     , "application/x-shockwave-flash"
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_GetArgs(args, ext, srcParamName, classid, mimeType){
  var ret = new Object();
  ret.embedAttrs = new Object();
  ret.params = new Object();
  ret.objAttrs = new Object();
  for (var i=0; i < args.length; i=i+2){
    var currArg = args[i].toLowerCase();    

    switch (currArg){	
      case "classid":
        break;
      case "pluginspage":
        ret.embedAttrs[args[i]] = args[i+1];
        break;
      case "src":
      case "movie":	
        args[i+1] = AC_AddExtension(args[i+1], ext);
        ret.embedAttrs["src"] = args[i+1];
        ret.params[srcParamName] = args[i+1];
        break;
      case "onafterupdate":
      case "onbeforeupdate":
      case "onblur":
      case "oncellchange":
      case "onclick":
      case "ondblClick":
      case "ondrag":
      case "ondragend":
      case "ondragenter":
      case "ondragleave":
      case "ondragover":
      case "ondrop":
      case "onfinish":
      case "onfocus":
      case "onhelp":
      case "onmousedown":
      case "onmouseup":
      case "onmouseover":
      case "onmousemove":
      case "onmouseout":
      case "onkeypress":
      case "onkeydown":
      case "onkeyup":
      case "onload":
      case "onlosecapture":
      case "onpropertychange":
      case "onreadystatechange":
      case "onrowsdelete":
      case "onrowenter":
      case "onrowexit":
      case "onrowsinserted":
      case "onstart":
      case "onscroll":
      case "onbeforeeditfocus":
      case "onactivate":
      case "onbeforedeactivate":
      case "ondeactivate":
      case "type":
      case "codebase":
        ret.objAttrs[args[i]] = args[i+1];
        break;
      case "id":
      case "width":
      case "height":
      case "align":
      case "vspace": 
      case "hspace":
      case "class":
      case "title":
      case "accesskey":
      case "name":
      case "tabindex":
        ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
        break;
      default:
        ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
    }
  }
  ret.objAttrs["classid"] = classid;
  if (mimeType) ret.embedAttrs["type"] = mimeType;
  return ret;
}




function normalSync()
{
    jQuery.get('index.php', {
        option: "com_jvideo",
        view: "jvideo",
        task: "normalSync",
        format: "raw"
    });
}

function consoleSync()
{
    jQuery.get('index.php', {
        option: "com_jvideo",
        view: "jvideo",
        task: "consoleSync",
        format: "raw"
    });
}

function rateVideo(videoId, userId, rating)
{
    jQuery('#ajaxRating').load('index.php', {
       option: 'com_jvideo',
       task: 'rate_video',
       format: 'raw',
       id: videoId,
       user_id: userId,
       rating: rating
    });
}

function approveVideo(videoId)
{
    jQuery.get('index.php', {
        option: 'com_jvideo',
        task: 'approve',
        format: 'raw',
        id: videoId
    });
}

function deleteVideo(videoId)
{
    jQuery.get('index.php', {
        option: 'com_jvideo',
        task: 'delete_video',
        format: 'raw',
        id: videoId
    });

    locate.reload();
}

function featureVideo(videoId, addFeatured)
{
    jQuery('#markFeatured').load('index.php', {
        option: 'com_jvideo',
        task: addFeatured == 'true' ? 'addFeatured' : 'removeFeatured',
        format: 'raw',
        videoId: videoId
    });
}

function publishVideo(videoId, publishVideo)
{
    jQuery('#publishVideo').load('index.php', {
        option: 'com_jvideo',
        task: publishVideo == 'true' ? 'publishVideo' : 'unpublishVideo',
        format: 'raw',
        id: videoId
    });
}

function updateVideoCategories(videoId, categoryId, isAdding)
{
    jQuery('#categoryList').load('index.php', {
        option: 'com_jvideo',
        task: isAdding == 'true' ? 'addVideoToCategory' : 'removeVideoFromCategory',
        format: 'raw',
        videoId: parseInt(videoId),
        categoryId: parseInt(categoryId)
    });
}

function getVideosByParams(videoParams, placeholderId)
{
    displayLoadingImage(placeholderId);

    var paramUrl = '?option=com_jvideo&view=videos&format=ajax';
    
    for (var videoKey in videoParams) {
        paramUrl += '&' + videoKey + '=' + videoParams[videoKey];

        if (videoKey == 'player_placeholder_id')
            break;
    }

    jQuery('#' + placeholderId).load('index.php' + paramUrl);
}

function getVideoPlayer(id, playerHeight, playerWidth, placeholderId)
{
    displayLoadingImage(placeholderId);

    jQuery('#' + placeholderId).load('index.php', {
        option: 'com_jvideo',
        view: 'watch',
        format: 'ajax',
        id: id,
        playerHeight: playerHeight,
        playerWidth: playerWidth
    });
}


//
// @deprecated; use the non-prototype functions defined in jquery.js
//
function JVideoAJAX() {}
JVideoAJAX.prototype.normalSync = function() { normalSync(); }
JVideoAJAX.prototype.dashboardSync = function() { consoleSync(); }
JVideoAJAX.prototype.rateVideo = function(videoID, userID, rating) { rateVideo(videoID, userID, rating); }
JVideoAJAX.prototype.approveVideo = function(videoID) {	approveVideo(videoID); }
JVideoAJAX.prototype.deleteVideo = function(videoID, resultPlaceholder) { deleteVideo(videoID, resultPlaceholder); }
JVideoAJAX.prototype.featureVideo = function(videoId, addFeatured) { featureVideo(videoId, addFeatured); }
JVideoAJAX.prototype.publishVideo = function(videoID, publishVideo) { publishVideo(videoID, publishVideo); }
JVideoAJAX.prototype.updateVideoCategories = function(videoID, categoryID, isAdding) {
    updateVideoCategories(videoID, categoryID, isAdding);
}
JVideoAJAX.prototype.getVideosByParams = function(params, placeholderId) { getVideosByParams(params, placeholderId); }
JVideoAJAX.prototype.getVideoPlayer = function(id, playerHeight, playerWidth, placeholderId) {
    getVideoPlayer(id, playerHeight, playerWidth, placeholderId);
}

function highlightStars(starMap, fullStar, halfStar, emptyStar) {
	var star1 = document.getElementById('videoStar1');
	var star2 = document.getElementById('videoStar2');
	var star3 = document.getElementById('videoStar3');
	var star4 = document.getElementById('videoStar4');
	var star5 = document.getElementById('videoStar5');

	// starmap: 0 = empty, 1 = half, 2 = full
	star1.className = starMap.charAt(0) == "2" ? fullStar : starMap.charAt(0) == "1" ? halfStar : emptyStar;
	star2.className = starMap.charAt(1) == "2" ? fullStar : starMap.charAt(1) == "1" ? halfStar : emptyStar;
	star3.className = starMap.charAt(2) == "2" ? fullStar : starMap.charAt(2) == "1" ? halfStar : emptyStar;
	star4.className = starMap.charAt(3) == "2" ? fullStar : starMap.charAt(3) == "1" ? halfStar : emptyStar;
	star5.className = starMap.charAt(4) == "2" ? fullStar : starMap.charAt(4) == "1" ? halfStar : emptyStar;
	
}

function displayLoadingImage(placeholderId)
{
	var contents = "";
	var contentHeight = document.getElementById(placeholderId).clientHeight;
	
	if (contentHeight < 16) {
		contentHeight = 16;
	}
	
	contents += '<div style="width: 100%; height: '+contentHeight+'px; vertical-align: middle; text-align: center; position: relative;">';
	contents += '	<img src="' + jsBaseUrl + '/components/com_jvideo/assets/images/wait.gif" border="0" height="11" width="43" alt="Loading..." style="position: absolute; top: '+(contentHeight / 2)+'px;" />';
	contents += '</div>';
	
	document.getElementById(placeholderId).innerHTML = contents; 
}




function embedSetAutoPlay(objId, val)
{
    obj = document.getElementById(objId);

    if (false == val) {
        obj.value = obj.value.replace(/AutoPlay=1/g, 'AutoPlay=0');
    } else {
        obj.value = obj.value.replace(/AutoPlay=0/g, 'AutoPlay=1');
    }

    obj.select();
}

function embedSetFullScreen(objId, val)
{
    obj = document.getElementById(objId);

    if (false == val) {
        obj.value = obj.value.replace(/allowFullScreen\"\ value=\"1/g, 'allowFullScreen" value="0');
        obj.value = obj.value.replace(/allowFullScreen=1/g, 'allowFullScreen=0');
    } else {
        obj.value = obj.value.replace(/allowFullScreen\"\ value=\"0/g, 'allowFullScreen" value="1');
        obj.value = obj.value.replace(/allowFullScreen=0/g, 'allowFullScreen=1');
    }

    obj.select();
}

function embedSetHeightWidth(objId, height, width)
{
    obj = document.getElementById(objId);
    obj.value = obj.value.replace(/height=\"[0-9]*\"/g, 'height="' + height + '"');
    obj.value = obj.value.replace(/width=\"[0-9]*\"/g, 'width="' + width + '"');
    obj.select();
}

function update_player_height(method)
{
	var theForm = document.getElementById('adminForm');
	
	var radioObj = theForm.aspect_constraint;
	var radioLength = radioObj.length;
	
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			var aspect_constraint = radioObj[i].value;
		}
	}
	
	/* Determine our multiplier based on our aspect ratio */
		if (method == 'width') {
			var multiplier = 0.75;
			
			if (aspect_constraint == '16x9') {
				multiplier = 0.5625;
			}
			
			var width = theForm.video_player_width.value;
			width = width.replace(/[^0-9]/g, '');
			theForm.video_player_width.value = width;
			
			var height = Math.round(width *  multiplier);
			
			theForm.video_player_height.value = height;
		} 
		if (method == 'height') {
			var multiplier = 1.3;
			
			if (aspect_constraint == '16x9') {
				multiplier = 1.8;
			}
			
			
			var height = theForm.video_player_height.value;
			height = height.replace(/[^0-9]/g, '');
			theForm.video_player_height.value = height;
			
			var width = Math.round(height *  multiplier);
			
			theForm.video_player_width.value = width;
		}
}
function edit_category()
{
	var theForm = document.getElementById('adminForm');
	theForm.task.value = 'edit_category';
}

function delete_category()
{
	if (confirm("Are you sure you want to delete this category?")) {
		/* Do something */
		var theForm = document.getElementById('adminForm');
		theForm.task.value = 'do_delete_category';
		theForm.submit();
	}
}

function jvideo_verify_admin_client_form()
{
	var theForm = document.getElementById('adminForm');
	
	var isValid = true;
	var message = "";
	
	if ( (theForm.pass.value != theForm.passConfirm.value) || theForm.pass.value == "")
	{
		message += 'Your passwords do not match.\n';
		isValid = false;
	}
	
	if (theForm.user_name.value == "")
	{
		message += 'You must enter a user name.\n';
		isValid = false;
	}
	
	if (theForm.infindomain.value == "")
	{
		message += 'You must enter a domain.\n';
		isValid = false;
	}
	
	if (message != "")
	{
		alert(message);
	}
	
	return isValid;
}

function featured_reorder_item(id, rank, updown)
{
	var theForm = document.getElementById('adminForm');
	
	theForm.featured_id.value = id;
	theForm.featured_order.value = rank;
	theForm.order_method.value = updown;
	theForm.task.value = 'do_reorder_featured_video';
	theForm.submit();
}

function featured_remove_item(id)
{
	var theForm = document.getElementById('adminForm');
	
	if (confirm('Are you sure you want to remove this featured video?')) {
		theForm.featured_id.value = id;
		theForm.task.value = 'do_remove_featured_video';
		theForm.submit();		
	}
}

function set_cat_crumb()
{
    var x=document.getElementById("parent_categories")
    alert(x.options[x.selectedIndex].text);
}

function attach_video_to_category_from_add()
{
	var theForm = document.getElementById('jvideoForm');
	theForm.task.value = 'attach_video_to_category_from_add';
	theForm.submit();
}

function detach_video_to_category_from_add()
{
	var theForm = document.getElementById('jvideoForm');
	theForm.task.value = 'detach_video_from_category_from_add';
	theForm.submit();
}
function attach_video_to_category_from_edit()
{
	var theForm = document.getElementById('jvideoForm');
	theForm.task.value = 'attach_video_to_category_from_edit';
	theForm.submit();
}

function detach_video_to_category_from_edit()
{
	var theForm = document.getElementById('jvideoForm');
	theForm.task.value = 'detach_video_from_category_from_edit';
	theForm.submit();
}
