/* JS */
function RoundCorners()
{
    if (jQuery.browser.msie)
    {
        topcorners = '<div class="corners top"><span class="l"></span><span class="m"></span><span class="r"></span></div>';
        bottomcorners = '<div class="corners bottom"><span class="l"></span><span class="m"></span><span class="r"></span></div>';
        jQuery('.topNavigation_container, .site_container, .footer_container').addClass("ie").css("position","relative").append(topcorners + bottomcorners);
    }
}

function documentLoaded()
{
    RoundCorners();
    
    /*
    //make visible any content with the "content-hide" inside the products markets widget
    //needs to be done so that jScrollPane doesn't break
    jQuery('.content_hide','#Product_Markets_Widget_Tabs_Content').show();
    
        //perform binding of scrollbars to all objects with the class .scroll-pane
        jQuery('.scroll-pane').jScrollPane({ scrollbarWidth:9 });
    
    //re-hide any content with the "content-hide" inside the products markets widget
    jQuery('.content_hide','#Product_Markets_Widget_Tabs_Content').hide();
    */
    
    //add the down arrow to all newly created JS scrollbars
    jQuery('.jScrollPaneDrag').append("<div class='jScrollPaneDrag_Custom_Arrow_Bottom'></div>");
    
    //set the products markets widget "markets" tab content to be hidden if its not currently active
    //jQuery('#ctl00_cph_left_panel_uc_products_market_widget_Product_News_Widget_market_tab_content').hide();

    //init news scroller
    jQuery('#news_scroller').jNewsTicker({ articles:".article", delay:5000 });
    
    // Try and find any tables to sort - find all elements with the class ".sortable-table"
    // Sort Default first column
    jQuery(".sortable-table").tablesorter({sortList: [[0,0]]});
    
    MakeTooltips();
    
    document.aspnetForm.action = window.location.href;
    
    
    /* Font adjust */
	jQuery('.site_container .fontsize-adjust span').click(function(){
	    _fontsize = "auto";
	    _classname = jQuery(this).attr('class');
	    switch (_classname){
	        case "m":
	            _fontsize = 1.2 + "em";
	        break;
	        case "l":
	            _fontsize = 1.4 + "em";
	        break;
	    }
	    
	    /* Set body font-size */
	    if (_fontsize != "auto")
	    {
	        jQuery('.site_container  #inner').css({ "font-size": _fontsize , "line-height": _fontsize });
	    }
	    else
	    {
	        jQuery('.site_container  #inner').attr("style", "z-index:2");
	    }
	    	    
	    /* Try save in cookie */
	    jQuery.cookie('font-size', _fontsize);
	    
	}).css('cursor','pointer');
	
	if (jQuery.cookie('font-size') != null)
	{
	    if (jQuery.cookie('font-size') != "auto")
	    {
	        jQuery('.site_container #inner').css({ "font-size": jQuery.cookie('font-size'), "line-height" : jQuery.cookie('font-size') });
	    }
	}
 
}

function MakeTooltips(){

    jQuery('.makeTooltip').each(function(){
        
        jQuery(this)
            .mousemove(function(e){
                jQuery("#tooltipHelperParent")
                    .css("top", e.pageY - getScrollXY()[1])
                    .css("left", e.pageX + 20)
            })
            .hover(
                function(){
                    content = jQuery(jQuery(this).attr("rel"), jQuery(this)).html();
                    jQuery("#tooltipHelper")
                        .find(".tooltipContent").html(content)
                        .end().parent().show();
                },
                function(){
                    jQuery("#tooltipHelper").parent().hide();
                }
            );   
    });
    
    // Generate Helper
    var markup = '<div id="tooltipHelperParent">\
                    <div id="tooltipHelper" class="tooltip">\
                        <img class="ear" src="/assets/visual/tooltipEar.jpg" alt="" width="5" height="4" />\
                        <div class="tooltipContent"></div>\
                    </div>\
                </div>';
    
    jQuery('body').append(markup);
}

function Product_News_Widget( showTab, showContentIndex )
{
    //set the tab_off class to all siblings of the tab clicked
    jQuery( showTab ).siblings().addClass("tab_off")
    //remove the tab_off class off the clicked tab
    jQuery( showTab ).removeClass("tab_off");
    
    //get all divs in the the "content" area and hide them
    jQuery( "#Product_Markets_Widget_Tabs_Content" ).children().filter("div").hide();
    //set the div at pos X in jQuery array to visible
    jQuery( "#Product_Markets_Widget_Tabs_Content" ).children().filter("div:eq("+showContentIndex+")").show();//.find("ul").show();
    jQuery( "#Product_Markets_Widget_Tabs_Content" ).children().filter("div.content_hide").removeClass("content_hide");//.find("ul").show();
}

function changeCategoryPaging( clickedObj )
{
    //remove the active class of all a tags in the paging area
    jQuery( clickedObj ).parent().parent().children().find("a").removeClass("active")
    //add active class to clicked page number
    jQuery( clickedObj ).addClass("active")
    
    //calculate new scrollTop pos - 400 = (scroll area / items in one "page") * (page number clicked -1)
    scrollTopPos = 400 * (parseInt(jQuery( clickedObj ).html())-1);
    //scroll teh scroller
    /*jQuery('#category_scroller:first').scrollTo(scrollTopPos);*/
    jQuery('#category_scroller:first').scrollTop( scrollTopPos);
    
    jQuery('#tooltipHelperParent').hide();
}

function toggleExtraCaseStudies( clickObj )
{
    jQuery(clickObj).fadeOut("fast");
    jQuery(clickObj).parent().prev().slideToggle("medium",function(){
        if ( jQuery(this).css("display") == "block" )
        {
            jQuery(clickObj).children().filter("span").html("less case studies");
            jQuery(clickObj).children().filter("img").attr("src","/assets/visual/arrow_toggle_up.jpg");
        }
        else
        {
            jQuery(clickObj).children().filter("span").html("more case studies");
            jQuery(clickObj).children().filter("img").attr("src","/assets/visual/arrow_toggle_down.jpg");
        }
        jQuery(clickObj).fadeIn("fast");
    })
}

function toggleLanguageSelector( clickObj )
{
    moveToLeft = jQuery( clickObj ).offset().left;
    clientWidth = jQuery('body').innerWidth();
    extraOffset = (clientWidth - 750) / 2;
    jQuery('.langSelector').css("left", (moveToLeft-extraOffset+18) ).toggle();
}

function resetForm( containerObj )
{
    jQuery( containerObj ).find("input").val("");
}

function goToPage( gotoURL )
{
    window.location.href = gotoURL;
}


function loadGMap(latitude,longitute) 
{
	if (GBrowserIsCompatible()) 
	{
		var map = new GMap2(document.getElementById("googleMap"));
		map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());

		var center = new GLatLng(latitude, longitute);
        map.setCenter(center, 13);
        
        // Set up our GMarkerOptions object
        markerOptions = { };
        
        var marker = new GMarker(center, markerOptions);
		
	    GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml("Lindapter International<br />Lindsay House<br />Brackenbeck Road<br />Bradford<br />West Yorkshire<br />BD7 2NF, England")
        });
        
        map.addOverlay(marker);
	 }
}

function Show3DRender(p_AssemblyID)
{
    var renderWindow = window.open('/assets/pages/3D_Render.aspx?assembly='+p_AssemblyID,'newWin','scrollbars=auto,resizable=1,width=620,height=530') 
    renderWindow.focus();
}


function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [ scrOfX, scrOfY ];
}

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { 
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString();
        }

        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { 
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};