$(document).ready(function(){

	// LEGAL PAGES
	
		// Launch Colorobx when site list links are clicked
		$("a[rel='site_list']").colorbox({opacity:.6});
	
	// BROWSE GAMES main nav popup menu fade in/out
	
		var popup_menu = $("#nav_bar #popup_menu_wrapper");
		var delay = 500;
	   	var tID;

	   	$("a#popup_menu_trigger").hover(
			function(){
				if(tID){clearTimeout(tID);}
	            var tmp = $(this);
	            popup_menu.stop(true,true).fadeIn("normal");
               	 
	        }, 
			function(){
	        	if(tID){clearTimeout(tID);}
	            tID = setTimeout(function(){                          
					popup_menu.stop(true,true).fadeOut("normal")
	            }, delay);
	        }
	    );  
		// this is so the menu doesn't close when you hover out of the trigger link...
	    popup_menu.hover(
			function(){
	    		if(tID){clearTimeout(tID);}
	        	popup_menu.stop(true,true).fadeIn("normal");
	        }, 
			function(){
	            if(tID){clearTimeout(tID);}
	           	tID = setTimeout(function(){                          
	          		popup_menu.stop(true,true).fadeOut("normal")
	            }, delay);
	        }
	    );
	
	
	// SEARCH RESULTS

		// toggle advanced search options pane when button is pressed
		$("a.toggle_search_options").click(function(event) {
			// toggle the div
			$(".search_filter_options").toggle("normal", function() {
			    // Animation complete.
				//alert("Animation complete.");
				// if the div is open, make the button say "Hide Options"
				if($(".search_filter_options").is(":visible")) {
					$("a.toggle_search_options span").html("Hide Options<img src=\"/images/whitearrow_up.png\" />");
				} else {
					$("a.toggle_search_options span").html("Show Options<img src=\"/images/whitearrow_down.png\" />");
				}
			});

			return false;
		});
		
		// advanced search params 
		$("ul.search_filters li a").click(function() {
			//var list_size = $(".category_list li").size();
			//var selected_cat = $("#category :selected").text();
							
			//console.log("current link: ");
			//alert($(this).html());
			
			// if it is not yet selected, add the "selected" class to the a element (and take off the "+")
			if( $(this).attr("class") == "" ) {
				$(this).attr("class","selected");
				var new_name = $(this).html().slice(2); // new_name is everything after the + (and a space)
				$(this).html(new_name); // and set the new_name
				
				// the li element (parent to the a) will have the type of filter this is: esrb, genre, or platform
				var id_to_assign = $(this).parent().attr("class") + "_";
				var name_to_assign = $(this).parent().attr("class") + "[]";
				
				// append the hidden input 
				$("<input id=" + id_to_assign + " name=" + name_to_assign + " type=\"hidden\" value=" + new_name.split(" ").join("_") + " />").appendTo("#hidden_fields");
				
				
			// ...otherwise, take it off (and add the "+" back on)
			} else {
				// remove the "selected" class
				$(this).removeAttr("class");
				
				// figure out the "value" attribute of the hidden element we need to remove
				var element_to_remove = "input[value=\"" + $(this).html().split(" ").join("_") + "\"]";
				
				// replace the "+" in the name
				var new_name = "+ ".concat($(this).html());
				$(this).html(new_name);
				
				// remove the hidden input element
				$(element_to_remove).remove(); 
			}
			
		
			return false;	
		});
		
		// grid view or list view
		$(".view_options a").click(function() {
							
			//console.log("current link: ");
			//alert($(this).html());
			
			// if it is not yet selected, add the "selected" class to the a element (and take off the "+")
			if( $(this).attr("class") == "" ) {
								
				// FIRST, IF the other view option is selected by chance, we need to clear it
				if ( $(".view_options a.selected").html() != null ) {
					//alert("another view options is already selected!");
					
					// figure out the "value" attribute of the hidden element we need to remove
					var to_remove = "input[value=" + $(".view_options a.selected").html().split(" ").join("_") + "]";
					
					// replace the "+" in the name
					var name = "+ ".concat($(".view_options a.selected").html());
					$(".view_options a.selected").html(name);
					$(".view_options a.selected").removeAttr("class"); // clear the "selected" class
					
					// remove the hidden input element
					$(to_remove).remove();
					
				}
				
				$(this).attr("class","selected"); // set the class to "selected"
				var new_name = $(this).html().slice(2); // new_name is everything after the + (and a space)
				$(this).html(new_name); // and set the new_name
				
				// the p element (parent to the a) will have the type of filter this is - view_option in this case
				var id_to_assign = $(this).parent().attr("class");
				var name_to_assign = id_to_assign;
				
				// append the hidden input 
				$("<input id=" + id_to_assign + " name=" + name_to_assign + " type=\"hidden\" value=" + new_name.split(" ").join("_") + " />").appendTo("#hidden_fields");
				
				
				
			// ...otherwise, take it off (and add the "+" back on)
			} else {
				// remove the "selected" class
				$(this).removeAttr("class");
				
				// figure out the "value" attribute of the hidden element we need to remove
				var element_to_remove = "input[value=" + $(this).html().split(" ").join("_") + "]";
				
				// replace the "+" in the name
				var new_name = "+ ".concat($(this).html());
				$(this).html(new_name);
				
				// remove the hidden input element
				$(element_to_remove).remove(); 
			}
			
		
			return false;	
		});
		
		
		// Expand menu when Buy It Now button is clicked
		// when the headlink is clicked (or the ul in case it is already expanded)...
		$("#buy_now_menu_small li.headlink a.headlink_selected").click(function() {

			// change the bg image to the blue "selected" one
			var cssObj = {
				'border' : '0px',
				'text-decoration' : 'none',
				'background' : 'transparent url(/images/buynow_menu_small_top.png) no-repeat',
				'width' : '147px',
				'height' : '26px',
				'padding' : '0'
			}
		    $("#buy_now_menu_small li.headlink a.headlink_selected").css(cssObj); 

			// slide the menu down...
			$(this).parent().find("ul").slideToggle("fast",function(){  
				// ...and in the callback check if we should change the headlink bg image back to the normal "NOT selected" one
				if ($("#buy_now_menu_small li.headlink ul").is(":hidden")) 
				{
					var cssObj = {
						'border' : '0px',
						'text-decoration' : 'none',
						'background' : 'transparent url(/images/buynow_menu_small_normal.png) no-repeat',
						'width' : '147px',
						'height' : '26px'
					}
					$("#buy_now_menu_small li.headlink a.headlink_selected").css(cssObj);
				}	
			});
			return false;

		}); // end Buy It Now menu 
		
		// **************************************************************
	
	
	// GAME
	
		// Launch Colorobx when game images are clicked
		$("a[rel='slideshow']").colorbox({opacity:.6});
	
		// Launch Colorbox when READ MORE link is clicked for the game description
		$("a[rel='full_description']").colorbox({opacity:.6});
		
		// Launch Colorbox when MORE link is clicked for the system requirements
		$("a[rel='full_sys_reqs']").colorbox({opacity:.6});
	
		// Expand menu when Buy It Now button is clicked
		// when the headlink is clicked...
		$("#buy_now_menu li.headlink a.headlink_selected").click(function() {

			// change the bg image to the "menu expanded" one
			var cssObj = {
				'border' : '0px',
				'text-decoration' : 'none',
				'background' : 'transparent url(/images/buynow_menu_top.png) no-repeat',
				'width' : '200px',
				'height' : '41px',
				'padding' : '0'
			}
		    $("#buy_now_menu li.headlink a.headlink_selected").css(cssObj); 

			// slide the menu down...
			$("#buy_now_menu li.headlink ul").slideToggle("fast",function(){  
				// ...and in the callback check if we should change the headlink bg image back to the normal "NOT selected" one
				if ($("#buy_now_menu li.headlink ul").is(":hidden")) 
				{
					var cssObj = {
						'border' : '0px',
						'text-decoration' : 'none',
						'background' : 'transparent url(/images/buynow_menu_normal.png) no-repeat',
						'width' : '200px',
						'height' : '41px'
					}
					$("#buy_now_menu li.headlink a.headlink_selected").css(cssObj);
				}	
			});
			return false;

		}); // end Buy Now menu
	
	
	// SEARCH BOX BLUR/FOCUS
		
		// gains focus		
		$("#nav_bar #topnav_search .default_text").focus(function(srcc) {
			// if the title is Search Games, take off the "default_text_active" class and clear the default value
			if ($(this).val() == $(this)[0].title) {
		    	$(this).removeClass("default_text_active");
		        $(this).val("");
		    }
		});
	
		// loses focus
		$("#nav_bar #topnav_search .default_text").blur(function() {
			//console.log("blur activated!!!");
			//console.log($(this).val());
			// when it loses focus AND it's blank, add back the "default_text_active" class and the default title
		   	if ($(this).val() == "") {
		    	$(this).addClass("default_text_active");
		        $(this).val($(this)[0].title);
		    }
		});

		// by default, make the input box lose focus (and thus restore the title attribute)
  		$("#nav_bar #topnav_search .default_text").blur();	
	
	
	// HOME page row 1 of code slider
	
		$("#coda-slider-1").codaSlider({
			dynamicArrows: false,
			dynamicTabs: false
		});
	
	
	// HOME page row 2 of code slider
	
		$("#coda-slider-2").codaSlider({
			dynamicArrows: false,
			dynamicTabs: false
		});
	
	
	// HOME tool tips
	
	$(".coda-slider a.tooltip_enabled").cluetip({
		splitTitle: '|', 
		arrows: true, 
		cluetipClass: 'aspyr_tooltip',
		cluezIndex: 101,
		activation: 'hover',
		clickThrough: true
		//sticky: true
		}
	);
	
	// CATEGORY PAGE tool tips (and SEARCH GRID tool tips)
	
	$(".search_results_list_wrapper a.tooltip_enabled").cluetip({
		splitTitle: '|', 
		arrows: true, 
		cluetipClass: 'aspyr_tooltip',
		cluezIndex: 101,
		activation: 'hover',
		clickThrough: true
		//sticky: true
		}
	);
	

});

/******************************************/
/************ CUSTOM FUNCTIONS ************/
/******************************************/

// mix of slideToggle and fade 
// got this from http://www.learningjquery.com/2008/02/simple-effects-plugins

jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);  
};

jQuery.fn.submitWithAjax = function() {
	this.unbind('submit', false);
	this.submit(function() {
		$(".add_tags_form .buttons").replaceWith("<img src='/images/ajax_loader.gif' />");
		$.post(this.action, $(this).serialize(), null, "script");
		return false;
	}) 
	return this;
};

  
  

