var videoWidth  = "640"; // in pixels
var videoHeight = "389"; // in pixels
var scrollbarColor = "0x729fa2";
var playerPath = "/images/2010/player.swf";

$(document).ready(function () {
    convertTabTable1();
    convertRolloutList();
    convertFLVLinks();
    setupCountryPageHashHandler();
    
    /*2011-08-08*/
    rolloutpanel();
        
        
});


function rolloutpanel() {
	$(".slidelink").click(function(){
	
		$(this).siblings('.rolloutpanel').slideToggle(); 
		
	  });  
}


function setupCountryPageHashHandler() {
    if (window.location.hash != "") {
        var params = new Object();
        var tokens = window.location.hash.replace("#", "").split("&");
        for (i = 0; i < tokens.length; i++) {
            var kv = tokens[i].split("=");
            params[kv[0]] = kv[1];
        }

        switch (params["t"]) {
            case "o":
                $("ul.tabs a:contains('Main Office'), ul.tabs a:contains('Office Details')").click();
                break;
        }
    }
}

function convertTabTable1() {
  $("table.tab-table-1").each(function() {
    var tt = new Array();
    $(this).find("tr:not(tr table tr)").each(function(i) {
      tt.push({
        title:   $(this).find('td:eq(0)').html(),
        content: $(this).find('td:eq(1)').html(),
        tabid:   $(this).attr("id")
      });
    });

    var tabTableDiv = $('<div class="tab-table-1"></div>');
    var tabs        = $('<ul class="tabs"></ul>');
    var panels      = $('<ul class="panels"></ul>');

    $(tt).each(function(i, val) {
      tabs.append($('<li id="' + val.tabid + '"><a href="#"' + ((i == 0) ? ' class="active"' : '') + '><span>' + val.title + '</span></a></li>'));
      panels.append($('<li' + ((i == 0) ? ' class="active first"' : '') +
        '><div class="panel-content-header"></div><div class="panel-content">' + val.content + 
        '</div><div class="panel-content-footer"></div></li>'));
    });
    tabs.append('<div style="clear:both"></div>');

    $(tabs).find("a").each(function(i, el) {
      $(this).click(function() {
        tabTableDiv = $(this).parents("div.tab-table-1");
        clickIndex  = tabTableDiv.find("ul.tabs a").index($(this));

        tabTableDiv.find("ul.tabs li a.active, ul.panels > li.active").removeClass("active");
        tabTableDiv.find("ul.tabs li a:eq(" + clickIndex + "), ul.panels > li:eq(" + clickIndex + ")").addClass("active");

        return false;
      });
    });

    tabTableDiv.append(tabs).append(panels);
    $(this).after(tabTableDiv);
    $(this).remove();
    tabTableDiv.after($('<div class="tab-table-1-footer">'));

    if (location.href.indexOf("#") > 0) {
      var tokens = location.href.split("#");
      if ($("li#" + tokens[1]).length > 0) {
        tabs.find("li#" + tokens[1] + " a").click();
      }
    }
  });
}

function convertRolloutList() {
    $("ul.rollout > li > p, ul.ROLLOUT > li > p").each(function () {
        if ($(this).children(":eq(1)").is("br")) {
            var clonedMe = $(this).clone();
            var clonedStrong = clonedMe.children(":eq(0)");
            var clonedSiblings = $(this).siblings().clone();

            clonedMe.children(":eq(0)").remove();
            clonedMe.children(":eq(0)").remove();
            $(this).siblings().remove();

            var insideHtml = clonedMe.html();
            $(this).html("");
            clonedStrong.css({ 'cursor': 'pointer' });

            $(this).append('<strong><a href="#" onclick="return false;" >' + clonedStrong.text() + '</a></strong>');
            var newDiv = $('<div class="rollout-content">' + clonedMe.text() + '</div>');
            newDiv.append(clonedSiblings);
            newDiv.hide();
            $(this).append(newDiv);
        }
        else {
            $(this).find("a").click(function () {
                wnd = window.open($(this).attr("href"), "svcwnd", "width=1100,height=600,scrollbars=yes,resizable=yes");
                wnd.focus();
                return false;
            });
            /*
            $(this).find("a").click(function () {
            overlay = $('<div style="border:0;width:610px;height:410px;padding:10px;position:absolute;margin:0 auto;top:0;"><a href="#" class="close-btn">Close</a></div>');
            overlay.append('<div style="border:1px solid #ccc;padding:2px;"><iframe src="' + $(this).attr("href") + '" width="600" height="400" frameborder="0"></div>');
            $("body").append(overlay);
            overlay.find("a.close-btn").click(function () {
            $(this).parent().remove();
            return false;
            });

            return false;
            });
            */
        }
    });
  $('ul.rollout li, ul.ROLLOUT li').click(function() {
    if ($(this).hasClass('opened') == false) {
      $('div.rollout-content').slideUp();
      $('ul.rollout li').each(function() {
        $(this).removeClass('opened');
      });
      $(this).find('div.rollout-content').slideDown();
      $(this).addClass('opened');
    }
  });
}

function convertFLVLinks() {
  $("a[href$=.flv], a[href*=.flv?], a[href*=.flv#]").each(function() {
    if ($(this).hasClass("noplayer")) {
      return ;
    }
    
    $(this).click(function() {
      $("#video-overlay").remove();
      var overlayDiv = $('<div id="video-overlay">');
      $("body").append(overlayDiv);
      overlayDiv.before('<div id="video-overlay-blocker">');

    	$('#video-overlay-blocker').css({
    		"width":    $(document).width(),
    		"height":   $(document).height()
    	});

      var headerDiv  = $('<div class="header">');
      var contentDiv = $('<div class="content">');
      var closeLink  = $('<a href="#" id="video-overlay-close" class="axn-close">Close</a>');

    	overlayTop  = ($(window).height() - overlayDiv.height()) / 2;
    	overlayLeft = ($(window).width() - overlayDiv.width()) / 2;
      overlayDiv.css({
        "position": "absolute",
        "top":  overlayTop + $(window).scrollTop() + "px",
        "left": overlayLeft + $(window).scrollLeft() + "px"
      });

      closeLink.click(function() {
        $("#video-overlay-blocker").remove();
        $("#video-overlay").remove();
        return false;
      });

      contentDiv.flash({ 
          "src": playerPath,
          "movie": playerPath,
          "width": videoWidth,
          "height": videoHeight,
          "quality": "high",
          "wmode": "window",
          "allowfullscreen": "true",
          "pluginspage": "http://www.macromedia.com/go/getflashplayer",
          "type": "application/x-shockwave-flash",
          "allowscriptaccess": "always",
          "name": "flashvideo",
          flashvars: {
            videoPath: $(this).attr("href"),
            Width: videoWidth,
            Height: videoHeight,
            vColor: scrollbarColor,
            rollColor: "0xc43563",
            vidLoop: "false",
            AutoPlay: "true",
            autostart: "false"
          }
        });

      headerDiv.append(closeLink);
      overlayDiv.append(headerDiv);
      overlayDiv.append(contentDiv);
    	
      overlayDiv.show();
      return false;
    });
  });
}


