//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




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




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


// END SITE SEARCH






// VIDEOS: ADVANCED SEARCH FILTER


//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 = "/search-videos.aspx" + 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');
	americaInventsDropdown('hide');
	topicsDropdown('hide');
    }
    
    if(action == "hide"){
        document.getElementById("innovatorDropdown").style.display = "none";
    } 
}

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


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


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


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

    
    if(action == "hide"){
        document.getElementById("contestDropdown").style.display = "none";
    }
}


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



function hideDropdowns(){
	innovatorDropdown('hide');
	aboutDropdown('hide');
	contestDropdown('hide');
	educationDropdown('hide');
	americaInventsDropdown('hide');
	topicsDropdown('hide');
}
*/






//Main menu dropdown

function innovatorDropdown(action, state){
    if(action == "show"){
        document.getElementById("innovatorDropdown").style.display = "block";
        document.getElementById("innovatorDropdown-shadow").style.display = "block";

	document.getElementById("nav_innovatorsLink").style.backgroundPosition = "0 -44px";

	aboutDropdown('hide');
	americaInventsDropdown('hide');
	topicsDropdown('hide');
    }
    
    if(action == "hide"){
        document.getElementById("innovatorDropdown").style.display = "none";
        document.getElementById("innovatorDropdown-shadow").style.display = "none";

	if(state == 'active'){
		document.getElementById("nav_innovatorsLink").style.backgroundPosition = "0 -44px";
	}

	if(state == 'inactive'){
		document.getElementById("nav_innovatorsLink").style.backgroundPosition = "0 0px";
	}

    } 
}

	
function americaInventsDropdown(action, state){
    if(action == "show"){
        document.getElementById("americaInventsDropdown").style.display = "block";
        document.getElementById("americaInventsDropdown-shadow").style.display = "block";

	document.getElementById("nav_americaInventsLink").style.backgroundPosition = "0 -44px";

	innovatorDropdown('hide');
	aboutDropdown('hide');
	topicsDropdown('hide');
    }
    
    if(action == "hide"){
        document.getElementById("americaInventsDropdown").style.display = "none";
        document.getElementById("americaInventsDropdown-shadow").style.display = "none";

	if(state == 'active'){
		document.getElementById("nav_americaInventsLink").style.backgroundPosition = "0 -44px";
	}

	if(state == 'inactive'){
		document.getElementById("nav_americaInventsLink").style.backgroundPosition = "0 0px";
	}
    } 
}


function aboutDropdown(action, state){
    if(action == "show"){
        document.getElementById("aboutDropdown").style.display = "block";
        document.getElementById("aboutDropdown-shadow").style.display = "block";

	document.getElementById("nav_aboutLink").style.backgroundPosition = "0 -44px";

	innovatorDropdown('hide','');
	educationDropdown('hide','');
	americaInventsDropdown('hide','');
	topicsDropdown('hide','');
    }
    
    if(action == "hide"){
        document.getElementById("aboutDropdown").style.display = "none";
        document.getElementById("aboutDropdown-shadow").style.display = "none";

	if(state == 'active'){
		document.getElementById("nav_aboutLink").style.backgroundPosition = "0 -44px";
	}

	if(state == 'inactive'){
		document.getElementById("nav_aboutLink").style.backgroundPosition = "0 0px";
	}
    }
}


function educationDropdown(action, state){
    if(action == "show"){
        document.getElementById("educationDropdown").style.display = "block";
        document.getElementById("educationDropdown-shadow").style.display = "block";

	document.getElementById("nav_educationLink").style.backgroundPosition = "0 -44px";
	
	innovatorDropdown('hide');
	aboutDropdown('hide');
	americaInventsDropdown('hide');
	topicsDropdown('hide');
    }
    
    if(action == "hide"){
        document.getElementById("educationDropdown").style.display = "none";
        document.getElementById("educationDropdown-shadow").style.display = "none";

	if(state == 'active'){
		document.getElementById("nav_educationLink").style.backgroundPosition = "0 -44px";
	}

	if(state == 'inactive'){
		document.getElementById("nav_educationLink").style.backgroundPosition = "0 0px";
	}

    }
}


function topicsDropdown(action, state){
    if(action == "show"){
        document.getElementById("topicsDropdown").style.display = "block";
        document.getElementById("topicsDropdown-shadow").style.display = "block";

	document.getElementById("nav_topicsLink").style.backgroundPosition = "0 -44px";

	innovatorDropdown('hide');
	aboutDropdown('hide');
	educationDropdown('hide');
	americaInventsDropdown('hide');
    }
    
    if(action == "hide"){
        document.getElementById("topicsDropdown").style.display = "none";
        document.getElementById("topicsDropdown-shadow").style.display = "none";

	if(state == 'active'){
		document.getElementById("nav_topicsLink").style.backgroundPosition = "0 -44px";
	}

	if(state == 'inactive'){
		document.getElementById("nav_topicsLink").style.backgroundPosition = "0 0px";
	}
    }
}



function hideDropdowns(){
	innovatorDropdown('hide','');
	aboutDropdown('hide','');
	educationDropdown('hide','');
	americaInventsDropdown('hide','');
	topicsDropdown('hide','');
}




function innovatorInfo(action,item) {
    if(action == "show"){
        document.getElementById(item).style.display = "block";
        //document.getElementById('arrow_' + item).style.display = "block";
	document.getElementById('dropdownInnovatorInfo-shadow_' + item).style.display = "block";
    }

    if(action == "hide"){
        document.getElementById(item).style.display = "none";
        //document.getElementById('arrow_' + item).style.display = "none";
	document.getElementById('dropdownInnovatorInfo-shadow_' + item).style.display = "none";
    }
}



function videoQuickviewLink(action,item) {
    if(action == "show"){
        document.getElementById('quickviewLink'+item).style.display = "block";
        document.getElementById('thumbHolder'+item).style.backgroundPosition = "0 0px";
    }

    if(action == "hide"){
        document.getElementById('quickviewLink'+item).style.display = "none";
        document.getElementById('thumbHolder'+item).style.backgroundPosition = "0 -325px";
    }
}



function videoDetailLink(action,item) {
    if(action == "show"){
        document.getElementById('detailLink'+item).style.display = "block";
        document.getElementById('thumbHolder'+item).style.backgroundPosition = "0 0px";
    }

    if(action == "hide"){
        document.getElementById('detailLink'+item).style.display = "none";
        document.getElementById('thumbHolder'+item).style.backgroundPosition = "0 -325px";
    }
}





