/**
 * @author jeremy.manoto
 */


$(document).ready(function() {
    PageStyle_Init();

    SetAccordion(4);

    SetSearchButton();

    SortFilterDDL();

    NewsArticle_Init();

    SubPageNavigation_Init();

    EventTracking_Init();

});

	function PageStyle_Init() {
		$(".article-quote + .article-quote").css("position", "relative").css("top", "-4px");
	}
	
	/**Initializes the Accordion
	 * 
	 * @param {Object} defaultExpanded
	 */
	function SetAccordion(defaultExpanded) {
		defaultExpanded = (defaultExpanded == null) ? 0 : defaultExpanded;
		
		// Hide all content at first. Resets the list
		$(".list-info").hide();
		
		// Show the first x number of nodes (defined by defaultExpanded)
		$(".list-info").each(function(index, obj) {
			if (index < defaultExpanded) {
				$(obj).show();
				$(obj).prev().addClass("div-open");
			}
			$(obj).css("display", "block");
		})
		
		/*
		$(".results .production-list-item").Accordion({
			header: 'h4.list-heading',
			clickableheader: 'h4.list-heading',
			alwaysOpen: false,
			showSpeed: 400,
			hideSpeed: 400
		});
		*/
		
		//Apply toggle event to hide/show
		$(".results .list-heading").click(function() {
			var listHeading = $(this);
			var listContent = $(this).next();
		
			listContent.slideToggle(300, function() {
	  			if (listContent.css("display") != "none") {
	  				//Open...so show
	  				$(this).prev().addClass('div-open').removeClass('div-closed');
					$(this).addClass('div-open').removeClass('div-closed');
					//$(this).attr("style","");
	         
	  			} else {
	  				//Closed 
	  				$(this).prev().addClass('div-closed').removeClass('div-open');			
	  				$(this).addClass('div-closed').removeClass('div-open');			
					//$(this).attr("style","visibility: hidden");
	  			} 
      		});
			
		}); 
    
	
		//Display subnav current page state
		var currentSubPage = $(".sub_nav-accordion h4.head ul li")
		$(currentSubPage).load(function() {
			 $(currentSubPage).children().addClass('sub_nav-accordian-secondary');
		 }); 

	}

	function captureSearchKeys(e) {
		var event = $(e);
		
		if (e.keyCode == 13) {
			searchPostback();
		}
	}

	function SetSearchButton() {
		
		$(".search-term").keydown(function(e) {
			if (e.keyCode == 13) {
				$(".search-submit").click();
			}
		})
		
		$(".search-button").click(function() {
			$(".search-trigger").click();
		});
		
		$(".search-trigger").toggle(function() {
			var triggerImage = $(this);
			var triggerImageSrc = triggerImage.attr("src");
			triggerImageSrc = triggerImageSrc.replace(".gif", "-left.gif");
			var search = $(this).parent();

			var searchHolder = $(this).prev(".search-holder");
			var buttonGo = searchHolder.children("a").hide();
			searchHolder.animate({ width: "225px"}, 700, null, function() {
				$(this).children("input").focus();
				search.css("background", "#FFFFFF");
				buttonGo.show();
				triggerImage.attr("src", triggerImageSrc);
				triggerImage.hide();
			});
		}, function() {
			var triggerImage = $(this);
			var triggerImageSrc = triggerImage.attr("src");
			triggerImageSrc = triggerImageSrc.replace("-left.gif", ".gif");
			var search = $(this).parent();
			search.css("background", "none");
			
			var searchHolder = $(this).prev(".search-holder");
			var buttonGo = searchHolder.children("a").hide();
			searchHolder.animate({width: "0px"}, 700, null, function() {
				searchHolder.hide();
				triggerImage.attr("src", triggerImageSrc);
				triggerImage.show();
			});
		});
		
	}
	
	
	/** Configure the Sort Filter to show it's drop downs as custom UL's instead of html select boxes
	 * 
	 */
	function SortFilterDDL() {
		$(".sort-filter .filter select").each(function() {
			var selectList = $(this);
			selectList.css("display", "none");
			selectList.before("<ul class='filter-values'></ul>");
			var filterValues = selectList.prev();
			var label = selectList.parent().children("label");
			
			var options = $(this).children("option").each(function(index, obj) {
				var optionName = $(this).html();
				var optionValue = $(this).val();
				
				if (index == 0 && optionValue == "") {
					optionName = "<strong><span title='" + label.html() + "'>Show All</span></strong>"; //"<strong>" + label.html() + "</strong>";
				}
				
				filterValues.append("<li title='" + optionValue + "'>" + optionName + "</li>");
			});
			
			//Add rollover state
			
			
			$(label).click(function() {
				$(this).parent().children(".filter-values").show();
				$(this).parent().children(".filter-values").hover(function() {

					
				}, function() {
					$(this).hide();
					
				});
			});
			
			$(label).parent().children(".filter-values").hover(function() {
				
			}, function() {
				$(this).hide();
			});

			var valueContainer = $(label).parent().children(".filter-values");
			$(valueContainer).children().hover(function() {
					$(this).addClass("hover");
				}, function() {
					$(this).removeClass("hover");
			});
					
			$(valueContainer).children().click(function() {
				//Do filter
				var text = $(this).html();
				var value = $(this).attr("title");
				
				var filterContainer = $(this).parent().parent();
				var filterValue = filterContainer.children(".filter-param");
				filterValue.attr("value", value);
				
				if (value == "") { //Show all Label only
					text = $(this).children("strong").children("span").attr("title");
				}
				
				label.html(text);
				valueContainer.hide();
				selectList.change();
			});
		});
		
	}
	
	
	function NewsArticle_Init() {
		$(".mod-news-item .more-link a").click(function() {
			var thisButton = $(this);
			var newsContainer = $(this).parents(".mod-news-item");
			var fullArticle = newsContainer.children().children(".full-article");
			
			if (fullArticle) {
				if (fullArticle.html() != "") {
					if ((fullArticle.hasClass("open"))) {
						// Article is open so close it
						fullArticle.slideUp("slow");
						fullArticle.removeClass("open");
						thisButton.html("Read Article");
					}
					else {
						// Article is closed so OPEN it.
						fullArticle.slideDown("slow", function(){
							fullArticle.addClass("open");
							thisButton.html("Close");
						});
						
					}
				} else {
					thisButton.remove();
				}
			}
			
		});
    }

    function SubPageNavigation_Init() {
        $(document).ready(function() {
            
        });
    }

    function EventTracking_Init() {
        $("a.download-link").click(function() {
            var downloadedFile = $(this).attr("href");
            TrackEvent('Downloads', 'Download', downloadedFile);
        });

        $("a.video-modal-link").click(function() {
            var videoTitle = $(this).attr("title");
            TrackEvent('Videos', 'View', videoTitle);
        });
    }

    function TrackEvent(Category, Action, Label) {
        //alert("tracking event: " + Category + ", " + Action + ", " + Label);
        
        if (Label) {
            pageTracker._trackEvent(Category, Action, Label);
        }
        else {
            pageTracker._trackEvent(Category, Action);
        }
    }
	