﻿/*
* Trade Bank of Iraq Scripts
* -----------------------------
* Common functions
* 04.03.09
* -----------------------------
*/

$(document).ready(function() {
    setupTogglers();
    setupRollovers();
    setupTopNav();
    setupBoard();
    setupBranchesMap();
    matchHeight();
    if ($(".headline").length) runTimeline();
    setupSearch();
});

// paging
nextPage = function(order, pagerID) {
    $("#" + pagerID).children(".multipage").eq(order).fadeOut(600, function() {
        $("#" + pagerID).children(".multipage").eq(order + 1).fadeIn(400);    
    });
}

prevPage = function(order, pagerID) {
    $("#" + pagerID).children(".multipage").eq(order).fadeOut(600, function() {
        $("#" + pagerID).children(".multipage").eq(order - 1).fadeIn(400);
    });
}

// tbi timeline
runTimeline = function() {
	headline_count = $("div.headline").size(); 
	$("div.headline:eq(" + current_headline + ")").css('top', '5px'); 
	headline_interval = setInterval(headline_rotate, 10000); 
}

// equal height of divs
matchHeight = function() {
   equalHeight($(".matchHeight")); 
}
     
// FAQs
toggleFaq = function(id) {
   $("#show_faq_" + id).toggle(); 
}
      
// load flash movie
loadFlash = function(id, swf, height, vars) {
   var width = $("#" + id).width(); 
   var params = {
      }; 
   var attributes = {
      }; 
   params.menu = "false"; 
   params.quality = "high"; 
   params.wmode = "transparent"; 
   params.bgcolor = "#000000"; 
   params.allowFullScreen = "true"; 
   params.base = "."; 
   // using a slash '/' here breaks ie6
   params.salign = "tl"; 
   params.scale = "default"; 
   attributes.allowFullScreen = "true"; 
   attributes.id = id; 
   attributes.name = id; 
   swfobject.embedSWF(swf, id, width, height, "9.0.0", false, vars, params, attributes); 
}
   
   
// load flv video
loadVideo = function(id, video, title, height) {
   var width = $("#" + id).width(); 
   var params = {
      }; 
   var flashvars = {
      }; 
   var attributes = {
      }; 
   params.menu = "false"; 
   params.quality = "high"; 
   params.bgcolor = "#000000"; 
   params.allowFullScreen = "true"; 
   params.base = "."; 
   // using a slash '/' here breaks ie6
   params.salign = "tl"; 
   params.scale = "default"; 
   attributes.allowFullScreen = "true"; 
   attributes.id = id; 
   attributes.name = id; 
   flashvars.videoName = video; 
   flashvars.vTitle = "Trade Bank of Iraq"; 
   flashvars.vSubTitle = title; 
   swfobject.embedSWF("/swf/video-player.swf", id, width, height, "9.0.0", false, flashvars, params, attributes); 
}
   
// Google map
loadMap = function(id, lat, lon, description) {
   if (GBrowserIsCompatible()) {
      var map = new GMap2(document.getElementById(id)); 
      map.setCenter(new GLatLng(lat, lon), 5); 
      var marker = new GMarker(new GLatLng(lat, lon)); 
      map.addOverlay(marker); 
      marker.openInfoWindowHtml(description); 
      map.addControl(new GSmallMapControl()); 
      }
}
   
// Google search
loadSearch = function(id, qry) {
    var searchControl = new google.search.SearchControl();

    var options = new google.search.SearcherOptions();
    options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);

    // Add in a full set of searchers
    var tbiSearch = new google.search.WebSearch();
    tbiSearch.setSiteRestriction("tbiraq.com");
    searchControl.setLinkTarget(google.search.Search.LINK_TARGET_SELF);
    searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);

    searchControl.addSearcher(tbiSearch, options);

    // Tell the searcher to draw itself and tell it where to attach
    searchControl.draw(document.getElementById(id));

    // Execute an inital search
    searchControl.execute(qry);
}
  
// search box
setupSearch = function() {
    $("#lnkSearch").click(function() {
        runSearch();
    });
    $("#txtSearch").bind('keypress', function(e) {
        var code = (e.keyCode ? e.keyCode : e.which); if (code == 13) {
            runSearch();
        }
    });
}

runSearch = function() {
    var search = $('#txtSearch').attr('value');
    location.href = "/" + lang + "/search/?qry=" + escape(search);
}
   
// toggle collapsible panels
setupTogglers = function() {
    $(".toggle").parent().click(function() {
        $(this).next(".collapsible").slideToggle("500"); 
        $(this).children(".toggle").children(".open").toggle();
        $(this).children(".toggle").children(".close").toggle();
    }); 
}
   
setupSubNav = function() {
   $("#rollovers .rollover .subnav a").mouseover(function() {
      var id = $(this).attr("id"); if (id.length) {
         var arr = id.split("_"); $("#ro_" + arr[1] + " .rolloverItem").hide(); $("#roContent_" + arr[2]).show(); }
      }
   ); 
   
   $("#rollovers .rollover .subnav a").mouseout(function() {
      var id = $(this).attr("id"); if (id.length) {
         var arr = id.split("_"); $("#ro_" + arr[1] + " .rolloverItem").hide(); $("#roContent_" + arr[1]).show(); }
      }
   ); 
}
   
setupRollovers = function() {
   var lnkID, nodeID; 
   // add mouseover event to top level links
   $(".lnkHover").mouseover(function() {
      var windowWidth = $(window).width(); var windowHeight = $(window).height(); $(".rollover").css("display", "none"); //clear others
      lnkID = $(this).attr("id"); nodeID = lnkID.slice(lnkID.lastIndexOf('_') + 1); 
	$("#ro_" + nodeID).css("display", "block"); 
	  }
   ); 
   $("#nav").mouseout(function() {
      $(".rollover").css("display", "none"); }
   ); 
   $("#rollovers").mouseover(function() {
      // keep overlay & rollover alive when rolling over rollove 
      $("#ro_" + nodeID).css("display", "block"); 
	  }
   ); 
   $("#rollovers").mouseout(function() {
    $(".rollover").css("display", "none"); 
									 }
   ); 
}
   
showOverlay = function(className, leftOffset, topOffset) {
   // Get window dimensions and apply to overlay	
   var windowWidth = $(window).width(); 
   var windowHeight = $(window).height(); 
   var documentHeight = $(document).height(); 
   var scrollPosition = $(window).scrollTop(); 
   if (windowHeight >= documentHeight) {
      var finalHeight = windowHeight; 
      }
   else {
      var finalHeight = documentHeight; 
      }
   $('.overlay.' + className).css( {
      height : windowHeight, width : windowWidth, top : scrollPosition, cursor : "pointer" }
   ); 
   var contentLeft = (windowWidth / 2) - leftOffset; 
   var contentTop = ((windowHeight / 2) - topOffset) + scrollPosition; 
   $('.overlayContent.' + className) .css( {
      top : contentTop , left : contentLeft }
   ); 
   // Animate overlay in, then fade in content
   $('.overlay.' + className).slideDown(250, function() {
      $(this).height(finalHeight).css( {
         top : 0 }
      ); $('.overlayContent.' + className).fadeIn(500); }
   ); 
}
   
   
hideOverlay = function(className) {
   $('.overlayContent.' + className).fadeOut(500, function() {
      var windowHeight = $(window).height(); $('.overlay.' + className).height(windowHeight); $('.overlay.' + className).slideUp(250); }
   ); 
}
   
   
setupTopNav = function(className) {
   $(" #topNav ul ").css( {
      display : "none"}
   ); 
   // Opera Fix
   $(" #topNav li").hover(function() {
      $(this).find('ul:first').css( {
         visibility : "visible", display : "none"}
      ).show(300); }
   , function() {
      $(this).find('ul:first').css( {
         visibility : "hidden"}
      ); }
   ); 
}
   
setupBoard = function() {
   $(".historyShell").css("display", "none"); 
   $(".historyDetail").css("display", "none"); 
   $(".details").toggle( function() {
      $(this).parent(".profile").children(".historyShell").slideDown("fast"); }
   , function() {
      $(this).parent(".profile").children(".historyShell").slideUp("fast"); }
   ); 
}
   
setupBranchesMap = function() {
   $(".countryBullet").click(function() {
      $(".addressBox").css("display", "none"); }
   ); 
   $(".mapOuter").click(function() {
      $(".addressBox").css("display", "none"); }
   ); 
   $(".showNajaf").click(function() {
      $(".najaf").css("display", "block"); }
   ); 
   $(".showBasrah").click(function() {
      $(".basrah").css("display", "block"); }
   ); 
   $(".showBaghdad").click(function() {
      $(".baghdad").css("display", "block"); }
   ); 
   $(".showBaghdad2").click(function() {
      $(".baghdad3").css("display", "block"); }
   ); 
   $(".showBaghdad3").click(function() {
      $(".baghdad2").css("display", "block"); }
   ); 
   $(".showSulaimania").click(function() {
      $(".sulaimania").css("display", "block"); }
   ); 
   $(".showErbil").click(function() {
      $(".erbil").css("display", "block"); }
   ); 
}
   
//tbi timeline
var headline_count; 
var headline_interval; 
var old_headline = 0; 
var current_headline = 0; 
function headline_rotate() {
   current_headline = (old_headline + 1) % headline_count; 
   $("div.headline:eq(" + old_headline + ")").animate( {
      top : - 330}
   , 2000, function() {
      $(this).css('top', '325px'); }
   ); 
   $("div.headline:eq(" + current_headline + ")").show().animate( {
      top : 5}
   , 2000); 
   old_headline = current_headline; 
//   $(".scrollup").mouseover(function() {
//      //top pause on rollover ( not finished, obviously... )	
//      }
//   ); 
}
   
equalHeight = function(divs) {
   tallest = 0; 
   divs.each(function() {
      thisHeight = $(this).height(); if(thisHeight > tallest) {
         tallest = thisHeight + 100; }
      }
   ); 
   //divs.height(tallest);
   $(".singleRight").height(tallest); 
}
   
function bookmark() {
   var title = "Trade Bank of Iraq"; 
   var url = "http://www.tbi.com.iq"; 
   if (window.sidebar) // firefox
   window.sidebar.addPanel(title, url, ""); 
   else if(window.opera && window.print) {
      // opera
      var elem = document.createElement('a'); 
      elem.setAttribute('href', url); 
      elem.setAttribute('title', title); 
      elem.setAttribute('rel', 'sidebar'); 
      elem.click(); 
      }
   else if(document.all)// ie
   window.external.AddFavorite(url, title); 
   else {
      alert("Sorry! Your browser doesn't support this function. Please bookmark this page manually."); 
      }
}
   
// Navigation : IE6 Helper
sfHover = function() {
   var i; 
   var lst1 = $("#topnav"); 
   if (lst1) {
      var sfEls = lst1.getElementsByTagName("li"); 
      for (i = 0; i < sfEls.length; i++) {
         sfEls[i].onmouseover = function() {
            this.className += " sfhover"; 
            }
         sfEls[i].onmouseout = function() {
            this.className = this.className.replace(new RegExp(" sfhover\\b"), ""); 
            }
         }
      }
}

