function SearchFocus()
{
	var termElem = document.getElementById("term");
	if (termElem.value == "Search Media") termElem.value = "";
}

function SearchBlur()
{
	var termElem = document.getElementById("term");
	if (termElem.value.length == 0) termElem.value = "Search Media";
}

function addCartItem(mID) {
	document.getElementById("cart_popup").style.display = "block";
	//alert("testing");

	var mDIV = document.getElementById("messageDIV");
	var cLink = document.getElementById("cartLink");
	var cartImageDiv = document.getElementById("cartDiv-"+mID);
	var msg = "";	

	var cookies = document.cookie;
	//alert("Cookies is "+cookies);
	var cartPos = cookies.indexOf("cart=");
	//alert("CartPos is "+cartPos);
	if(cartPos != -1) {

		//alert("cart exists");
		var start = Number(cartPos + 5);
		var end = cookies.indexOf(";",start);
		if(end == -1) end = cookies.length;
		var value = cookies.substring(start,end);
		//alert("Value is "+value);
		//alert("Value length is "+value.length);

		var itemArray = value.split(',');
		var cartTotal = itemArray.length;
		var newCart = "";
		var counter = 1;
		for(var i=0; i<itemArray.length; i++) {
			//alert("item "+itemArray[i]);
			if(itemArray[i] != mID) {
				if(itemArray[i].length > 0) {
					if(newCart.length == 0) {
						newCart += itemArray[i];
						//alert("Adding "+itemArray[i]);
					} else {
						newCart += ','+itemArray[i];
						//alert("Adding "+itemArray[i]);
					}

					counter++;
				}	

			}

		}

		//alert("Counter is "+counter+" AND itemArray.length is "+itemArray.length);

		if(counter > itemArray.length || itemArray.length == 0) {
			msg = "Item added to your cart";			
		} else {
			msg = "Item already in your cart";
		}

		if(newCart.length == 0) {
			document.cookie = "cart="+mID;
			cartTotal = 1;
		} else {
			document.cookie = "cart="+newCart+","+mID;
			cartTotal = cartTotal + 1;
		}
		cartImageDiv.innerHTML = "<img src='images/btn-in-cart.png' border='0' alt='Item In Your Cart'><br><a href='cart.php'>Checkout Now!</a>";
		cLink.innerHTML = "My Cart ("+cartTotal+")";
		
	} else {
		// Set the first cart item
		document.cookie = "cart="+mID;
		cLink.innerHTML = "My Cart (1)";
		msg = "Item added to your cart";
		
	}

	if(msg.length > 0) {

		//mDIV.innerHTML = msg;

	}

	

}

function updateNotificationDiv(notifyCode,notifyMsg) {
	theDiv = document.getElementById("notificationArea");
	if (theDiv) {
	  theDiv.innerHTML = "";
	  theDiv.className = "";
	  if (notifyCode.substr(0,7) == "confirm") {
	  	theDiv.className = "success-msg";
	  } else if (notifyCode.substr(0,5) == "error") {
	  	theDiv.className = "error-msg";
	  }
		switch (notifyCode) {
			// Package CRUD /////////////////////////////
			case "confirmSavePackage1":
			  theDiv.innerHTML = "Program has been created. Proceed with adding content.";
			  break;
			case "confirmSavePackage2":
			  theDiv.innerHTML = "Course has been created. Proceed with adding content.";
			  break;
			case "errorSavePackage1":
			  theDiv.innerHTML = "Error creating Program.";
			  break;
			case "errorSavePackage2":
			  theDiv.innerHTML = "Error creating Course.";
			  break;
			case "confirmUpdatePackage":
			  theDiv.innerHTML = "Info has been updated.";
			  break;
			case "errorUpdatePackage":
			  theDiv.innerHTML = "Error updating info.";
			  break;
			case "confirmDeletePackage":
			  theDiv.innerHTML = "Item has been deleted.";
			  break;
			case "confirmDeletePackage":
			  theDiv.innerHTML = "Error deleting item.";
			  break;
			 // Package Content CRUD /////////////////////////////
			case "confirmPackage1AddPackage":
			case "confirmPackage1AddContent":
			  theDiv.innerHTML = "Item has been added to Program.";
			  break;
			case "confirmPackage2AddPackage":
			case "confirmPackage2AddContent":
			  theDiv.innerHTML = "Item has been added to Course.";
			  break;
			case "confirmPackage1RemovePackage":
			case "confirmPackage1RemoveContent":
			  theDiv.innerHTML = "Item has been removed from Program.";
			  break;
			case "confirmPackage2RemovePackage":
			case "confirmPackage2RemoveContent":
			  theDiv.innerHTML = "Item has been removed from Course.";
			  break;
			case "confirmPrioritySaved":
			  theDiv.innerHTML = "Priority order has been updated.";
			  break;
			case "errorPrioritySaved":
			  theDiv.innerHTML = "Error saving priority order.";
			  break;
			// Package/Media Assignment CRUD /////////////////////////////
			case "confirmPackageAssign":
			case "confirmMediaAssign":
			  theDiv.innerHTML = "Assignment has been successfully saved.";
			  break;
			case "confirmPackageRemoveAssign":
			case "confirmMediaRemoveAssign":
			  theDiv.innerHTML = "Assignment has been successfully removed.";
			  break;
			// User CRUD /////////////////////////////
			case "confirmUserUpdated":
				theDiv.innerHTML = "User successfully updated.";
				break;
			case "errorUserSaved":
				theDiv.innerHTML = "Error saving the User.";
				break;
			case "errorUserSavedDuplicate":
				theDiv.innerHTML = "Error saving the user. Duplicate email.";
				break;
			case "confirmUserRemoved":
				theDiv.innerHTML = "User has been removed from Group.";
				break;
			case "errorUserRemoved":
				theDiv.innerHTML = "Error removing User from Group.";
				break;
			// Group CRUD /////////////////////////////
			case "confirmGroupSaved":
				theDiv.innerHTML = "Department/Channel successfully saved.";
				break;
			case "errorGroupSaved":
				theDiv.innerHTML = "Error saving the Department/Channel.";
				break;
			// Content CRUD /////////////////////////////
			case "confirmContentRemoveAssociation":
				theDiv.innerHTML = "Associated content successfully removed.";
				break;
			case "confirmContentAddAssociation":
				theDiv.innerHTML = "Associated content successfully added.";
				break;
			// Other ///////////////////////////////////////////////
			case "":
			  theDiv.innerHTML = "";
			  break;
			default:
			  theDiv.innerHTML = notifyCode;
			  theDiv.className = "error-msg";
			  break;
		}
	}
}

function addFavorite(fID,uID) {
	var ajaxRequestObj = ajaxRequest();
	var fDIV = document.getElementById("favoriteLinkBox-"+fID);	

	// Ajax Change Handler
	ajaxRequestObj.onreadystatechange = function(){
		if(ajaxRequestObj.readyState == 4) {
			if(ajaxRequestObj.status == 200) {
				//alert("response text is "+ajaxRequestObj.responseText);
				if(ajaxRequestObj.responseText == "true") {
					fDIV.innerHTML = "<img height='16' border='0' width='17' style='vertical-align: middle;' alt='In Favorites' src='images/favorite-on.gif'/>";
				} else {
					fDIV.innerHTML = "<em>Error</em>";
				}
			}			
		}
	}
	// Ajax Request
	ajaxRequestObj.open('GET', 'ajax/favorites.php?action=addfavorite&mid='+fID+'&uid='+uID+'', true);
	ajaxRequestObj.send(null);
}

function newPopup(url) {
	popupWindow = window.open(
		url,'popUpWindow','height=900,width=1100,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
}

var globalMediaIntervalID = 0;

function openMedia(mid,w,h,isPreview) {
	//alert("Opening "+mid+" w: "+w+" h:"+h+" isPreview:"+isPreview);
	//if(w < 600) w = 600;
	if(h > 90) h+=100;
	$.fancybox({
				'href': 'includes/mediaPlayer.php?mid='+mid+'&isPreview='+isPreview,
				//'modal' : true,
				'data' : 'remid',
				'autoDimensions': false,
				'width': (w+30),
				'height': (h),
				'scrolling': 'no',
				'showCloseButton' : true,
				'transitionIn': 'none',
				'transitionOut': 'none',
				'onComplete': function() {
					//$.fancybox.resize();
					//setTimeout ( '$.fancybox.resize()', 1000 );
					return true;
				},
				'onClosed': function() {
					clearInterval(globalMediaIntervalID);
					return true;
				}
							
			});
	//setTimeout ( '$.fancybox.resize()', 3000 );
}

function contentHistory(cid) {
	$.fancybox({
				'href': 'includes/contentHistory.php?cid='+cid,
				//'modal' : true,
				'data' : 'remid',
				'autoDimensions': false,
				'width': 750,
				'height': 550,
				'scrolling': 'yes',
				'showCloseButton' : true,
				'transitionIn': 'none',
				'transitionOut': 'none',
				'onComplete': function() {
					//$.fancybox.resize();
					//setTimeout ( '$.fancybox.resize()', 300 );
				}			
			});
	//setTimeout ( '$.fancybox.resize()', 3000 );
}

function packageHistory(pid) {
	$.fancybox({
				'href': 'includes/packageHistory.php?pid='+pid,
				//'modal' : true,
				'data' : 'remid',
				'autoDimensions': false,
				'width': 750,
				'height': 550,
				'scrolling': 'yes',
				'showCloseButton' : true,
				'transitionIn': 'none',
				'transitionOut': 'none',
				'onComplete': function() {
					// Cleanup
				}			
			});
}

function quizHistory(qid) {
	$.fancybox({
				'href': 'includes/quizHistory.php?qid='+qid,
				//'modal' : true,
				'data' : 'remid',
				'autoDimensions': false,
				'width': 750,
				'height': 550,
				'scrolling': 'yes',
				'showCloseButton' : true,
				'transitionIn': 'none',
				'transitionOut': 'none',
				'onComplete': function() {
					// Cleanup
				}			
			});
}

function openExternal(url,w,h) {	
	var win = window.open(url,'win','width='+w+',height='+h+'location=false,status=false,toolbar=false');
}

// allows older browsers (< Javascript 1.6) to support Array.filter()
if (!Array.prototype.filter) {  
  Array.prototype.filter = function(fun /*, thisp */) {  
    "use strict";  
    if (this === void 0 || this === null)  
      throw new TypeError();  
    var t = Object(this);  
    var len = t.length >>> 0;  
    if (typeof fun !== "function")  
      throw new TypeError();  
    var res = [];  
    var thisp = arguments[1];  
    for (var i = 0; i < len; i++) {  
      if (i in t) {  
        var val = t[i]; // in case fun mutates this  
        if (fun.call(thisp, val, i, t))  
          res.push(val);  
      }  
    }  
    return res;  
  };  
}  
function openModal(name){
	$('#' + name).modal();
}

