// set variable to use to detect certain browsers
var ie6 = ($.browser.msie && /MSIE 6\.0/i.test(window.navigator.userAgent) && !/MSIE 7\.0/i.test(window.navigator.userAgent));
var ie = ($.browser.msie);
var mozilla = ($.browser.mozilla);
var safari = ($.browser.safari);
var opera = ($.browser.opera);
var browsers = (safari) || (opera);

//General Styling Changes 
$(document).ready(function(){
    $("#footer li:first-child").addClass("first");
    $("#footer li:last-child").addClass("last");
    
	if ($("#newsletter input#newsletter-email-address-subscribe").length > 0) {
		$("#newsletter input#newsletter-email-address-subscribe").placehold({placeholdValue: "ENTER EMAIL ADDRESS"}); //default text on search field
	}
	
    $("input#edit-search_block_form_keys").placehold({placeholdValue: "SEARCH"}); //default text on search field
		$("#eventsearch-searchform input#edit-key").placehold({placeholdValue: "Search asia:nz online"}); //default text on search field

		$("input#name").placehold({placeholdValue: "Name"}); //default text on search field
		$("input#trziu-trziu").placehold({placeholdValue: "Email"}); //default text on search field

    $("#main-content .event-calendar .list-view .day:first-child").css("background","0").css("padding-top","0");
    $("#main-content .block-l2_main_story .content .lvl-2-front-page-node:nth-child(odd)").addClass("right");
    $("#main-content .right-column .right-node:last-child").addClass("last");
    $("#main-content .country-extra .resources .country-resource:last-child").addClass("last");   
});

// Primary Menu
$(document).ready(function(){
    $("#primary-navigation ul.menu li").hover(
        function(){ $("ul", this).slideDown("medium"); }, 
        function() {} 
    );
    $("#primary-navigation ul.menu li").hover(
      function(){
        $(this).addClass("sfHover");
      },
      function(){
        $(this).removeClass("sfHover");
      }
    );    
    
    $("#primary-navigation ul li a").focus(
      function(){
        $(this).parents(".expanded").addClass("sfHover");       
      }    
    ); 
    $("#primary-navigation ul li a").blur(
      function(){
        $(this).parents(".expanded").removeClass("sfHover");
      }    
    );     
    $("#primary-navigation li.expanded.active").addClass("active-parent");
    //fix IE6 Select issues with dropdown
    if(ie6){
        $("#primary-navigation li").bgiframe();
        $("#main-content div.event .teaser").bgiframe();        
    }   
});

//Secondary Menu
$(document).ready(function(){
	//add class to parent item of current page
	//enables styling of parent of a hidden 4th level page
	$("li.collapsed.active a").addClass("active-parent");
})

// Popups
$(document).ready(function(){

    // this function creates popups that close when the mouse leaves either the link or popup.
    // the arguments f_over_p and f_out_p are optional callback functions.

    $.fn.sticky_popups = function(link_selector, popup_selector,
                                  f_over_p, f_out_p){
        var mouse_over_popup = false;
        $(popup_selector).each(function(){
            $(this).hide().siblings(link_selector).hover(
                function(){
                    // fires on mouse over link
                    $(popup_selector).hide();
                    $(this).siblings(popup_selector).show();
                    $(this).siblings(popup_selector).hover(
                        function(){
                            // fires on mouse over popup
                            mouse_over_popup = true;
                            if (f_over_p != null) f_over_p.call(this);
                        },
                        function(){
                            // fires on mouse out from popup
                            mouse_over_popup = false;
                            $(this).hide();
                            if (f_out_p != null) f_out_p.call(this);
                        }
                    );
                },
                function(){
                    // fires on mouse out from link
                    var tmp = this;
                    setTimeout(function() {
                        if (!mouse_over_popup) {
                            $(tmp).siblings(popup_selector).hide();
                        }
                    },400);
                }
            );
        });
    }

    //  Event Calendar Popup

    $(".teaser").css("position","absolute");  
    $(".teaser").css("width","200px"); 
    $("#main-content div.event-calendar .link").css("cursor","pointer");
    $(".teaser-wrapper").sticky_popups(".teaser-link a", ".teaser-wrapper", null, null);
    $(".month-view .teaser-wrapper").each(function(){
      $(".teaser-link > h2 > a").focus(function(){
          $(".teaser-wrapper").hide();
          $(this).parent().parent().siblings(".teaser-wrapper").show();
      });
      $(".teaser-link > h2 > a").hover(
        function(){
          var nid = $(this).parent().parent().attr("id").substring(1);
          $(".e" + nid).parent().addClass("active");
          $("#n" + nid).parent().parent().addClass("active");
        },
        function(){
          var nid = $(this).parent().parent().attr("id").substring(1);
          $(".e" + nid).parent().removeClass("active");
          $("#n" + nid).parent().parent().removeClass("active");
        }
      );
      $(this).hover(
        function(){
          var classes = $(this).attr("class").split(' ');
          for ( var i in classes ) {
            var tempClass = classes[i];
            if (tempClass.substring(0, 2) == 'tw') {
              var nid = tempClass.substring(2);
              if (nid == parseInt(nid)) {
                $(".e" + nid).parent().addClass("active");
                $("#n" + nid).parent().parent().addClass("active");
              }
            }
          }
        },
        function(){
          var classes = $(this).attr("class").split(' ');
          for ( var i in classes ) {
            var tempClass = classes[i];
            if (tempClass.substring(0, 2) == 'tw') {
              var nid = tempClass.substring(2);
              if (nid == parseInt(nid)) {
                $(".e" + nid).parent().removeClass("active");
                $("#n" + nid).parent().parent().removeClass("active");
              }
            }
          }
        }
      );
    });

    //  Country Map Popup

    $("#main-content #country-map li span.popup").removeClass("hide-popup");
    //hides link text
    $("#main-content #country-map li span.country-text").each(function(){
        $(this).css("position","absolute").css("left","-9999px").css("top","-9999px");
    });
    $("#main-content #country-map li span.popup").sticky_popups(
        "a",
        "span.show-popup",
        function() { $(this).siblings("a").addClass("hover"); },
        function() { $(this).siblings("a").removeClass("hover"); }
    );
    $("#main-content #country-map span.show-popup").each(function(){
        $("a").focus(function(){
            $("span.popup").hide();
            $(this).siblings("span.show-popup").show();
        });
    }); 
});

// Fudge a link to the country page on the popups 'read more' text
$(document).ready(function(){ 
    var countries = ['bangladesh', 'bhutan', 'brunei-darussalam', 
                     'burma', 'cambodia', 'china', 
                     'taiwan', 'hong-kong-sar', 'india', 
                     'indonesia', 'japan', 'laos', 
                     'malaysia', 'mongolia', 'nepal', 
                     'pakistan', 'philippines', 'singapore', 
                     'south-korea', 'sri-lanka', 'thailand', 
                     'timor-leste', 'vietnam'];
    $.each(countries, function(index, country) {    
	    //alert("Hello");
	    $("span." + country + " span.link").click(function(){    
	        window.location = "/the-asian-region/" + country;
	        return false;    
	    }); 
	    //if(ie6){
	        $("span." + country + " span.link").addClass("hover");
	    //}	       
    });
});

// Country Sidebar Rollover
$(document).ready(function(){
    var countries = ['bangladeshhh', 'bhutan', 'brunei-darussalam', 
                     'burma', 'cambodia', 'china', 
                     'taiwan', 'hong-kong-sar', 'india', 
                     'indonesia', 'japan', 'laos', 
                     'malaysia', 'mongolia', 'nepal', 
                     'pakistan', 'philippines', 'singapore', 
                     'south-korea', 'sri-lanka', 'thailand', 
                     'timor-leste', 'vietnam'];
    $.each(countries, function(index, country) {
        function addHover() {
            $("#main-content #country-map li." + country + " a").addClass(country + "-hover");
        }
        function removeHover() {
            $("#main-content #country-map li." + country + " a").removeClass(country + "-hover");
        }
        $("#secondary-content ." + country).hover(addHover, removeHover);
        $("#secondary-content ." + country + " a").focus(addHover);
        $("#secondary-content ." + country + " a").blur(removeHover);
    })
});




//  Country Map Popup for IE
$(document).ready(function(){

    // A thousand curses upon you, Internet Explorer.
    // Z-index issues mean we have to move the popups out of
    // their <li>.

    if(ie){
        $.fn.sticky_popups_ie = function(){

            var countries = ['bangladesh', 'bhutan', 'brunei-darussalam', 
                             'burma', 'cambodia', 'china', 
                             'taiwan', 'hong-kong-sar', 'india', 
                             'indonesia', 'japan', 'laos', 
                             'malaysia', 'mongolia', 'nepal', 
                             'pakistan', 'philippines', 'singapore', 
                             'south-korea', 'sri-lanka', 'thailand', 
                             'timor-leste', 'vietnam'];

            var mouse_over_popup = false;

            $.each(countries, (function(index, country){
                var all_popups     = "#main-content span.popup";
                var link_selector  = "#main-content #country-map li." + country + " a";
                var popup_selector = "#main-content span." + country + ".show-popup";
                $(link_selector).hover(
                    function(){
                        // fires on mouse over link
                        $(all_popups).hide();
                        $(popup_selector).show();
                        $(popup_selector).hover(
                            function(){
                                // fires on mouse over popup
                                mouse_over_popup = true;
                                $(link_selector).addClass("hover");
                            },
                            function(){
                                // fires on mouse out from popup
                                mouse_over_popup = false;
                                $(this).hide();
                                $(link_selector).removeClass("hover");
                            }
                        );
                    },
                    function(){
                        // fires on mouse out from link
                        setTimeout(function() {
                            if (!mouse_over_popup) {
                                $(popup_selector).hide();
                            }
                        },400);
                    }
                );
            }));
        }

        //move popups out of ul#country-map to avoid z-index problems
        $("#main-content #country-map li span.popup").each(function(){
            $(this).remove();
            $(this).insertAfter("#main-content #country-map");      
        });
        //$("#country-map").parent().find("span.popup:nth-child(1)").addClass("first");
        //$("#country-map").parent().find("span.popup:last-child").addClass("last");    
        //reveal and hide popups
        $("#main-content span.popup").removeClass("hide-popup");
        $("#main-content #country-map").sticky_popups_ie();
    }
});


// Events calendar popup
$(document).ready(function() {
	$('.event_title_container').click(function(e) {
		$('.event_title_container').removeClass('clicked');
		$(this).addClass('clicked');
		
		e.stopPropagation();
		$('.event_popup').hide();
		$(this).find('.event_popup').show();
	});
	
	$("#events_calendar").click(function() {
		$('.event_popup').hide();
		$('.event_title_container').removeClass('clicked');
	});

	$("#page-wrapper").click(function() {
		$('.event_popup').hide();
		$('.event_title_container').removeClass('clicked');
	});

});


//Fixing Layout for Safari 
$(document).ready(function(){ 
    if(safari){
        $("#secondary-content .block .month-view caption").css("margin-left","0");    
        $("#secondary-content .block .month-view table").css("margin-left","-10px");
        //$("#main-content .main-node").css("min-height","616px");
    }
 
});

// Rotating sponsor images
$(document).ready(function() {
           
  $('#sponsors a').click(function(){
    window.open(this.href);
    return false;
  });

  $('#sponsor_left li:first').addClass('active');
  $('#sponsor_right li:first').addClass('active');

  rotate = function() {
    $('#sponsor_left li').removeClass('active');
    $active_left.addClass('active');
    $('#sponsor_right li').removeClass('active');
    $active_right.addClass('active');
  };

  
  rotateImages = function(){
      play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
        if ($('#sponsor_left li.active').hasClass('static')) {
          $active_left = $('#sponsor_left li.active'); //Move to the next paging
        } else {
          $active_left = $('#sponsor_left li.active').next(); //Move to the next paging
          if ( $active_left.length === 0) { //If paging reaches the end...
            $active_left = $('#sponsor_left li:first'); //go back to first
          }
        }
        $active_right = $('#sponsor_right li.active').next(); //Move to the next paging
        if ( $active_right.length === 0) { //If paging reaches the end...
          $active_right = $('#sponsor_right li:first'); //go back to first
        }
        rotate(); //Trigger the paging and slider function
      }, 5000); //Timer speed in milliseconds (7 seconds)
  };

  rotateImages();

});


$(document).ready(function() {
  // $('.page-node #main-content .node-content .content img').each(function(index){
  //   var title = $(this).attr('alt');
  //   if (title != '') {
  //     $(this).wrap('<div class="inline-image titleized" />');
  //     $('.inline-image').append('<p>'+title+'</p>');
  //   } else {
  //     $(this).wrap('<div class="inline-image" />');
  //   }
  // });
});

	$(document).ready(function() {

		$("a.iframe").fancybox({
			'width'			: 441,
			'height'		: 366,
			'autoScale'		: false,
			'overlayOpacity'	: 0.5,
			'overlayColor'		:'#000',
			'transitionIn'		: 'fade',
			'transitionOut'		: 'none',
			'type'			: 'iframe'
		});
	});
