var IE6 = !window.XMLHttpRequest;
var searchInputActive = false;
var flashIsActive = false;
var setSwfInterval;
var downloadFlashTimeout;
var helpIsOn = false;
var assetID = 0;
var params = { allowFullScreen: "true", wmode: "window", bgcolor: "#ffffff" };
var attributes = { id: "timelineObject", name: "timelineObject" };
var defaultAssetID = 0;
var flashvars;
var searchEnabled = false;
var pageMode;
if (pageMode == undefined) pageMode = "search";
var flashPath = "SearchSwf1";
var searchX;
var searchBox;
var landingSpot = getFlashDate(new Date());
var delegatesUrl = "";//"DataDelegateInstructions.xml";

function loadTimeline(channel, subChannel, timeline) {
  window.location.href = channel + "_" + subChannel + ".htm";
}

function SearchTimeline() {
  if (!searchInputActive) return;
  if (!searchEnabled) return;
  searchEnabled = false;
  searchBox.style.backgroundPosition = (pageMode == "widget") ? "0px -19px" : "-196 -74px";
  var phrase = document.getElementById('SearchText').value;
  var timelineObject = document.getElementById("timelineObject");
  fireStatisticEvent("Search", "phrase");
  if (timelineObject != null) {
    if (timelineObject.search != null) {
      timelineObject.search(phrase);
    }
  }
}

function flashVersionOk(val) {
  if (!flashIsActive) {
    flashIsActive = true;
    if (!val) {
      document.getElementById("head").style.display = "none";
      document.getElementById("alternative_head").style.display = "block";
    }
  }
}

function openHtmlWindow(url) {
    window.open(url, null, "height=490,width=420,status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbar=no");
}

function assetClicked(assetID) {
  window.location.hash = '' + assetID;
}

function balloonClosed(){
  window.location.hash = '';
}

function setSwfParams() {
  if (window.location.hash != "")
    defaultAssetID = parseInt(window.location.hash.substr(1, 20));
  var AomUrl = "http://www.allofme.com";
  flashvars = {
    "Viewer": "0",
    "IsLoggedIn": "false",
    "PageType": "timeline",
    "Channel": "culture",
    "SubChannel": "leisure",
    "Timelines": timelineID,
    "OwnPage": "false",
    "TimeScaleID": "1",
    "ExternalMode": "1",
    "LandingSpot": landingSpot,
    "BetaRegistered": "true",
    "ApiUrl": AomUrl + "/Misc/ApiBroker.aspx",
    "ApplicationSkin": "YNet",
    "ExternalSearch": "true",
    "WebProvider.StaticBase": escape("http://seuro1.allofme.com/"),
    "DefaultAssetID": defaultAssetID,
	"SwfPath": flashPath,
    "DataDelegateInstructionsFile": delegatesUrl
  };

}

function documentLoaded() {
  switch(pageMode){
    case "widget":
		searchBox = document.getElementById("widget_searchbox");
		searchX = document.getElementById("widget_search_x");
		break;
    case "search":
        searchContainer = '<div class="search_search_container">';
        searchContainer += '    <div style="width: 200px; height: 10px; float: right"></div>';
        searchContainer += '    <input type="text" id="SearchText" style="float: right; margin-top: 7px" value="חפש בציר הזמן"';
        searchContainer += '        onfocus="if (!searchInputActive) clearSearch()" onkeyup="searchKeydown(event)" />';
        searchContainer += '    <div id="search_searchbox" onmouseover="box_over()" onmouseout="box_out()" onclick="SearchTimeline()">';
        searchContainer += '    </div>';
        searchContainer += '    <div id="search_search_x" onclick="resetSearch()"></div>';
        searchContainer += '</div>';
        if (IE6) {
            document.getElementById("search_for_ie6").innerHTML = searchContainer;
            document.getElementById("search_for_ie6").style.display = "block";
        } else {
            document.getElementById("search_not_ie6").innerHTML = searchContainer;
            document.getElementById("search_not_ie6").style.display = "block";
        }
		searchBox = document.getElementById("search_searchbox");
		searchX = document.getElementById("search_search_x");
		break;
	default:
	   searchBox = document.getElementById("searchbox");
		searchX = document.getElementById("search_x");
}
  resetSearch();
  documentResized();
  setSwfInterval = setInterval(embedFlash, 200);
  downloadFlashTimeout = setTimeout(downloadFlash, 10000);
  if (pageMode != "widget") window.onresize = documentResized;

}

function embedFlash() {
    if (flashIsActive) {
        var mainSwf;
        switch (pageMode) {
            case "widget": mainSwf = flashPath + "/YnetWidget.swf"; break;
            default: mainSwf = flashPath + "/all.swf";
        }
        swfobject.embedSWF(mainSwf, "timeline_container_swf", "100%", "100%", "10.0.12.36", flashPath + "/expressInstall.swf", flashvars, params, attributes);
        clearInterval(setSwfInterval);
        clearInterval(downloadFlashTimeout);
  }
}

function downloadFlash() {
  clearInterval(setSwfInterval);
  if (!flashIsActive) {
    document.getElementById("head").style.display = "none";
    document.getElementById("alternative_head").style.display = "block";
  }
}

function documentResized() {
  var h = document.body.offsetHeight;
  var containerDiv = (pageMode == "widget") ? document.getElementById("widget_container") : document.getElementById("timeline_container");
  var timelineHeight;
  if (pageMode == "widget")
      timelineHeight = h - document.getElementById("widget_head").offsetHeight;
  else
      timelineHeight = h - document.getElementById("head").offsetHeight;
  containerDiv.style.height = timelineHeight + 'px';
}

function searchIsReady(){
    document.getElementById("SearchText").disabled = false;
    searchEnabled = true;
    box_out();
    if (pageMode == "widget") {
        document.getElementById("widget_head").style.backgroundPosition = "top left";
    }
}

function box_over() {
  if (!searchEnabled) return;
  switch(pageMode){
    case "widget":
        //searchBox.style.backgroundPosition = "0px -38px";
	  break;
	case "search":
	  searchBox.style.width = helpIsOn ? "244px" : "48px";
	  searchBox.style.marginLeft = helpIsOn ? "0px" : "196px";
	  searchBox.style.backgroundPosition = helpIsOn ? "0px -37px" : "-196px -37px";
	  searchX.style.marginRight = "-260px";
	  break;
	default:
      searchBox.style.width = helpIsOn ? "244px" : "48px";
      searchBox.style.marginLeft = helpIsOn ? "0px" : "196px";
      searchBox.style.backgroundPosition = helpIsOn ? "0px -37px" : "-196px -37px";
  }
}

function box_out() {
  if (!searchEnabled) return;
  switch(pageMode){
    case "widget":
        searchBox.style.backgroundPosition = "0px -57px";
	  break;
	case "search":
      searchBox.style.width = "48px"
      searchBox.style.backgroundPosition = "top right";
      searchX.style.marginRight = helpIsOn ? "-64px" : "-260px";
	  break;
	default:
      searchBox.style.marginLeft = "196px"
      searchBox.style.width = "48px"
      searchBox.style.backgroundPosition = "top right";
  }
}

function box_click() {
  SearchTimeline();
}

function resetSearch() {
  document.getElementById("SearchText").value = "חפש בציר הזמן";
  document.getElementById("SearchText").style.color = "#97aabd";
  searchX.style.display = "none";
  searchInputActive = false;
  var timelineObject = document.getElementById("timelineObject");
  if (timelineObject != null)
    if (timelineObject.search != null)
    timelineObject.search("");
}

function clearSearch() {
  document.getElementById("SearchText").style.color = "#000000";
  document.getElementById("SearchText").value = "";
  searchX.style.display = "block";
  searchInputActive = true;
}

function searchKeydown(e) {
  keynum = e.keyCode;
  switch (keynum) {
    case 27: // escape:
      resetSearch();
      document.getElementById("SearchText").blur();
      break;
    case 13: // enter:
      SearchTimeline();
      break;
  }
}

function noenter() {
  return !(window.event && window.event.keyCode == 13);
}

function helpMode(mode) {
  helpIsOn = mode;
}

function getFlashDate(d) {
  var flashDate = "";
  var shortMonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  flashDate += shortMonths[d.getMonth()] + "/";
  if (d.getDate() < 10) flashDate += "0";
  flashDate += d.getMonth() + "/" + d.getFullYear() + " ";
  if (d.getHours() < 10) flashDate += "0";
  flashDate += d.getHours() + ":";
  if (d.getMinutes() < 10) flashDate += "0";
  flashDate += d.getMinutes() + ":";
  if (d.getSeconds() < 10) flashDate += "0";
  flashDate += d.getSeconds();
  return flashDate;
}

function fireStatisticEvent(category, action, label, value) {
  if (label == "") {
    pageTracker._trackEvent(category, action);
  } else {
  if (value == 0) {
      pageTracker._trackEvent(category, action, label);
    } else {
    pageTracker._trackEvent(category, action, label, value);
    }
  }
}
