/* Javascript stylesheet */
document.write("<link rel=\"StyleSheet\" href=\"/thunderbirdAssetsV2/css/thunderbirdJs.css\" type=\"text/css\" media=\"screen\" />");
// e3 javascript for Thunderbird

// when the document is loaded
$(document).ready(function() {

    fnLoadForm();
    //	fnLoadScreensaverForm();
    //fnScreensaverFormPopup()

    fnSifr();
    fnToggleVideo();
    fnHotspots();
    fnReplaceTwoColourText();
    fnSplashFlash();
    
    fnScrollPane();
        
    // if($("#mainNav ul").length>0){
    var tipText = '';
    $("#mainNav ul li span")
			.hover(function() {
			    // alert(tipText);
			    $(this).parents("li")
					.append('<div class="tipWrapper"><div class="tip">' + tipText + '<div class="clr"></div></div></div>').fadeIn("fast")
					.find("span:first").attr("title", "");
			    return false;
			}, function() {
			    $(".tip").remove();
			    $(this).attr("title", tipText);
			});
    // }


    /* lightbox */

    if ($(".galleryFb").length > 0) {
        $(".galleryFb:first div a").lightBox({
            imageLoading: '/thunderbirdAssetsV2/images/lightbox-ico-loading.gif',
            imageBtnClose: '/thunderbirdAssetsV2/images/close.gif',
            imageBtnPrev: '/thunderbirdAssetsV2/images/previous.gif',
            imageBtnNext: '/thunderbirdAssetsV2/images/next.gif',
            imageBlank: '/thunderbirdAssetsV2/thunderbirdAssets/images/lightbox-blank.gif',
            containerResizeSpeed: 350,
            containerBorderSize: 10,
            txtImage: 'Image'
        });
    }

    $(".home .titleH1RT").css("width", "142px")

    /*
    if ($(".insight .video .videoInner").length) {
    $(".insight .video .videoInner").flash({
    // test.swf is the flash document   
    swf: '/thunderbirdAssets/flash/video_player_01.swf?flv=video/14406_16x9_bb.flv',
    width: 520,
    height: 412
    });
    }
    */
});
function fnToggleVideo() {
    $("#ShowVideo").toggle(
      function() {
          $("#VideoDiv").toggleClass("show");
          $(this)
                .css('background', 'url("/thunderbirdAssetsV2/images/VideoStop.jpg") 0 0 no-repeat')
                .attr("title", "Click to hide video")
                .find("span:first").text("Hide Video");
      },
      function() {
          $("#VideoDiv").toggleClass("show");
          var rewrite = $('#flashDiv');
          $('#flashDiv').remove();
          $("#VideoDiv").prepend(rewrite);          
          $(this)
                .css('background', 'url("/thunderbirdAssetsV2/images/VideoPlay.jpg") 0 0 no-repeat')
                .attr("title", "Click to show video")
                .find("span:first").text("Show Video");                
                
      }
    );

}
function fnReplaceTwoColourText() {
	
    var swf1 = ["/thunderbirdAssetsV2/flash/European_text_500x50.swf",500,50]
    var swf2 = ["/thunderbirdAssetsV2/flash/European_text_310x50.swf",310,50]
    var swf3 = ["/thunderbirdAssetsV2/flash/European_text_310x50.swf",310,50]
    var swf4 = ["/thunderbirdAssetsV2/flash/European_text_right_500x50.swf",500,50]
    //var swf4 = ["/thunderbirdAssetsV2/flash/European_text_500x50.swf",500,50]
    var swf5 = ["/thunderbirdAssetsV2/flash/European_text_440x90.swf",440,90]
        
    var t1 = ['#frt1', swf1]
    var t2 = ['#frt2', swf2]    
    var t3 = ['#frt3', swf3]
    var t4 = ['#frt4', swf4]
    //var t2 = ['.FRTFeaturesHeadline', swf1]
    //var t3 = ['.PressHeading', swf1]
    //var t4 = ['#MainNewsDiv #MainHeadingDiv.Story h1', swf1]
    var t5 = ['h1.homePageText', swf5]
    
    var titles = [t1,t2,t4,t5];    
    
    for (var i=0; i < titles.length; i++) {
	    if ($(titles[i]).length > 0) {
	        if (flash.installed) {
	            //flash.insert(src, 440, 50, 6, "firstWord=Thunderbird&secondWord=clothing");
	            var firstWord = $(titles[i][0] + " span:first").text();
	            firstWord = $.trim(firstWord);
	            var secondWord = $(titles[i][0] + " span:nth-child(2)").text();
	            secondWord = $.trim(secondWord);
	            var flashVars = "firstWord=" + firstWord + "&secondWord=" + secondWord;
	            //alert("1:" + firstWord + "\n2" + secondWord + "\n" + flashVars);
	            $(titles[i][0]) .css({            	
	            	fontSize: '100%',
	            	lineHeight: '0',
	            	width: titles[i][1][1]
	        	});
	            $(titles[i][0]).addClass("cfx");
	            $(titles[i][0] + " span").addClass("hide");
	            //$(titles[i][0]).append('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + titles[i][1][1] + '" height="' + titles[i][1][2] + '" id="European_text_500x50" align="middle"><param name="allowScriptAccess" value="sameDomain" /><PARAM NAME=FlashVars VALUE="' + flashVars + '"><param name="movie" value="' + titles[i][1][0] + '" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#000000" /><embed src="' + titles[i][1][0] + '" quality="high" bgcolor="#000000" width="' + titles[i][1][1] + '" height="' + titles[i][1][2] + '" name="European_text_500x50" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" salign="T" FlashVars="' + flashVars + '" wmode="transparent" /></object>');
	            $(titles[i][0]).append('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + titles[i][1][1] + '" height="' + titles[i][1][2] + '" id="European_text_500x50" align="middle"><param name="wmode" value="transparent" /><param name="allowScriptAccess" value="sameDomain" /><PARAM NAME=FlashVars VALUE="' + flashVars + '"><param name="movie" value="' + titles[i][1][0] + '" /><param name="quality" value="high" /><param name="bgcolor" value="#000000" /><embed src="' + titles[i][1][0] + '" quality="high" bgcolor="#000000" width="' + titles[i][1][1] + '" height="' + titles[i][1][2] + '" name="European_text_500x50" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" FlashVars="' + flashVars + '" wmode="transparent" /></object>');
	
	            	            
	            
	            //$(".FRTFeaturesHeadline").append('<embed wmode="transparent" src="' + src1 + '" menu="false" width="' + width1 + '" height="' + height1 + '" scale="noscale" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" salign="T" FlashVars="' + flashVars + '"/>');
	            //alert("1:" + firstWord + "\n2" + secondWord);
	        }
	    }
    };
    
}
function fnSifr() {
    if (typeof sIFR == "function") {
    sIFR.replaceElement("#MainNewsDiv .Story h2", named({ sFlashSrc: "/thunderbirdAssetsV2/flash/sifr-impact.swf", sColor: "#ffffff", sCase: "upper", sWmode: "transparent"}));
    sIFR.replaceElement("#MainPressDiv h2", named({ sFlashSrc: "/thunderbirdAssetsV2/flash/sifr-impact.swf", sColor: "#7d91a1", sCase: "upper", sWmode: "transparent"}));
    sIFR.replaceElement("#StoryContainerDiv h3", named({ sFlashSrc: "/thunderbirdAssetsV2/flash/sifr-impact.swf", sColor: "#7d91a1", sCase: "upper", sWmode: "transparent"}));    
    }

}

var formPage = "";
var formPage2 = "";
var regForm = "hello";
var screensaverForm = "hello";

function fnLoadForm() {

    if ($('a.registerBtn').length) {


        formPage = $('a.registerBtn').attr('href');
        closeButton = '<div id="closeWrap"><a href="#" id="closeBtn">Close</a></div>';


/*
  AJL - This now appends the form holder with iframe with send to friend inside it, so the send
  to friend page can submit the form to itself. So i removed the load and just hard coded this markup to 
  run all the time.
*/


        $('body').append('<div id="formHolder">' + closeButton + '<iframe src="' + formPage + '" width="500" height="500" frameborder="0"><p><a href="' + formPage + '">Please follow this link to send the site to a friend.</a></p></iframe></div>');

        $('a.registerBtn').click(function() {
              $.blockUI({
                  message: $('#formHolder'),
                  css: {
					top: '50%',
					left: '50%',
					marginTop: '-300px',
					marginLeft: '-300px',
					width: '570px',
					height:'570px'
                  }
              });
              return false;
          });

          $('#closeBtn').click(function() {
              $.unblockUI();

              return false;
          });

        /*
        $('#formHolder').load(formPage + ' #regForm', '', function() {
            //reload the flash titles as the form needs it
            fnSifr();

            $('#formHolder').prepend(closeButton);

            $('a.registerBtn').click(function() {
                $.blockUI({
                    message: $('#formHolder'),
                    css: {
                        top: '0%',
                        width: '50%',
                        height:'90%'
                    }
                });
                return false;
            });

            $('#closeBtn').click(function() {
                $.unblockUI();

                return false;
            });

        });
        */

    } //end if
}

function fnLoadScreensaverForm() {


    if ($('a.screensaverBtn').length) {

        test = $('a.screensaverBtn').length

        //alert("hello " + test)


        formPage2 = $('a.screensaverBtn').attr('href');
        closeButton2 = '<div id="closeWrap2"><a href="#" id="closeBtn2">Close</a></div>';

        $('body').append('<div id="formHolder2"></div>');

        $('#formHolder2').load(formPage2 + ' #screensaverForm', '', function() {
            //reload the flash titles as the form needs it
            fnSifr();

            $('#formHolder2').prepend(closeButton2);



            $('a.screensaverBtn').click(function() {
                $.blockUI({ message: $('#formHolder2'), css: { top: '30%', width: 'auto'} });
                return false;
            });

            $('#closeBtn2').click(function() {
                $.unblockUI();

                return false;
            });

        });


    } //end if

}



function fnScreensaverFormPopup() {

    closeButton2 = '<div id="closeWrap2"><a href="#" id="closeBtn2">Close</a></div>';

    $('body').append('<div id="formHolder2"></div>');

    $('a.screensaverBtn').click(function() {

        formPage2 = $(this).attr('href');

        $('#formHolder2').load(formPage2 + ' #screensaverForm', '', function() {
            //reload the flash titles as the form needs it
            fnSifr();

            $('#formHolder2').prepend(closeButton2);

            $.blockUI({ message: $('#formHolder2'), css: { top: '30%', width: 'auto'} });
            $('#closeBtn2').click(function() {
                $.unblockUI({
                    onUnblock: function() { $('#closeBtn2').remove(); }
                });

                return false;
            });
        });

        return false;

    });

}

function fnHotspots() {
    if ($('#hotspots').length>0) {

        
        $('#hotspots li a').each(
			function(i) {
        //$('#hotspots').append('<a class="hs" id="hs' + i + '" href="#hsTxt' + i + '">' + (i + 1) + '</a>');
                $(this).addClass("hs").attr("id", "hs" + i);
                //$('#hotspots').append('<span class="hsText" id="hsTxt' + i + '">' + $(this).text() + '</span>');
                var title = $(this).attr("title");
                $(this).removeAttr("title");
                $(this).parent().append('<span class="hsText" id="hsTxt' + i + '">' + title + '</span>');
			}
		);

       // $('#hotspots ul').remove();

			$('#hotspots .hsText').each(
			function(i) {
			    var w = $(this).width();
			    //console.log($(this).width())
			    var position = $(this).parent().position();
			    var xPos = position.left;
			    var yPos = position.top;
			    //console.log($(this).attr("id")+" x:" + xPos + ", Y:" + yPos);
			    xPos = xPos - (w / 2);
			    yPos = yPos - 93;

			    if ((xPos + w) > 990) {
			        xPos = 990 - w;
			    }
			    else if (xPos < 0) {
			        xPos = 0
			    }
			    //console.log($(this).attr("id")+" is 2x:" + xPos + ", 2Y:" + yPos);
			    $(this).hide().css({ 'left': xPos, 'top': yPos });
			});

        $('#hotspots .hs').hover(
			function() {
			    $(this).addClass('hover');
			    //$(this).next('span.hsText').fadeIn();
			},
			function() {
			    $(this).removeClass('hover');
			    $(this).next('span.hsText').fadeOut();
			}
		);

		$('#hsTxt0').css("top", "20px");
		$('#hsTxt0').css("left", "168px");
		$('#hsTxt1').css("top", "100px");
		$('#hsTxt1').css("left", "105px");
		$('#hsTxt2').css("top", "85px");
		$('#hsTxt2').css("left", "110px");
		$('#hsTxt3').css("top", "0px");
		$('#hsTxt3').css("left", "168px");
		$('#hsTxt4').css("top", "0px");
		$('#hsTxt4').css("left", "170px");
		$('#hsTxt5').css("top", "50px");
		$('#hsTxt5').css("left", "300px");
        $('#hsTxt6').css("top", "-20px");
        $('#hsTxt6').css("left", "150px");
        $('#hsTxt7').css("left", "250px");
        $('#hsTxt7').css("top", "-150px");

    } //end if
}


/**************************************************************
Function:	fnBrowser
Purpose:	Check we're in a supported browser
Notes:		
**************************************************************/
// Global Variables
var iepc, firefox, ns7, ns8, mozilla, safari;
function fnBrowser() {
    var ua = navigator.userAgent.toLowerCase();

    iepc = (((ua.indexOf("msie 7") != -1) || (ua.indexOf("msie 6") != -1) || (ua.indexOf("msie 5.5") != -1)) && (ua.indexOf("windows") != -1) && (ua.indexOf("opera") == -1)) ? true : false;
    firefox = ua.indexOf("firefox") != -1 ? true : false; // pc or mac
    ns7 = ua.indexOf("netscape/7") != -1 ? true : false; // firefox mode
    ns8 = ua.indexOf("netscape/8") != -1 ? true : false; // firefox mode
    mozilla = (ua.indexOf("gecko") != -1) && (ua.indexOf("netscape") == -1) ? true : false;
    safari = ua.indexOf("safari") != -1 ? true : false;
    opera = ua.indexOf("opera/9") != -1 ? true : false;
    if (iepc || firefox || mozilla || safari || opera) {
        return true;
    } else {
        return false;
    }
}


// Flash detect. boolean true/false if installed and version number accessed via flash.version
var flash = new Object();
flash.installed = false;

if (navigator.plugins && navigator.plugins.length) {
    for (x = 0; x < navigator.plugins.length; x++) {

        if (navigator.plugins[x].name.indexOf('Shockwave Flash') != -1) {
            flash.installed = true;
            flash.version = eval(navigator.plugins[x].description.split('Shockwave Flash ')[1].split('.')[0]);
            break;
        }
    }
}
else if (window.ActiveXObject) {
    for (x = 2; x <= 20; x++) {
        try {
            oFlash = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + x + "');");
            if (oFlash) {
                flash.installed = true;
                flash.version = x;
            }
        }
        catch (e) { }
    }
}

/********************************************************************************
 
Name:     			Flash embed
Description:  		Writeout flash code. Fixes Eolas update for IE as well.
@param swf  		String - path to swf file
@param width     	Number - value of movie width
@param height    	Number - value of movie height
@param noFlashID	String - ID of a div to show if flash not installed or wrong version
@param minVersion	Number - version of flash required for this movie
*********************************************************************************/
 
flash.insert = function(swf,width,height,noFlashID,minVersion) {
	alert('flash.insert');
	swf = swf.replace(/\./g,"%2E") // encode periods as %2e	
	
	if(!minVersion){minVersion = 6;}		 // default flash required version is 6.
	
 	if(flash.installed && (flash.version >= minVersion)) {
 		flashObject = '<embed src="'+swf+'" menu="false" quality="high" width="'+width+'px" height="'+height+'px" type="application/x-shockwave-flash" salign="T" pluginspage="http://www.macromedia.com/go/getflashplayer"/>'
		document.write(flashObject);
		
		if (noFlashID) {
			fnHideItem(noFlashID);	// no flash content should be hidden by the JS stylesheet, but in case its not, hide it now.
		}
	} else {
		if (noFlashID) {
			fnShowItem(noFlashID);	// show no flash content.
		}
	} 
}
function fnHide(id) {
    if (!document.getElementById(id)) { return false; };
    var oTarg = document.getElementById(id).style;
    oTarg.position = "absolute";
    oTarg.top = "-5000px";
    oTarg.left = "0";
}

function fnShow(id) {
    if (!document.getElementById(id)) { return false; };
    var oTarg = document.getElementById(id).style;
    oTarg.position = "static";
    oTarg.top = "";
    oTarg.left = "";
}



function fnFav(div) {
    if ($.browser.msie && ($.browser.version >= 6 && $.browser.version < 7)) {

        div.prepend('<a href="#" class="fav">Add to favourites</a> ');

        $('a.fav').click(function() {
            var url = document.URL;
            var title = document.title;
            window.external.AddFavorite(url, title);
            return false;
        });
    }

}

function fnPrint(div) {

    div.prepend('<a href="#" class="print">Print this page</a>');

    $('a.print').click(
		function() {
		    window.print();
		    return false;
		});
}


function fnPageTools() {

    var div = $('#pageTools');

    //if the pagetools div exists
    if (div.length) {
        fnFav(div);
        fnPrint(div);
    }
}



function fnSplashFlash(){
	
	if($('#MainSplashDiv').length > 0){
		//alert("dsfsd");
		if( $('#MainBGDiv').attr("class") != '' &&  $('#MainBGDiv').attr("class") != null){
			
			var lang = $('#MainBGDiv').attr("class").substring(4);				
			var swf= "";
			if(lang == "EN"){
				swf = "/thunderbirdAssetsV2/flash/splash.swf";				
			}	
			else{
				swf = "/thunderbirdAssetsV2/flash/splash"+ lang +".swf";	
			}	
			
			
			$('#MainSplashDiv #MainFlashDiv')			
				.find("img").addClass("hide").end()
				.html('<embed src="' + swf + '" menu="false" width="990" height="336" scale="noscale" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" salign="T" wmode="transparent" />')
			}
			
		}
		
		
		
		
	
}


function fnScrollPane() {

	if($('#MainStory').length > 0){
		
		// setup custom scrolling pane - see jScrollPane.js for options
		$('#MainStory')			
			.jScrollPane({
				showArrows:true,
				scrollbarWidth:15,
				arrowSize:16,
				dragMinHeight:31,			
				dragMaxHeight:31			
			});
		//$(".jScrollPaneContainer").attr("tabIndex","0")
				
	}
	
}