﻿/* MUCH AXS */
function searchAXS( searchTerms )
{
	searchTerms = "%22" + escape( searchTerms ) + "%22";
	launchMediaPlayer( "http://axs.muchmusic.com/?fr_search=" + searchTerms );
}

/* INSTRUCTIONS */
function closeMe(theAnchor)
{
    theAnchor.parentNode.parentNode.removeChild(theAnchor.parentNode);
    
    createCookie(COOKIE_NAME,CLOSED_VAL,10);
}
function createCookie(name,value,days) 
{
    if (days) 
    {
	    var date = new Date();
	    date.setTime(date.getTime()+(days*24*60*60*1000));
	    var expires = "; expires="+date.toGMTString();
    }
    else 
    {
        var expires = "";
    }
    
    document.cookie = name+"="+value+expires+";";
}

/* MAKE A COUNTDOWN */
function initAddSongAutoCompletes()
{
    var numberOfSongs = 5;
    for(i=1;i<numberOfSongs+1;i++)
    {
        initAutoComplete
	    (
			    "countdownArtist"                       ,
			    CONTROL_PREFIX +"_artist"  + i		    ,
			    "artistContainer"          + i  		,
			    ["Item", "Artist"]
	    );
    				
	    /*initAutoComplete
	    (		
			    "countdownKeywordWithArtist"            ,
			    CONTROL_PREFIX +"_song"   + i   	    ,
			    "songContainer"           + i        	,
			    ["Item", "Song"]                        
	    );*/
	}
}
function updateArtist(songTextBoxIndex,artistTextBox)
{
    if(artistTextBox.value != artistTextBox.oldValue)
    {
        initAutoComplete
	    (		
			    "countdownKeywordWithArtist"                        ,
			    CONTROL_PREFIX +"_song"         + songTextBoxIndex	,
			    "songContainer"                 + songTextBoxIndex 	,
			    ["Item", "Song"]                                    ,
			    "artistString="                 + artistTextBox.value
	    );
	    
	    artistTextBox.oldValue = artistTextBox.value;
    }
}
function showRulesPopUp()
{
    window.open("Rules.htm",null,"height=600,width=400,status=yes,toolbar=no,menubar=no,location=no,scrollbars=1");
}
function checkTagText(tagArea, isBlur)
{
   var oldText = tagArea.oldText == null || tagArea.oldText == "undefined" 
                    ? tagArea.getAttribute( "oldText" ) : tagArea.oldText;

   if( stripBadLineBreakChars( tagArea.value ) == stripBadLineBreakChars( oldText )  )
   {
        tagArea.value = "";
   }
   else if( tagArea.value == "" && isBlur )
   {
        tagArea.value = oldText
   }
}
function stripBadLineBreakChars( string )
{
    // return line breaks
    return unescape( escape( string )
                        .replace(/%0D%0A/g,"")  //Windows line berak
                        .replace(/%0A/g,"")     //UNIX line break
                        .replace(/%0D/g, "")    //other line break  
                   );
}


/* SEARCH */

	var ArtistSearchMessage		= "Search Artists";
	var KeywordSearchMessage	= "Search Songs";
	var TagSearchMessage        = "Search Tags";
	var lastShown;
	var isShowing;

	function changeSearchArea( selectBox, forceSelected )
	{
		var toShow = selectBox.options[ selectBox.selectedIndex ].value.toLowerCase();

		if( forceSelected )
		{
			toShow = forceSelected.toLowerCase();
			for(var i=0;i< selectBox.options.length;i++)
			{
				selectBox.options[i].selected = ( forceSelected.toLowerCase() == selectBox.options[i].value.toLowerCase() );
			}
		}

		var artistSearchArea	= document.getElementById( "artistSearch" );
		var keywordSearchArea	= document.getElementById( "keywordSearch" );
        var tagSearchArea       = document.getElementById( "tagSearch" );
        
		if( toShow == "artistsearch" )
		{
			artistSearchArea.style.display = "block";
			keywordSearchArea.style.display = "none";
			tagSearchArea.style.display = "none";
			
			if( lastShown ==  "keywordsearch" && document.getElementById("autocompletekeywordinput").value != KeywordSearchMessage )
			{
				document.getElementById("autocompleteinput").value = document.getElementById("autocompletekeywordinput").value;
			}
			else if( lastShown ==  "tagsearch" && document.getElementById("autocompletetaginput").value != TagSearchMessage ) 
			{
			    document.getElementById("autocompleteinput").value = document.getElementById("autocompletetaginput").value;
			}
			lastShown = toShow;
		}
		else if( toShow == "keywordsearch" )
		{
			artistSearchArea.style.display = "none";
			tagSearchArea.style.display = "none";
			keywordSearchArea.style.display = "block";
			
			if( lastShown ==  "artistsearch" && document.getElementById("autocompleteinput").value != ArtistSearchMessage )
			{
				document.getElementById("autocompletekeywordinput").value = document.getElementById("autocompleteinput").value;
			}
			else if( lastShown ==  "tagsearch" && document.getElementById("autocompletetaginput").value != TagSearchMessage ) 
			{
			    document.getElementById("autocompleteinput").value = document.getElementById("autocompletetaginput").value;
			}
			lastShown = toShow;
		}
		else if( toShow == "tagsearch" )
		{
			artistSearchArea.style.display = "none";
			keywordSearchArea.style.display = "none";
			tagSearchArea.style.display = "block";
			
			if( lastShown ==  "artistsearch" && document.getElementById("autocompleteinput").value != ArtistSearchMessage )
			{
				document.getElementById("autocompletetaginput").value = document.getElementById("autocompleteinput").value;
			}
			else if( lastShown ==  "keywordsearch" && document.getElementById("autocompletekeywordinput").value != KeywordSearchMessage )
			{
				document.getElementById("autocompletetaginput").value = document.getElementById("autocompletekeywordinput").value;
			}
			lastShown = toShow;
		}
		else
			return;
			
		isShowing = toShow;
	}
	function showSearchMessage( theInput)
	{
		var theSearchMessage =  getSearchMessage();
     
		if( theInput.value == "")
		{
			theInput.value = theSearchMessage;
		}
		else if ( theInput.value == theSearchMessage )
		{
			theInput.value = "";
		}
	}
	function getSearchMessage()
	{
		if( isShowing=="artistsearch")
		{
			return ArtistSearchMessage;
		}
		else if( isShowing=="keywordsearch" )
		{
			return KeywordSearchMessage;
		}
		else if( isShowing=="tagsearch" )
		{
			return TagSearchMessage;
		}
		
		return "";
	}
	function SearchEnter(e)
	{
		if (!e) var e = window.event;
		
		if( e.keyCode == 13 )
		{	
			submitForm();
			return false;
		}
	}
	function submitForm()
	{
	    var toSearch        = isShowing=="artistsearch"  ?  "SearchArtists"     : 
	                          isShowing=="keywordsearch" ?  "SearchSongs"       : 
	                                                        "SearchTags"        ;
	                          
	    var searchValue     = isShowing=="artistsearch"  ?  document.getElementById("autocompleteinput").value           :
	                          isShowing=="keywordsearch" ?  document.getElementById("autocompletekeywordinput").value    : 
	                                                        document.getElementById("autocompletetaginput").value;
	                                                        
	    window.location     = "SearchCountdown.aspx?searchQuery=" + toSearch + "&searchTerm=" + searchValue; 
	}
	function SearchArtists(toSearch)
	{
	    isShowing = "artistsearch";
	    document.getElementById("autocompleteinput").value = toSearch;
	    
	    submitForm();
	}
	function SearchSongs(toSearch)
	{
	    isShowing = "keywordsearch";
	    document.getElementById("autocompletekeywordinput").value = toSearch;
	    
	    submitForm();
	}
	function initAutoComplete( methodToCall, theInputID, theContainerID, theXMLschema, addToQueryString ) 
	{
		if(! methodToCall )
			methodToCall = "countdownArtist";

		var theAJAXpage		= "AJAXhandler.aspx";
		
		httpConnection = new YAHOO.widget.DS_XHR	
										(
											theAJAXpage	,				
											theXMLschema
										);

		with( httpConnection )
		{
			scriptQueryParam	= "toSearchFor";
			responseType		= YAHOO.widget.DS_XHR.TYPE_XML;
			scriptQueryAppend	= "toLookUp=" + methodToCall + (addToQueryString ? "&" + addToQueryString : ""); 
			maxCacheEntries 	= 100;  
		}
		theAutoComplete = new YAHOO.widget.AutoComplete( 
															theInputID			,
															theContainerID		,
															httpConnection
														);

		with( theAutoComplete )
		{
			autoHighlight		= false;
			forceSelection      = false;   
			typeAhead			= true;
			useShadow			= true;
			useIFrame  			= false;
			animSpeed			= 0.1;
			queryDelay			= 0.4;
			formatResult		= function(oResultItem, sQuery) 
									{
										var result	= oResultItem[0];
										return ( result );
									};
		}													
	}
	var oldonload = window.onload;
	window.onload =	function()
					{
                        if( oldonload )
                        {
                            oldonload();
                        }
                        var toShow = "artistSearch";
   
                        changeSearchArea( document.getElementById("searchType"), toShow );
                        
                        initAutoComplete
						(
								"countdownArtist"						,
								"autocompleteinput"						,
								"autocompletecontainer"					,
								["Item", "Artist"]
						);
									
						initAutoComplete
						(		
								"countdownKeyword"						,
								"autocompletekeywordinput"				,
								"autocompletekeywordcontainer"			,
								["Item", "Song"]
						);
						
						initAutoComplete
						(		
								"countdownTag"						    ,
								"autocompletetaginput"				    ,
								"autocompletetagcontainer"			    ,
								["Item", "Tag"]
						);
                    };
