﻿// Main Javascript File
YAHOO.namespace('anim');

var searchBox;
var search_right;

function swapCSS(id, css) {
		var obj = document.getElementById(id);
		obj.className = css;
}

function swapImage(id, path) {
    var obj = document.getElementById(id);
	obj.src = path;
}

function show(id, type) {
    var element = document.getElementById(id);
    if (type) {
        element.style.display = type;
    }else{
        element.style.display = "inline";
    }
}

function hide(id) {
    var element = document.getElementById(id);
    element.style.display = "none";
}

function toggleTeaser(id, toggle, height, width) {
    if (width == null){width = 320;}
    if (height == null){height = 80;}
    var obj = document.getElementById(id);
    if (toggle == "on") {
        obj.style.width = 0;
        obj.style.height = 0;
        obj.style.padding = 0;
        obj.style.display = "inline";
        var attributes = {
          padding: {to: 4, unit: 'px'},
          width: {to: width},
          height: {to: height},
          opacity: {from: 0, to: .85}
        }
        var myAnim = new YAHOO.util.Anim(id, attributes, .35, 
                                 YAHOO.util.Easing.easeOut);
        //myAnim.onComplete.subscribe(addPadding);
        myAnim.animate();
    }else{
        obj.style.display = "none";
    }
}

// Master page specific
var globalImageRoot = "/images/global/"; //Path to global image directory
var isOverSearchButton = false;
var defaultSearchText = "Type a keyword";

//Global search command
function processSearch(keyword, type) {
    var path = "/cms/search.aspx";
    var query = "?q=";
    if (!type)
    {
        type = '1';
    }
    if ((keyword.length > 0) && (keyword != defaultSearchText)) {
        alert(path);
        document.location.href = (path + query + keyword + "&t=" + type);
    }
}

function search_onMouseEvent(toggle) {
    var doc = document;
    var p = globalImageRoot;
    
    if (toggle == "off"){
        var p = globalImageRoot;
        swapImage("search_left", p + "header_search_left.gif");
        swapImage("search_top", p + "header_search_top.gif");
        swapImage("search_botm", p + "header_search_botm.gif");
        if (!isOverSearchButton) {
            swapImage(search_right, p + "header_search_right.gif");
        }
    }else{
		swapImage("search_left", p + "header_search_left_glow.gif");
		swapImage("search_top", p + "header_search_top_glow.gif");
		swapImage("search_botm", p + "header_search_botm_glow.gif");
		if (!isOverSearchButton) {
		    swapImage(search_right, p + "header_search_right_glow.gif");
		}
	}
}

function searchButton_onMouseEvent(toggle) {
    var p = globalImageRoot;
	if (toggle == "off"){
		swapImage(search_right, p + "header_search_right_glow.gif");
		isOverSearchButton = false;
		search_onMouseEvent("off");
	}else{
		swapImage(search_right, p + "header_search_right_roll.gif");
		isOverSearchButton = true;
		search_onMouseEvent("on");
	}
}

function searchBox_onFocusEvent() {
	var obj = document.getElementById(searchBox);
    if (obj.value == defaultSearchText){
	    obj.value = "";
	}else{
	    if (obj.value == "") {
	        obj.value = defaultSearchText;
		}
	}
}

function searchButton_onMouseUp() {
    var q = document.getElementById(searchBox).value;
    processSearch(q);
}

function initSearchGlow() {
    //Preloads images used for search glow
    var p = globalImageRoot;
    //Mouse over images
    img1 = new Image();
    img1.src = p + "header_search_left_glow.gif";
	img2 = new Image();
    img2.src = p + "header_search_right_glow.gif";
	img3 = new Image();
    img3.src = p + "header_search_top_glow.gif";
	img4 = new Image();
    img4.src = p + "header_search_botm_glow.gif";
    img5 = new Image();
    img5.src = p + "header_search_right_roll.gif";
    //Mouse out images - default
    img6 = new Image();
    img6.src = p + "header_search_left.gif";
	img7 = new Image();
    img7.src = p + "header_search_right.gif";
	img8 = new Image();
    img8.src = p + "header_search_top.gif";
	img9 = new Image();
    img9.src = p + "header_search_botm.gif";
}

//****Search page********


//Called when any keypress event occurs
function KeyPress(e){
  try {
    var key = window.event.keyCode;
  }catch(ex){
    var key = e.which;
  }
  //Check for enter key, then execute search query
  if(key == 13)
  {
    var headerKeyword = document.getElementById(searchBox).value;
    if ((headerKeyword.length > 0) && (headerKeyword != defaultSearchText))
    {
        processSearch(headerKeyword);
    }
    else
    {
        var cmsSearch = document.getElementById('txtSearchKeyword');
        if (cmsSearch != null)
        {
            var cmsPageKeyword = document.getElementById('txtSearchKeyword').value;
            var typeElement = document.getElementById('ddlSearchType');
            var type = typeElement.options[typeElement.selectedIndex].value;
            processSearch(cmsPageKeyword, type);
        }
    }
  }
}

//function searchButton_onClick(searchId, typeId) {
//    var keyword = document.getElementById(searchId).value;
//    var type = document.getElementById(typeId).options[document.getElementById(typeId).selectedIndex].value;
//    processSearch(keyword, type);
//}

//******Gallery page*****

function initPage() {
    initSearchGlow();
    
    //Registers an event handler for any keypress event on the page
//    try {
//      document.addEventListener("keypress", KeyPress, true);
//    }catch(e){
//      try {
//        document.attachEvent("onkeypress", KeyPress);
//      }catch(e){
//        alert(e);
//      }
//    }
}

initPage();