/**
 * !! DO NOT change !!
 *  This is required as we
 *  need to wait for ALL images
 *  to load before we process them
 */
$(window).load(
    function() {
        imgResizer($('#mainBlock .mediaImg'), 28);
        iframeResizer($('#mainBlock iframe'), 28);
    }
);

$("document").ready(function()
{
    $('.newsletter-form-button-border').bind('click', function() {
        var a  = $(this).find('a');
        var id = $(a).attr('href');
        $(id).submit();
        return false;
    });


    mainStage();
    articleEditor();
    // footer equal column heights
    $(".heightfix").equalHeights();
    // footerDisplay();
    mainArticleWidth();
    gradient();

    $("a[href^='http']").click(function(){window.open(this.href); return false;});
});


// JavaScript Document

// Form fields gradiants
resizerImgCacheWidth     = new Array();
resizerImgCacheHeight    = new Array();
resizerIframeCacheWidth  = new Array();
resizerIframeCacheHeight = new Array();

function gradient(){
 
    $('.newsletter-form-border').gradient({
        from:      '021922',
        to:        '007aa3',
        direction: 'vertical'
    });
 
    $('.newsletter-form-button-border').gradient({
        from:      '36b2dc',
        to:        '035471',
        direction: 'horizontal'
    }); 
 
    $('.button-border').gradient({
        from:      '36b2dc',
        to:        '035471',
        direction: 'horizontal'
    });

	
    footerDisplay();
	
}

function mainStage()
{
	siteWidth = $("#background-Bottom").width();	
	if (siteWidth < 1311) 
	{
	newSiteWidth = siteWidth - 40;
	$("#wrapper").width(newSiteWidth);
	$(".miniBrandMenu").width(newSiteWidth);
	$("#footer").width(newSiteWidth);
	}
	else
	{
		$("#wrapper").width(1310);
		$(".miniBrandMenu").width(1310);
		$("#footer").width(1311);
	}
}

function mainArticleWidth() 
{
	
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    //																								 //
    // This function sorts out the prodcuts and article container width in the two column layout     //
    //																								 //
    ///////////////////////////////////////////////////////////////////////////////////////////////////
	
    totalMainArticleWidth = $('#wrapper').innerWidth();
	
    // full screen width or bigger
    if (totalMainArticleWidth == 1311 || totalMainArticleWidth > 1311) {
		
        $("#articleContainer").width(775);
		$("#articleContainer").css("min-height", "860px");
        $("#lastestContainer").css("float", "right");
        $("#lastestContainer").css("width", "524px");
		$("#subLatestContainer").css("float", "right");
        $("#subLatestContainer").css("width", "524px");
		
    }
	
    //
    if (totalMainArticleWidth < 1311 && totalMainArticleWidth > 1053)
    {
        newMainWidth = (totalMainArticleWidth - 535);
		
        $("#articleContainer").width(newMainWidth);
		$("#articleContainer").css("min-height", "860px");
        $("#lastestContainer").css("float", "right");
        $("#lastestContainer").css("width", "524px");
		$("#subLatestContainer").css("float", "right");
        $("#subLatestContainer").css("width", "524px");
    }
	
	
    if (totalMainArticleWidth <= 1053)
    {
        $("#articleContainer").css("width", "100%");
		$("#articleContainer").css("min-height", "10px");
        $("#lastestContainer").css("float", "left");
        $("#lastestContainer").css("width", "100%");
		$("#subLatestContainer").css("float", "left");
        $("#subLatestContainer").css("width", "100%");
		
		
    }
	
}

function footerDisplay() 
{
    ////////////////////////////////////////////////////////////////
    //
    // This function sorts out the footer for different size devices
    //
    ////////////////////////////////////////////////////////////////
	
    totalFooterWidth = $('#footer').innerWidth();
	
	
    // full screen width or bigger
    if (totalFooterWidth == 1311 || totalFooterWidth > 1311) {
		
        newsletterWidth = 520;
        contactWidth = 244;
        websitesWidth = 524;
		
        $(".newsletterContainer").width(520);
        $(".contactDetailsContainer").width(244);
        $("#websitesContainer").width(524);
        $(".contactDetailsContainer").css("float", "left");
        $(".contactDetailsContainer").css("margin-left", "11px");
        $(".contactDetailsContainer").css("display", "block");
        $("#newsletterForm").css("margin-right", "8px");
		
        $(".newsletterParagraph").css("display", "block");
        $("#newsletterForm").css("float", "right");
        $("#newsletterForm").css("margin-left", "0");
		
        $(".contactDetailsFullWidth").css("display", "none");
	
    }
	
    // less that full screen but more the half a screen
    if (totalFooterWidth < 1311 && totalFooterWidth > 1053)
    {
		
		
        //alert('total = ' + totalFooterWidth);
		
		// Because of the liquid design 
		// flash replace doesn't render hidden div layer first time round.
		// the fix for this is to re round the setup script for the footer to solve the title rendering issue when the layer it shown.
		
		sIFR.replace(tradegothic, {
  		selector:"#footer h2",
  		offsetTop : "6",
   		tuneHeight : "6",
  		css: [
      		'.sIFR-root { position:relative; margin:0; padding:0; text-align: center; background:none; font-size:16px; color: #ffffff; display:inline; }'
  		]
  		,wmode: 'transparent'
	
		});
		
		
        alterWidth = totalFooterWidth - $(".newsletterContainer").width();
        $(".contactDetailsContainer").css("display", "none");
        $(".contactDetailsFullWidth").css("display", "block");
        $("#websitesContainer").css("width", alterWidth-9);
        $("#newsletterForm").css("margin-right", "8");
		
        $(".newsletterParagraph").css("display", "block");
        $("#newsletterForm").css("float", "right");
        $("#newsletterForm").css("margin-left", "0");
		
    }


    if (totalFooterWidth <= 1053 && totalFooterWidth > 773)
    {
        $(".newsletterContainer").width(520);
        $("#newsletterForm").css("margin-right", "8px");
        $(".contactDetailsContainer").css("display", "block");
        $(".contactDetailsFullWidth").css("display", "none");
        $(".contactDetailsContainer").css("float", "right");
        $(".contactDetailsContainer").css("margin-left", "0");
	
        $(".contactDetailsContainer").css("width", (totalFooterWidth-529));
        $(".contactDetailsContainer").css("min-width", "244px");
        $("#websitesContainer").css("width", "100%");
		
        $(".newsletterParagraph").css("display", "block");
        $("#newsletterForm").css("float", "right");
        $("#newsletterForm").css("margin-left", "0");
    }

    if (totalFooterWidth <= 773)
    {
		
        formWidth = totalFooterWidth - 520;
        if(formWidth > 0 )
        {
            formNewWidth = $("#newsletterForm").width() + formWidth;
            $("#newsletterForm").css("margin-right", formWidth);
        }
        $(".newsletterContainer").css("width", "100%");
        $(".contactDetailsContainer").css("width", "100%");
		
        $(".newsletterParagraph").css("display", "block");
        $("#newsletterForm").css("float", "right");
        $("#newsletterForm").css("margin-left", "0");
        $(".contactDetailsFullWidth").css("display", "none");
    }
	
    if (totalFooterWidth <= 520)
    {
		

        $(".newsletterParagraph").css("display", "none");
        $("#newsletterForm").css("float", "left");
        $("#newsletterForm").css("margin-left", "13px");
        $(".contactDetailsFullWidth").css("display", "none");
    }

}
function iframeResizer(item, widthOffset) {

    item.each(function(elementId) {
        index = parseInt(elementId);
        resizerIframeCacheWidth[index]  = resizerIframeCacheWidth[index] || $(this).width() + widthOffset;
        resizerIframeCacheHeight[index] = resizerIframeCacheHeight[index] || $(this).height();

        //now calculatute the true dimensions of the video and container
        var totalContainerWidth = $("#articleContainer").width();
        var itemWidth = $(this).width() + widthOffset;
        var newWidth  = (itemWidth - (itemWidth - totalContainerWidth)) - widthOffset;

        //work out scale between height and width
        var aspect    = 0;
        var newHeight = 0;
        if(resizerIframeCacheWidth[index] > resizerIframeCacheHeight[index]) {
            aspect    = resizerIframeCacheWidth[index] / newWidth;
            newHeight = resizerIframeCacheHeight[index] / aspect;
        }

        //only apply change if the new dimensions are lesser than original
        if(newWidth < resizerIframeCacheWidth[index]) {
            $(this).width(newWidth);
            $(this).height(newHeight);
        } else {
            $(this).width(resizerIframeCacheWidth[index]);
            $(this).height(resizerIframeCacheHeight[index]);
        }
    });
}

function imgResizer(item, widthOffset) {

    
    item.each(function(elementId) {
        
        index = parseInt(elementId);
        resizerImgCacheWidth[index]  = resizerImgCacheWidth[index] || $(this).width() + widthOffset;
        resizerImgCacheHeight[index] = resizerImgCacheHeight[index] || $(this).height();

        //now calculatute the true dimensions of the video and container
        var totalContainerWidth = $("#articleContainer").width();
        var itemWidth = $(this).width() + widthOffset;
        var newWidth  = (itemWidth - (itemWidth - totalContainerWidth)) - widthOffset;

        //work out scale between height and width
        var aspect    = 0;
        var newHeight = 0;
        if(resizerImgCacheWidth[index] > resizerImgCacheHeight[index]) {
            aspect    = resizerImgCacheWidth[index] / newWidth;
            newHeight = resizerImgCacheHeight[index] / aspect;
        }

        //only apply change if the new dimensions are lesser than original
        if(newWidth < resizerImgCacheWidth[index]) {
            $(this).width(newWidth);
            $(this).height(newHeight);
        } else {
            $(this).width(resizerImgCacheWidth[index]);
            $(this).height(resizerImgCacheHeight[index]);
        }
    });


}

function articleEditor() {
    if($('input[name="docAttach"]:checked').length > 0) {
        hideArticleEditors();
        showArticleEditors($('input[name="docAttach"]:checked'));
    }

    $('input[name="docAttach"]').bind('click', function() {
        hideArticleEditors();
        showArticleEditors($(this));
    });
}

function hideArticleEditors() {
    $('#document').hide();
    $('#shortDesc').hide();
    $('#detailedDesc').hide();
}

function showArticleEditors(radio) {
    if($(radio).val() == 1) {
        $('#document').show();
        $('#shortDesc').show();
    } else {
        $('#shortDesc').show();
        $('#detailedDesc').show();
    }
}

$(window).resize(function() {

    mainStage();
    footerDisplay();
    mainArticleWidth();

    imgResizer($('#mainBlock .mediaImg'), 28);
    iframeResizer($('#mainBlock iframe'), 28);
});


