//   Yodel JS

var ie6 = false;
var $j = jQuery.noConflict();
var $ie6Boolean = $j.browser.msie && $j.browser.version=="6.0";
var recaptureKey;

$j(document).ready(function () {

	fnJsClass();
	fnCheckIe();
	fnResize();
	fnFormDefaultText();
	fnTabs();
	fnPrint();
	fnFlyouts();
	fnTicker();
	fnHomeSlides();
	fnEqualH($j(".pChk p"));
	fnEqualH($j(".ChkDivH"));
	fnShowHide();
	fnPanelToggle();
	fnScroller();
	fnSifrHover();
	fnVideoOverlay();
	fnToolTip();
	fnImageAlign();
	//fnSIFRfix();
	
    /* IE6 Functions */
    if (ie6) {
        fnBgFlicker();
    }
});





/**
* jqEM to detect font size changes
*/
//set the text size that the default body computes to
var fontSize = 13;

$j.jqem.bind(function(a,b,c) {
	fnResize();
});
	
function fnResize(){
	if( $j.jqem.current() == fontSize) {			
		$j('body, #main').addClass('enh');
	}else if($j.jqem.current() != fontSize){		
		$j('body, #main').removeClass('enh');
	};
};

function fnJsClass(){
	$j('body').addClass('js');
}

/**
 * Assign simple hide and show default value for inputs like search box
 */
function fnFormDefaultText() {
	
	$j(".formDefault").each(function(){
		var $label = $j(this).prev('label');
		var txt = $label.text();
		if($label.has('em').length>0){
			txt = $j('em',$label).text();
		}
		
		 if ($j(this).val() == "" || $j(this).val() == txt) {
			$j(this)[0].defaultValue = txt;
        	$j(this).val($j(this)[0].defaultValue);
     	}
		
	});
	
	$j(".formDefault").unbind("focus,blur").focus(function () {
		if ($j(this).val() == $j(this)[0].defaultValue) {
        	$j(this).val("").addClass('focus');
        }
	 }).blur(function() {
        if ($j(this).val() == "") {
        	$j(this).val($j(this)[0].defaultValue).removeClass('focus');
     	}
	 });
}

function fnCheckIe(){
	if($j('#ie6').length>0){ie6 = true;}
}

function fnBgFlicker(){
	try {document.execCommand("BackgroundImageCache", false, true);} catch(err){}
}


function fnTabs() {
	if($j('div.tabs')){
		$j('div.tabs').tabs({			
		});
		$j('div.tabs ul:first li:last').addClass('last');
		$j('div.tabs ul:first li:first').addClass('first');
	}
	
	if (typeof initTab != "undefined"){
		$j('div.tabs').tabs( "select" , initTab );
	}
	
}

function fnPrint(){
	
	$j('a.print').click(function(){
		window.print();	
		return false;
	});
	
}

function fnFlyouts()
{
    $j("#mNav1,#mNav2,#mNav3").each(function()
    {
        var oT = $j(this);
        var oTo = null;
        
        oT.mouseenter(function()
        {
            oTo = setTimeout(function()
            {
                oT.addClass('hover').children('.flyout').css('top', '45px');
            },
            50);
        }).mouseleave(function()
        {
            clearTimeout(oTo);
            oTo = setTimeout(function()
            {
                oT.removeClass('hover').children('.flyout').css('top', '-9999px');
            },
            50);
        });
    });
}

/* SIFR Initiation */

/* This section adds the ability to use :not in the selectors to filter out anything that shouldn't be replaced */
parseSelector.pseudoClasses = {
  'not': function(nodes, selector) {
    var result = [];
    each: for(var i = 0, node; i < nodes.length; i++) {
      node = nodes[i];
      var ignore = parseSelector(selector, node.parentNode);
      for(var j = 0; j < ignore.length; j++) {
        if(ignore[j] == node) continue each;
      }
      result.push(node);
    }
    return result;
  }
};

	var verlagBold = {src: '/yodelAssets/flash/verlag-bold.swf'};
	var verlagBlack = {src: '/yodelAssets/flash/verlag-black.swf'};
	var verlagBook = {src: '/yodelAssets/flash/verlag-book.swf'};
	
	// You probably want to switch this on, but read <http://wiki.novemberborn.net/sifr3/DetectingCSSLoad> first.
	sIFR.useStyleCheck = true;
	sIFR.activate();
	
	sIFR.isActive = true;
	
function fnReplace(){
	var bl18 = '.bl18';
	var bl18Link = '.bl18Link';
	var bl18LinkGreyBg = '.bl18LinkGreyBg';
	var bl24 = '.bl24';
	var bo17 = '.bo17';
	var bo17Link = '.bo17Link, .flyout h3';
	var bo17or = '.orange .bo17theme';
	var bo17ch = '.cherry .bo17theme';
	var bo33or = '.orange .bo33'; //telephone
	var bo33ch = '.cherry .bo33'; //telephone
	var bo20 = '.bo20';
	var bl16 = '.bl16'; //black and white for track and trace page
	var error404 = '.error404'; //404 Error heading
	
	sIFR.replace(verlagBlack, {
		selector: error404
		,wmode: 'transparent'
		,css:  {
			'.sIFR-root':{'color': '#99CC00'} 
			}
		,selectable: false
	});
	
	sIFR.replace(verlagBlack, {
		selector: bl18
		,wmode: 'transparent'
		,css:  {
			'.sIFR-root':{'color': '#99CC00', 'text-transform':'uppercase'}, 
			'em':{ 'font-style': 'normal', 'color': '#000000' }
			}
		,selectable: false
	});
	
	sIFR.replace(verlagBlack, {
		selector: bl24
		,wmode: 'transparent'
		,css:  {
			'.sIFR-root':{'color': '#99CC00', 'text-transform':'uppercase'}, 
			'em':{ 'font-style': 'normal', 'color': '#000000' }
			}
		,selectable: false
	});
	
	sIFR.replace(verlagBold, {
		selector: bo17
		,wmode: 'transparent'
		,css:  {
			'.sIFR-root':{'color': '#000000'}
			}
		,selectable: false
	});
	
	sIFR.replace(verlagBold, {
		selector: bo17or
		,wmode: 'transparent'
		,css:  {
			'.sIFR-root':{'color': '#ff5800'}
			}
		,selectable: false
	});
	
	sIFR.replace(verlagBold, {
		selector: bo17ch
		,wmode: 'transparent'
		,css:  {
			'.sIFR-root':{'color': '#c90044'}
			}
		,selectable: false
	});
	
	sIFR.replace(verlagBold, {
		selector: bo33or
		,wmode: 'transparent'
		,css:  {
			'.sIFR-root':{'color': '#ff5800'}
			}
		,selectable: false
	});
	
	sIFR.replace(verlagBold, {
		selector: bo33ch
		,wmode: 'transparent'
		,css:  {
			'.sIFR-root':{'color': '#c90044'}
			}
		,selectable: false
	});
	
	sIFR.replace(verlagBold, {
		selector: bo20
		,wmode: 'transparent'
		,css:  {
			'.sIFR-root':{'color': '#000000', 'text-transform':'uppercase'},
			'a:link': { 'color': '#000000','text-decoration':'none'}, 
			'a:hover': { 'text-decoration':'none', 'color' : '#99cc00'}
			}
		,selectable: false
	});
	
	sIFR.replace(verlagBlack, {
		selector: bl16
		,wmode: 'transparent'
		,css:  {
			'.sIFR-root':{'color': '#ffffff', 'text-transform':'uppercase'}, 
			'em':{ 'font-style': 'normal', 'color': '#000000' }
			}
		,selectable: false
	});
	
	/* Links */
	
	sIFR.replace(verlagBlack, {
		selector: bl18Link
		,wmode: 'transparent'
		,css:  {'.sIFR-root': {'color': '#99cc00', 'text-transform':'uppercase'},
				'em':{ 'font-style': 'normal', 'color': '#000000' },
				'a:link': { 'color': '#99cc00','text-decoration':'none'}, 
				'a:hover': { 'text-decoration':'none', 'color' : 'inherit'}
				}
		,selectable: true,
		fitExactly: true,
		tuneWidth: 30,
		tuneHeight: 3,
		offsetTop:3
		//forceWidth: true
		
	});
	
	sIFR.replace(verlagBlack, {
		selector: bl18LinkGreyBg
		,wmode: 'transparent'
		,css:  {'.sIFR-root': {'color': '#99cc00', 'text-transform':'uppercase'},
				'em':{ 'font-style': 'normal', 'color': '#000000' },
				'a:link': { 'color': '#99cc00','text-decoration':'none'}, 
				'a:hover': { 'text-decoration':'none', 'color' : 'inherit'}
				}
		,selectable: true,
		fitExactly: true,
		tuneWidth: 30,
		tuneHeight: 2,
		offsetTop:2
		//forceWidth: true
		
	});
	
	sIFR.replace(verlagBold, {
		selector: bo17Link
		,wmode: 'transparent'
		,css:  {'.sIFR-root': {'color': '#000000'}, 
				'a:link': { 'color': '#000000','text-decoration':'none'}, 
				'a:hover': { 'color':'#000000','text-decoration':'none'}
				}
		,selectable: true,
		fitExactly: true,
		tuneWidth: 30,
		tuneHeight: 2,
		offsetTop:2
		//forceWidth: true
		
	});
}

fnReplace();


function fnSifrHover() {

	$j($j('object.sIFR-flash')).live('mouseover mouseout', function(event) {
		if (event.type == 'mouseover') {
			$j(this).addClass('hover')
		} else {
			$j(this).removeClass('hover')
		}
	});// end of live
}





function fnTicker() {
	$j('#js-news').ticker(
		{
		speed: 0.10,
		titleText: 'Latest News',
		controls: false,
		debugMode: false
		}
	
	);
}
 

 
function fnHomeSlides(){
	$j('#homeSlides .slidesWrap')
	.after('<div id="slideControls"></div>')
	.cycle({ 
	    timeout: 5000,
	    speed: 1500,
    	delay: 6000,
    	pager:'#slideControls',
		pagerAnchorBuilder: function(index, oDom)
		{
			return '<a href="#"><span>'+(index+1)+'</span></a>';
		}
	    }
	);
}


function fnEqualH($collection) {
	
		var $intTall = 0;
		if($collection.length>0) {
 
			$j($collection).each(
				function() {
				var $thisHeight = $j(this).height();
				if($thisHeight > $intTall) {
				$intTall = $thisHeight;
				}// if
			}//function
		); //each
   
		$j($collection).height($intTall);
	
		$find = $collection.find('div.cHld');
				
		$find.height($intTall - 170); /* 170);  */

	//
	}//if
 
}

function fnShowHide() {
	
	$j(".accordion .reveal").hide();
	
	$j(".accordion h4 a").click(function() {
		
	    $j(".accordion h4 a.active").not(this).parent().next().slideToggle('fast');
	    $j(".accordion h4 a.active").not(this).removeClass("active");

	    $j(this).toggleClass("active");
	    $j(this).parent().next().slideToggle('fast',function()
	    {
            $j('.row:visible .recaptcha', $j(this)).each(function()
            {
                showRecaptcha($j(this).attr('id'));
            });
	    });
	    return false; 

	}
	);    

	$j(".accordion h4 a.active").parent().next().slideToggle('fast',function()
    {
            $j('.row:visible .recaptcha', $j(this)).each(function()
        {
            showRecaptcha($j(this).attr('id'));
        });
    });

}// //fnShowHide




function fnPanelToggle() {

	if($j('body.busTB').length>0) {
	
		$j('.panel').hover(
			function(){
			$j(this).addClass("panelHL");
			$j(this).children('div.topL,div.topR').addClass('panelHL');
			$j(this).children().children('div.botL,div.botR').addClass('childHL');
			},
			function(){
			$j(this).removeClass("panelHL");
			$j(this).children('div.topL,div.topR').removeClass('panelHL');
			$j(this).children().children('div.botL,div.botR').removeClass('childHL');
			}
		);//

	}
	
}

// Initiate scroller
function fnScroller(){ 

	var $scroll_1;
	var $scroll_2;
	var $scrollChild_1;
    var $scrollChild_2; 
	var $i;
	
	
	$j(".scroll-pane").each(function ($i) {
		var $NoUnits = $j('.scroll-pane').eq($i).find('.unit').length;   	
		var $Width = $j('.scroll-pane').eq($i).find('.unit').width();    	
		var $ScrollID = $i;  									        	
		
		var $Total = $Width * $NoUnits;										
		if($NoUnits >1) {													
			$j('.scroll-pane').eq($i).find('.wrpBX').width($Total);
			$j(this).jScrollPane();	
		}

	 });

}


function fnVideoOverlay() {
	
		$video = $j('a.video');
		
		if($video.length){
			$video.each(function(){
				
				$vars = 'flv=' +$j(this).attr('href');
				
				$j(this).fancybox({
					'padding'			: 0,
					'autoScale'			: false,
					'transitionIn'		: 'none',
					'transitionOut'		: 'none',
					'href'				: '/yodelAssets/flash/player.swf',
					'type'				: 'swf',
					'width'				: 500,
					'height'			: 330,
					'titleShow'			: false,
					'swf'				:{
					   	'wmode'				: 'transparent',
						'allowfullscreen'	: 'true',
						'flashvars'			: $vars
						}

				});
			});
		}	
	
}

//Wrapping the Recaptcha create method in a javascript function
function showRecaptcha(element) {
    Recaptcha.create(recaptureKey, element, {
        theme: "white"//,
        //callback: Recaptcha.focus_response_field
    });
}

function fnToolTip() {
	if($j('a.tTip').length>0) {
		$j('a[title].tTip').qtip(
								{ position: {
											corner: {
													target: 'topRight',
													tooltip: 'bottomLeft'
													},
											adjust: { x: -30, y: 5 } //x = hor y vertical
											},
								
								style: { 	name: 'green', 
											tip: true 
										}
								}
								);
		
	}//if

}

function fnPngFix(){
	//DD_belatedPNG.fix('#logo a img, #mNav a.mainLink, #f, .flyout .top, .flyout .mid, .flyout .bot');
}

function fnImageAlign(){
	var tallest = 0;
	$j('.sendingDelivery .panel img').each(function() {
		var $thisHeight = $j(this).height();
			if($thisHeight > tallest) {
				tallest = $thisHeight;
			}
		}
	);
	
	$j('.sendingDelivery .rowMidKeylines').css({'padding-bottom': tallest+'px', 'position' : 'relative'});
	
	fnEqualH($j('.sendingDelivery .panel'));
	
}
