//GLOBAL VARIABLE DECALRATIONS

var theQstring = "";
var removedChks = "";
var addChks = "";
var runonce = "";







// PAGING & SORTING

//send page number param into the querystring
// ========================================================================================================
	function page(p){
		var strHref = window.location.href;
		var subStrHref = "";
		if(strHref.indexOf("?") > 0){
			if(strHref.indexOf("page") > 0){
				subStrHref = "page=" + queryStringValue('page');
				strHref = strHref.replace(subStrHref,"page=" + p);
				location.href = strHref;
			}else{
				location.href = location.href + "&page=" + p;
			}
		}else{
			location.href = location.href + "?page=" + p;
		}
	}




// send sort by param into the qstring
// ==============================================================================================================
	function sortBy(val){
		var strHref = window.location.href;
		var sStr = "";
		var page = "";
		if(val != ""){
			if(strHref.indexOf("sort=") > -1){	
				sStr = "sort=" + queryStringValue('sort');				
				strHref= strHref.replace(sStr,"sort=" + val);
				strHref = resetPageNum(strHref);
				location.href=strHref;
			}else{
				strHref = resetPageNum(strHref);
				if(strHref.indexOf("?") > -1){
					strHref = strHref + "&sort=" + val;
					location.href=strHref;
				}else{
					location.href = location.href + "?sort=" + val;
				}
			}	
		}
	}




//send number of video records per page into querystring
// ========================================================================================================
	function recs(n){
		var strHref = window.location.href;
		var subStrHref = "";
		var page = "";
		if(strHref.indexOf("?") > 0){			
			if(strHref.indexOf("recs") > 0){
				subStrHref = "recs=" + queryStringValue('recs');
				strHref = strHref.replace(subStrHref,"recs=" + n);
				strHref = resetPageNum(strHref);
				location.href = strHref;
			}else{
				strHref = location.href + "&recs=" + n;
				strHref = resetPageNum(strHref);
				location.href = strHref;
			}
		}else{
			location.href = location.href + "?recs=" + n;
		}
	}



//function for search 'show'
// ========================================================================================================
	function searchShow(val){
		var strHref = "";
		if(location.href.indexOf('show') > -1){
			var showSubStr = location.href.substring(location.href.indexOf("&show="));
			if(val == ""){		
				strHref = location.href.replace(showSubStr,"");
				strHref = resetPageNum(strHref);	
				location.href=strHref;
			}else{
				strHref = location.href.replace(showSubStr,"&show="+val);
				strHref = resetPageNum(strHref);
				location.href=strHref;
			}
		}else{
			if(val == ""){				
				location.href=resetPageNum(location.href);
			}else{
				location.href=resetPageNum(location.href) + "&show=" + val;
			}
		}
	}
	

	
	//function for innovator 'show'
// ========================================================================================================
	function innovatorShow(val){
		if(location.href.indexOf('show') > -1){
			var showSubStr = location.href.substring(location.href.indexOf("&show="));
			if(val == ""){			
				location.href=location.href.replace(showSubStr,"");
			}else{   
			    if(location.href.indexOf('?show') > -1){
			        if(val == ""){				
				        location.href=location.href;
			       }else{
				       location.href=location.href.replace(showSubStr,"?show="+val);
			        }
		    	}else{
			        if(val == ""){				
				     location.href=location.href;
			        }else{
				        location.href=location.href.replace(showSubStr,"&show="+val);
			        }			
			    }
			}
		}else{
		    if(location.href.indexOf('?') > -1){
			    if(val == ""){				
				    location.href=location.href;
			    }else{
				    location.href=location.href + "&show=" + val;
			    }
			}else{
			    if(val == ""){				
				    location.href=location.href;
			    }else{
				    location.href=location.href + "?show=" + val;
			    }			
			}
		}
	}
	
	
	
	
	
		
	//function for playlist 'show'
// ========================================================================================================
	function playlistShow(val){

		if(location.href.indexOf('show') > -1){
			var showSubStr = location.href.substring(location.href.indexOf("&show="));
			if(val == ""){			
				location.href=location.href.replace(showSubStr,"");
			}else{   
			    if(location.href.indexOf('?show') > -1){
			        if(val == ""){				
				        location.href=location.href;
			       }else{
				       location.href=location.href.replace(showSubStr,"?show="+val);
			        }
		    	}else{
			        if(val == ""){				
				     location.href=location.href;
			        }else{
				        location.href=location.href.replace(showSubStr,"&show="+val);
			        }			
			    }
			}
		}else{
		    if(location.href.indexOf('?') > -1){
			    if(val == ""){				
				    location.href=location.href;
			    }else{
				    location.href=location.href + "&show=" + val;
			    }
			}else{
			    if(val == ""){				
				    location.href=location.href;
			    }else{
				    location.href=location.href + "?show=" + val;
			    }			
			}
		}
	}

	
	

// END PAGING & SORTING










// SITE SEARCH


//function for keyword search box
// ========================================================================================================
	function keywordsearch(){
		var keyword=document.getElementById('txt1').value;
		location.href="/search.aspx?keyword=" + keyword;
	}

// END SITE SEARCH








// VIDEOS: ADVANCED SEARCH FILTER

// add & remove category search criteria for checkboxes
// ========================================================================================================
	//c param is the chkBox -- ctype param is the chkBox type
	function search(c,ctype){
		var ChkBoxID = ctype + "_" + c;
		var theChkBox = document.getElementById(ChkBoxID);

		//removing search criteria, the checkbox is being turned off
		//----------------------------------------------------------
		if(theChkBox.checked == false){
			if(theQstring.indexOf(ctype + "=") > -1){ 
				//find first var
				var varLength = c.length;
				var varsIndex = theQstring.indexOf(ctype + "=") + ctype.length+1;
				var firstVar = theQstring.substring(varsIndex,varsIndex + varLength);
				//if the first var is being removed
				if(c == firstVar){
					if(theQstring.indexOf("&" + ctype) > -1){
						//then its the last one in the grouping
						if(theQstring.indexOf("&" + ctype) < theQstring.lastIndexOf(",")){
							theQstring= theQstring.replace(c + ",", "");
						}else{
							theQstring= theQstring.replace("&" + ctype + "=" + c, "");
						}
					}
					if(theQstring.indexOf("?" + ctype) > -1){
						//then its the last one
						if(theQstring.indexOf("?" + ctype) < theQstring.lastIndexOf(",")){
							theQstring= theQstring.replace(c + ",", "");
						}else{
							//not the last one so determine if there is an innovator in the str
							if(theQstring.indexOf("&") > -1){
								theQstring= theQstring.replace(ctype + "=" + c, "");
							}else{	
								theQstring= theQstring.replace("?" + ctype + "=" + c, "");
							}
						}
					}
				}else{
					//not the first var so remove the leading comma and the value
					theQstring= theQstring.replace("," + c, "");
				}
			}else{
				//the var is not there, so add it
				theQstring= theQstring.replace("?" + ctype + "=" + c, "");
			}	
			if(theQstring.indexOf(ctype) < 0) theQstring= theQstring.replace("?" + ctype + "=" + c, "");
			removedChks = "yes";
			loadContent();

		//adding search criteria, the checkbox is being turned on
		//--------------------------------------------------------
		}else{
			//the qstring is already started
			if(theQstring.indexOf("?") > -1){ 
				//there is already a var in the qstring
				if(theQstring.indexOf(ctype + "=") > -1){					
					var cStr="";
					var cStr2="";	
					cStr = ctype + "=" + theQstringValue(ctype);
					cStr2 = cStr + "," + c;
					
//if(ctype=="category"){
//uncheckAll(document.tag);
//uncheckAll(document.innovator);
//}

					theQstring= theQstring.replace(cStr,cStr2);

					loadContent();
				//add var to qstring
				}else{


//if(ctype=="category"){
//uncheckAll(document.tag);
//uncheckAll(document.innovator);
//}

					theQstring = theQstring + "&" + ctype + "=" + c;

					loadContent();			
				}
			//the qstring is not started yet
			}else{
				theQstring = "?" + ctype + "=" + c;
				loadContent();
			}
		}
		addChks = "yes";
	}



// read array from qstring and check the box for each piece of search criteria
// ========================================================================================================
	function setChks(ctype){
		var strHref = window.location.href;
		var theHref = strHref.indexOf("?");
		var theStr = "";
		
		if(runonce == ""){
			if(theQstring == "" && theHref > 0){
				theQstring = strHref.substring(theHref);
				runonce = "yes";
			}
		}
		if(theQstring.indexOf(ctype + "=") > -1){
			theStr = theQstringValue(ctype);
		}
	
		var myArray = new Array();
		myArray = theStr.split(',');
		for(var i=0, len=myArray.length; i<len; ++i){
			var ChkBoxID = ctype + "_" + myArray[i];
			var theChkBox = document.getElementById(ChkBoxID);
			if(theChkBox!=null){
				theChkBox.checked = true;
			}
		}	
	}






//check / uncheck Video Filter		
// ========================================================================================================
		function checkAll(field){
			for (i = 0; i < field.length; i++){
			    field[i].checked = true ;
			    if(field[i].id.indexOf('category') > -1){
  			            if(i==0){
  			                if(theQstring.indexOf("?") > -1){
  			                    theQstring = theQstring + "&category=" + field[i].value;
  			                }else{
  			                    theQstring = theQstring + "?category=" + field[i].value;
  			                }
  			            }else{
  			                theQstring = theQstring + "," + field[i].value;
  			            }
  			    }
  			    
  			    if(field[i].id.indexOf('innovator') > -1){
  			            if(i==0){
  			                if(theQstring.indexOf("?") > -1){
  			                    theQstring = theQstring + "&innovator=" + field[i].value;
  			                }else{
  			                    theQstring = theQstring + "?innovator=" + field[i].value;
  			                }
  			            }else{
  			                theQstring = theQstring + "," + field[i].value;
  			            }       
  			    }
			}
		}
		
		
		

		function uncheckAll(field){
			for (i = 0; i < field.length; i++){
				field[i].checked = false;
		    }


			//theQstring= theQstring.replace(theQstringValue('innovator'),"");
			//theQstring= theQstring.replace(theQstringValue('tag'),"");

		    theQstring="";


		    /*
		    var uncheckCatStr = theQstring.substring("&category","");
		    
		    if(theQstring.indexOf('&category') > -1){
		        theQstring= theQstring.replace("&category", "");
		    }
		    
		    if(theQstring.indexOf('?category') > -1){
		        theQstring= theQstring.replace("?category", "");
		    }		    
		    */
		}
		
		



//function to get results
// ========================================================================================================
	function getResults(){	
		theQstring = resetPageNum(theQstring);
		location.href = "/videos.aspx" + theQstring;
	}




//function to get results
// ========================================================================================================
	function getInnovators(){
		location.href = "/innovators.aspx" + theQstring;
	}




//function for closing the livesearch box
// ========================================================================================================
	function ls_close(){
		document.getElementById('livesearch').style.display="none";
		document.getElementById('ls_all').style.display="none";
	}



// END VIDEOS: ADVANCED SEARCH FILTER








// SUB NAV

//function for the about sub nav
// ========================================================================================================
	function aboutSubNav(state){
		if(state == 'on'){
			document.getElementById('aboutSubNav').style.display='block';
		}else{
			document.getElementById('aboutSubNav').style.display='none';
		}
	}

// END SUB NAV








// UTILITY SUB-FUNCTIONS


//ooyala html embed video
//==========================================================================================================
	function yt_embed(videoID){
		document.getElementById('getEmbedForm').value = '<script src="http://player.ooyala.com/player.js?width=1280height=720&embedCode='+videoID+'"></script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="ooyalaPlayer_29wun_g323hz39" width="1280" height="720" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab"><param name="movie" value="http://player.ooyala.com/player.swf?embedCode='+videoID+'&version=2" /><param name="bgcolor" value="#000000" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="true" /><param name="flashvars" value="embedType=noscriptObjectTag&embedCode='+videoID+'" /><embed src="http://player.ooyala.com/player.swf?embedCode='+videoID+'&version=2" bgcolor="#000000" width="1280" height="720" name="ooyalaPlayer_29wun_g323hz39" align="middle" play="true" loop="false" allowscriptaccess="always" allowfullscreen="true" type="application/x-shockwave-flash" flashvars="&embedCode='+videoID+'" pluginspage="http://www.adobe.com/go/getflashplayer"></embed></object>';
	}



//function for page number reset within the qstring - called from other functions
// ========================================================================================================
	function resetPageNum(strHref){
		if(strHref.indexOf("page=") > -1){
			page = "page=" + queryStringValue('page');
			if(page != ''){
				strHref = strHref.replace(page,"page=1");
			}
		}
		return strHref;
	}



// get Querystring - from location.href
// ========================================================================================================
	function queryStringValue(findName){   
    		var name = new String();   
    		var value = new String();   
    		var querystring = document.location.href;   
    		if(querystring.indexOf("?")==-1){   
        			return "null";   
    		}   
    		querystring = querystring.split("?");   
    		querystring = querystring[1].split("&");   
    		for(q=0;q<querystring.length;q++){   
        			var pair = querystring[q].split("=");   
        			name = pair[0];//.toLowerCase();   
        			value = pair[1];//.toLowerCase();   
        			//if(findName.toLowerCase() == name){   
			if(findName == name){   
            			return value;   
        			}   
    		}   
	}   



// get theQstring - from internal theQstring global variable
// ========================================================================================================
	function theQstringValue(findName){   
    		var name = new String();   
    		var value = new String();   
    		var querystring = theQstring;   
    		if(querystring.indexOf("?")==-1){   
        			return "null";   
    		}   
  		querystring = querystring.split("?");   
    		querystring = querystring[1].split("&");   
    		for(q=0;q<querystring.length;q++){   
        			var pair = querystring[q].split("=");   
        			name = pair[0];//.toLowerCase();   
        			value = pair[1];//.toLowerCase();   
        			//if(findName.toLowerCase() == name){   
			if(findName == name){   
            			return value;   
        			}   
    		}   
	}


// END UTILITY SUB-FUNCTIONS






function viewAll(){
	var keyword = document.getElementById("txt1").value;
	lcasekw = keyword.toLowerCase();
	location.href='/search.aspx?keyword='+lcasekw;
}





function collection(id){
	//Flickr Integration
    	$.getJSON("http://api.flickr.com/services/feeds/photoset.gne?set="+id+"&nsid=47071885@N08&lang=en-us&format=json&jsoncallback=?", function(data){
			
		$.each(data.items, function(i,item){
			var myImg = item.media.m;
			var newImg = myImg.replace("_m.jpg", ".jpg");

			if(i==0){
				$('#collection1').attr("href", newImg);
			} else {
				$('#FlickrImages').append('<a href="'+ newImg +'" rel="gallery1" class="collectionsModal"></a>');
			}				
		});
				
		// MUST call ColorBox after JSON has finished
		// ColorBox
		$("#collection1").colorbox({

		});

		$(".collectionsModal").colorbox({

		});
    	});

}




//Main menu dropdown
function innovatorDropdown(action){
    if(action == "show"){
        document.getElementById("innovatorDropdown").style.display = "block";
	aboutDropdown('hide');
	contestDropdown('hide');
    }
    
    if(action == "hide"){
        document.getElementById("innovatorDropdown").style.display = "none";
    } 
}
	

function aboutDropdown(action){
    if(action == "show"){
        document.getElementById("aboutDropdown").style.display = "block";
	innovatorDropdown('hide');
	contestDropdown('hide');
    }
    
    if(action == "hide"){
        document.getElementById("aboutDropdown").style.display = "none";
    }
}


function contestDropdown(action){
    if(action == "show"){
        document.getElementById("contestDropdown").style.display = "block";
	innovatorDropdown('hide');
	aboutDropdown('hide');
    }
    
    if(action == "hide"){
        document.getElementById("contestDropdown").style.display = "none";
    }
}


function hideDropdowns(){
	innovatorDropdown('hide');
	aboutDropdown('hide');
	contestDropdown('hide');
}


