/* Main JS functions */

// load JS-only styles
loadStylesheet( '/syn/assets/style/has_js.css', 'screen' )

$(document).ready(function() {

	// initialize page
    pageInit();
	
	$(window).resize(function(){

	pageInit();
	
	});
    

    // init map
    MAP.init();

    // gallery lightbox
    $('a.mediaLightbox').lightbox();

	// in page navigation - product
    $('#inPageNav').children('li').children('ul').children('li').children('a').click(function() {
        // get currently selected content and hide
        $('#inPageContentWrapper').children('div.selectedContent').removeClass('selectedContent').hide();

        // get ID of clicked link and show new content
        $('#' + $(this).attr('href').split('#')[1]).addClass('selectedContent').show();

        return false;
    });

    // tabbed list navigation
    $('#tabbedListNav').children('li').children('a').click(function() {
        // get currently selected content and hide
        $('#tabbedListContentWrapper').children('div.selectedContent').removeClass('selectedContent').hide();

        // get ID of clicked link and show new content
        $('#' + $(this).attr('href').split('#')[1]).addClass('selectedContent').show();

        // update nav
        $('#tabbedListNav').children('li.selected').removeClass('selected');
        $(this).parent().addClass('selected');
        
        return false;
    });

	// glossary nav
	$('a.glossaryItem').click(function() {
		// get letter name
		var glossarySection = "#glossary-" + $(this).attr('href').split('#')[1];
		
		// remove current class from displayed section
		$('dl.current').removeClass('current');
		$('ul.glossaryNav').children('li').children('a.current').removeClass('current');
		
		// add class to section to be displayed
		$(glossarySection).addClass('current');
		$(this).addClass('current')
		
		return false;
	});

	// max-width on lhs promo module images
	$('div.secondaryContent').find('.leftPromo').find('img').each(function() {
		// check image width
		if (this.width > 180) {
			this.width = 180;
		}
	});
	
	// max-width on rhs promo module images
	$('div.tertiaryContent').find('.rightPromo').find('img').each(function() {
		// check image width
		if (this.width > 245) {
			this.width = 245;
		}
	});

	// max-width on rhs module images
	$('div.tertiaryContent').children('div.module').children('img').each(function() {
		// check image width
		if (this.width > 245) {
			this.width = 245;
		}
	});
	
	$('div.tertiaryContent').find('div.moduleContent').find('img').each(function() {
		// check image width
		if (this.width > 225) {
			this.width = 225;
		}
	});
	
	$('div.tertiaryContent').find('div.contentImageFull').find('img').each(function() {
		// check image width
		if (this.width > 225) {
			this.width = 225;
		}
	});
	
	$('div.moduleWrapper').find('div.module').find('img').each(function() {
		// check image width
		if (this.width > 225) {
			this.width = 225;
		}
	});
	
	$('div.moduleWrapper').find('div.contentImageFull').find('img').each(function() {
		// check image width
		if (this.width > 225) {
			this.width = 225;
		}
	});
	
	$("a.rssFeedOpenLink").click(function (event) {
		event.preventDefault();
    	$(this).parent().next('div').toggle();
		if ($(this).parent().hasClass('active')) {
			$(this).parent().removeClass('active')
		}
		else {
			$(this).parent().addClass('active')
		}
  	});
	$("a.rssFeedCloseLink").click(function (event) {
		event.preventDefault();
		$(this).parent().parent().toggle();
		if ($(this).parent().parent().prev('p').hasClass('active')) {
			$(this).parent().parent().prev('p').removeClass('active')
		}
		else {
			$(this).parent().parent().prev('p').addClass('active')
		}
  	});

	// open in new window
	$('a[@rel$="external"]').click(function() {
		this.target = "_blank";
	}); 
	
});



function pageInit(){

	var myWidth = 0, myHeight = 0;
	
	if (typeof (window.innerWidth) == 'number') {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	
	// position the country selector link based on the position of the bottom header and it's height / allows for dynamic nav height
    var headerBottomPos = findPos(document.getElementById('headerBottom'))[1];
    var headerBottomHeight = $('#headerBottom').height();
    var countryLinkHeight = $('#syngentaWorldwide').height() + 14;
	
	
	var pageWidth = $('#container').width();
	var pageXPos = findPos(document.getElementById('container'))[0];
	
    document.getElementById('countrySelect').style.top = ((headerBottomPos + headerBottomHeight) - countryLinkHeight) + "px";
	document.getElementById('countrySelect').style.left = ((pageXPos+pageWidth)-255) + "px";
    
    //if there are any back to top links show them if the page is 2 screens in length or more
    var backToTopLinks = $('p.backToTop');
    if(backToTopLinks.length > 0){
         
        var scrOfX = 0, scrOfY = 0;
        
        var pageHeight = $('#container').height();

        if(pageHeight > (myHeight * 2)){
        
            for (i = 0; i < (backToTopLinks.length); i++){
                ($(backToTopLinks)[i]).style.display = 'block';
            }
            
        }
        
    }

}

// determine the recurrsive position of a DOM element
function findPos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft
    curtop = obj.offsetTop
    while (obj = obj.offsetParent) {
      curleft += obj.offsetLeft
      curtop += obj.offsetTop
    }
  }
  return [curleft,curtop];
}
