﻿
/*
 * Superfish v1.4.1 - jQuery menu widget
 * Copyright (c) 2008 Joel Birch
 *
 * Dual licensed under the MIT and GPL licenses:
 * 	http://www.opensource.org/licenses/mit-license.php
 * 	http://www.gnu.org/licenses/gpl.html
 *
 * CHANGELOG: http://users.tpg.com.au/j_birch/plugins/superfish/changelog.txt
 */

(function($){
	$.superfish = {};
	$.superfish.o = [];
	$.superfish.op = {};
	$.superfish.defaults = {
		hoverClass	: 'sfHover',
		pathClass	: 'overideThisToUse',
		delay		: 800,
		animation	: {opacity:'show'},
		speed		: 1,
		oldJquery	: false, /* set to true if using jQuery version below 1.2 */
		disableHI	: false, /* set to true to disable hoverIntent usage */
		// callback functions:
		onInit		: function(){},
		onBeforeShow: function(){},
		onShow		: function(){}, /* note this name changed ('onshow' to 'onShow') from version 1.4 onward */
		onHide		: function(){}
	};
	$.fn.superfish = function(op){
		var bcClass = 'sfbreadcrumb',
			over = function(){
				var $$ = $(this), menu = getMenu($$);
				getOpts(menu,true);
				clearTimeout(menu.sfTimer);
				$$.showSuperfishUl().siblings().hideSuperfishUl();
			},
			out = function(){
				var $$ = $(this), menu = getMenu($$);
				var o = getOpts(menu,true);
				clearTimeout(menu.sfTimer);
				if ( !$$.is('.'+bcClass) ) {
					menu.sfTimer=setTimeout(function(){
						$$.hideSuperfishUl();
						if (o.$path.length){over.call(o.$path);}
					},o.delay);
				}		
			},
			getMenu = function($el){ return $el.parents('ul.superfish:first')[0]; },
			getOpts = function(el,menuFound){ el = menuFound ? el : getMenu(el); return $.superfish.op = $.superfish.o[el.serial]; },
			hasUl = function(){ return $.superfish.op.oldJquery ? 'li[ul]' : 'li:has(ul)'; };

		return this.each(function() {
			var s = this.serial = $.superfish.o.length;
			var o = $.extend({},$.superfish.defaults,op);
			o.$path = $('li.'+o.pathClass,this).each(function(){
				$(this).addClass(o.hoverClass+' '+bcClass)
					.filter(hasUl()).removeClass(o.pathClass);
			});
			$.superfish.o[s] = $.superfish.op = o;
			
			$(hasUl(),this)[($.fn.hoverIntent && !o.disableHI) ? 'hoverIntent' : 'hover'](over,out)
			.not('.'+bcClass)
				.hideSuperfishUl();
			
			var $a = $('a',this);
			$a.each(function(i){
				var $li = $a.eq(i).parents('li');
				$a.eq(i).focus(function(){over.call($li);}).blur(function(){out.call($li);});
			});
			
			o.onInit.call(this);
			
		}).addClass('superfish');
	};
	
	$.fn.extend({
		hideSuperfishUl : function(){
			var o = $.superfish.op,
				$ul = $('li.'+o.hoverClass,this).add(this).removeClass(o.hoverClass)
					.find('>ul').hide().css('visibility','hidden');
			o.onHide.call($ul);
			return this;
		},
		showSuperfishUl : function(){
			var o = $.superfish.op,
				$ul = this.addClass(o.hoverClass)
					.find('>ul:hidden').css('visibility','visible');
			o.onBeforeShow.call($ul);
			$ul.animate(o.animation,o.speed,function(){ o.onShow.call(this); });
			return this;
		}
	});
	
	$(window).unload(function(){
		$('ul.superfish').each(function(){
			$('li',this).unbind('mouseover','mouseout','mouseenter','mouseleave');
		});
	});
})(jQuery);


/* Copyright (c) 2006 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $LastChangedDate: 2007-02-18 22:09:54 -0600 (Sun, 18 Feb 2007) $
 * $Rev: 1379 $
 */

/**
 * The bgiframe is chainable and applies the iframe hack to get 
 * around zIndex issues in IE6. It will only apply itself in IE 
 * and adds a class to the iframe called 'bgiframe'.
 * 
 * It does take borders into consideration but all values
 * need to be in pixels and the element needs to have
 * position relative or absolute.
 *
 * NOTICE: This plugin uses CSS expersions in order to work
 * with an element's borders, height and with and can result in poor 
 * performance when used on an element that changes properties 
 * like size and position a lot. Two of these expressions can be
 * removed if border doesn't matter and performance does.
 * See lines 39 and 40 below and set top: 0 and left: 0
 * instead of their current values.
 *
 * @example $('div').bgiframe();
 * @before <div><p>Paragraph</p></div>
 * @result <div><iframe class="bgiframe".../><p>Paragraph</p></div>
 *
 * @name bgiframe
 * @type jQuery
 * @cat Plugins/bgiframe
 * @author Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
 */
jQuery.fn.bgIframe = jQuery.fn.bgiframe = function() {
	// This is only for IE6
	if ( !(jQuery.browser.msie && typeof XMLHttpRequest == 'function') ) return this;
	var html = '<iframe class="bgiframe" src="javascript:false;document.write(\'\');" tabindex="-1" '
	 					+'style="display:block; position:absolute; '
						+'top: expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)  || 0) * -1) + \'px\'); '
						+'left:expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth) || 0) * -1) + \'px\'); ' 
						+'z-index:-1; filter:Alpha(Opacity=\'0\'); '
						+'width:expression(this.parentNode.offsetWidth + \'px\'); '
						+'height:expression(this.parentNode.offsetHeight + \'px\')"/>';
	return this.each(function() {
		if ( !jQuery('iframe.bgiframe', this)[0] )
			this.insertBefore( document.createElement(html), this.firstChild );
	});
};
var App = {
	Cache:{
		data:{},
		get:function(k){return this.data[k];},
		set:function (k,v){this.data[k]=v;}
	},
	Page:{
		register:function (){
			App.Cache.set("origContentHeight",$("#cw-page").height());
			App.Page.resize();
			$(window).resize(function(){
			  App.Page.resize();
			});
		},
		resize:function (){
			if($("#cw-bottomcontainer").length >0){
				var docHeight = $(window).height();
				var origContentHeight = App.Cache.get("origContentHeight");
				if(docHeight>origContentHeight){
					$("#cw-bottomcontainer").css("min-height",(docHeight-origContentHeight)+"px");
				}
			}
		},
		serviceMenuFunc:function(){
		    var serviceMenu = $("#cw-topnav-upper-new");
		    $(serviceMenu).find(".mmSiteList").each(function(){
		        $(this).find(".GroupHeading:not(:first)").each(function(){
		            $(this).addClass("marginT-s");
		        });
		    });
		    $(serviceMenu).find("> li:has(div[class*=Menu])").hover(function(){
		            $(serviceMenu).find("> div[class*=Menu]:visible").hide();
                    $(this).addClass("hoverMenu").find("> div[class*=Menu]").delay(400).show(1);
                },function(){
                    if($(this).find("> div[class*=Menu]:visible").length != 0){
                        $(this).removeClass("hoverMenu").find("> div[class*=Menu]").hide();
                    }
                    else {
                        $(this).removeClass("hoverMenu").find("> div[class*=Menu]").clearQueue();
                    }
            });
		},
        topNavFunc:function(){
            var topNav = $("#cw-topnav-lower-new");
            $(".mmFlexMenu:has(.mmColumnTwo)").addClass("mmTopStoryMenu");
            $(topNav).find("> li:has(div[class*=Menu])").hover(function(){
                    $(topNav).find("> div[class*=Menu]:visible").hide();
                    $(this).addClass("hoverMenu").find("> div[class*=Menu]").delay(400).show(1);
                },function(){
                    if($(this).find("> div[class*=Menu]:visible").length != 0){
                        $(this).removeClass("hoverMenu").find("> div[class*=Menu]").hide();
                    }
                    else {
                        $(this).removeClass("hoverMenu").find("> div[class*=Menu]").clearQueue();
                    }
            });
            //$(topNav).find("li > div[class*=Menu]").each(function(){
                //$(this).css("height",$(this).height()).find(".innerMenu").css("height",$(this).height());
            //});
        },
        megaTabFunc:function(){
            $("#MegaTab div.mmtab").each(function(i){
                $(this).addClass("mmtab"+i);
            });
            var megaTab = $("#MegaTab");
            $(megaTab).find("div[class*=mmtab]:has(div.mmColLeft) > div").each(function(){
                $(this).mouseenter(function(){
                    $(megaTab).find(".mmtab:visible").hide();
                    $(megaTab).find(".mmColRight").hide();
                    $(megaTab).find(".chosen").removeClass("chosen").css("z-index","");
                    $(this).parent().find(".mmColRight").show();
                    $(this).parent().addClass("chosen").css("z-index","4");
                    $("#mmDefaultPicture").hide();
                    var thisClass = $(this).parent().attr("class").split(" chosen")[0];
                    var thisNow = $(megaTab).find("div[class*="+ thisClass +"]")[1];
                    $(thisNow).show();
                });
            });
            $(megaTab).mouseleave(function(){
                $(megaTab).find(".mmtab:visible").hide();
                $(megaTab).find(".mmColRight").hide();
                $(megaTab).find(".chosen").removeClass("chosen").css("z-index","");
                $("#mmDefaultPicture").show().find(".mmColRight").show();
            });
        },
        rteTableStyles:function(){
            $("#mainColumn div.textContent table").addClass("cowi-rteTable")
                .find("tr:first:has(.cowi-rteCustom-Heading)").addClass("thead").end()
                    .find("td:has(.cowi-rteCustom-Heading)").addClass("thead").end()
                        .find("tr:odd").addClass("odd");
        },
        rteFixes:function(){
            $("#mainColumn div.textContent:not(:has(div.ms-formfieldcontainer)) a:visible").prepend("<span class='inlineBg'>&nbsp;</span>");
        },
        printPage:function(){
          $("#PrintHyperLink").live("click",function(e){
            e.preventDefault();
            var loc = location.href + "?printing";
            window.open(loc,"","scrollbars=yes,width=820,height=540");
          })
        },
        searchBtnHover:function(){
            $("#contentWrapper span.searchBtnWrapper").hover(function(){
                $(this).addClass("searchBtnHover");
            },function(){
                $(this).removeClass("searchBtnHover");
            });
        },
        leftMenu:function(){
            $("ul#leftMenu").superfish({
                animation : { opacity:"show", height:"show" }
	        })
	            .find(">li:has(ul)").mouseover(function(){
	                    $("ul", this).bgIframe({opacity:false});
	                })
	                    .find("a").focus(function(){
	                            $("ul", $("#leftMenu>li:has(ul)")).bgIframe({opacity:false});
	                        });
        },
        hideEmpties:function(){
            var editMode = $("html").hasClass("editMode");
            if($("#topnavContainer").children().length < 1){
                $("#topnavContainer").parent().css("display","none");
            }
            $("div.col,div.col-m").each(function(){
                if(editMode !== true){
                    if($(this).children().length == 0){
                        $(this).css("display","none");
                    }
                } else {
                    if($(this).children().length == 0){
                        $(this).css("display","block");
                    }
                }
            });
            if($(".imageTextContent div[id*=RestrictedRichHtmlField]").children().length > 1){
                if(editMode != true){
                    $(".imageTextContent div[id*=RestrictedRichHtmlField]").addClass("imageTextInner");
                } else {
                    $(".imageTextContent div[id*=RestrictedRichHtmlField]").removeClass("imageTextInner");
                }
            }
            if(($("#cw-siteactions td").children().length < 1) && ($("#cw-user").children().length < 1)){
                $("div.siteActions").css("display","none");
            }
        },
        init:function(){
            if($("#cowiPopup").length != 0){
                $("html").addClass("printing");
            }
            if($("#cw-consoles span:has(table)").length != 0){
                $("html").addClass("editMode");
            }
            App.Page.printPage();
            App.Page.leftMenu();
            App.Page.rteFixes();
            App.Page.searchBtnHover();
            App.Page.hideEmpties();
            App.Page.register();            
        }
	} // @Page
};

function openwindow(url,x,y)
{
	window.open(url,"mywindow","menubar=0,resizable=1,width="+x+",height="+y);
}

function log(o){
	if(window.console){
		window.console.log(o)
	} else {
		alert(o)
	}
}

var loc = location.href;
if(loc.indexOf("?printing") != -1){
    $("html").addClass("printing");
    $("document").ready(function(){
        window.print();
    });
}
if(loc.indexOf("?Publication") != -1){
    $("html").addClass("publicationPopup");
}
function printThisPage() {
//	content = document.getElementById("printableArea").innerHTML;

//	printWindow=window.open('/_layouts/printcowipage.aspx','Print',
//	'width=820,height=540'
//	+',menubar=0'
//	+',toolbar=0'
//	+',status=0'
//	+',scrollbars=1'
//	+',resizable=0');
//   
//	top.printWindow.document.close()
    var loc = location.href + "?printing";
    window.open(loc,"","width=820,height=540");
}

function openThis(target){
    var john = $("#"+target).val();
    if(john.indexOf("default") == -1){
        location.assign(john);
    }
}

$("document").ready(function(){
    App.Page.init();
});
$(window).load(function(){
    App.Page.topNavFunc();
    App.Page.serviceMenuFunc();
    App.Page.megaTabFunc();
});