// JavaScript Document



brandworkzUtils = {
	
	init : function() {
		//Apply hover class to all li's in #topNavigation
		brandworkzUtils.applyHoverClass('#topNavigation li');
	
	},
	
	tabSwitcher : function(src, siblings){	
		// Remove active class from all tabs
		jQuery(siblings).removeClass('active');
		
		//Add active class to clicked tab
		jQuery(src).addClass('active');
	},
	
	contentSwitcher : function(target, siblings) {
		//Remove active class from all sibling content containers
		jQuery(siblings).removeClass('active');
		
		//Get target details....
		$target = '#' + jQuery(target).attr('href');
		
		//Show target content container
		jQuery($target).addClass('active');	
	},
	
	applyHoverClass : function(src) {
		jQuery(src).mouseover(function() {
			jQuery(this).addClass('hover');
		}).mouseout(function() {
			jQuery(this).removeClass('hover');
		});
	}, 
	
	toggleZindexClass : function(src) {
		jQuery(src).mouseover(function() {
			jQuery(this).css('z-index', 1000);
		}).mouseout(function() {
			jQuery(this).css('z-index', 1);
		});
	}, 
	
	applyActiveClass : function(target, activeClass) {
		jQuery(target).addClass(activeClass);
	}
}

utilitiesDrop ={
	init : function() {
		brandworkzUtils.applyHoverClass('li.utils_dropdown');
	}
}


topNavigation2D = {
	
	//Function to setup top featured(section) navigation
	init : function() {
		//brandworkzUtils.applyHoverClass('#topNavigation li');
		topNavigation2D.shiftDropdownRight('#topNavigation li.level_one');
		topNavigation2D.detectSpaceRequired();
	}, 
	
	//Function to shift top navigation dropdown to the left, if it's right edge is beyond the space available
	shiftDropdownRight : function(src) {	
		var $brandworkzContainer;
		var $dropdown;
		
		$(src).hover(function () {
			//get the child div.level_two_container for the li that was hovered over
			$dropdown = jQuery(this).children('div.level_two_container');
			if($dropdown.length){
				//Calculate offset right position of div.level_two_container
				var $dropdownOffset = jQuery($dropdown).offset({border: true}).left;
				var $dropdownWidth = jQuery($dropdown).width();
				var $dropdownOffsetRight = $dropdownOffset + $dropdownWidth;
	
				//Calculate the offset right position of the masthead
				$brandworkzContainer = jQuery('#brandworkzContainer');
				var $brandworkzContainerOffset = jQuery($brandworkzContainer).offset({border: true}).left;
				var $brandworkzContainerWidth = jQuery($brandworkzContainer).width();
				var $brandworkzContainerOffsetRight = $brandworkzContainerOffset + $brandworkzContainerWidth;
				
				//Calculate the offset right position of the body
				var $bodyOffset = document.body.offsetLeft; //Do not use "jQuery('body').offset({border: false}).left" as this causes a bug where elements in the side navigation to shift down
				var $bodyWidth = jQuery('body').width();	
				var $bodyOffsetRight = $bodyOffset + $bodyWidth;
				
				//If offset right edge of dropdown is beyond offset right edge of the body apply negative left positioning
				if($dropdownOffsetRight > $bodyWidth) {	
					//$(this).addClass('hover_right');
					var offsetDifference = $dropdownOffsetRight - $bodyWidth;
					$($dropdown).css('left', '-' + eval(offsetDifference + 10)  + 'px')			
					
					//Check the new offset left of the dropdown is not beyond the offset ;eft of the body
					var $newDropdownLeft = jQuery($dropdown).offset({border: true}).left;
					
					if($newDropdownLeft < 0) {
						//Calculate difference to determine how far dropdown can be shifted left and apply to dropdown
						var $allowedShift = eval(offsetDifference + 10) + $newDropdownLeft - 10;
						
						$($dropdown).css('left', '-' + $allowedShift  + 'px')
					}					
				}
			}

		}, function (){	});	
	}, 
	
	//Function to reset left positioning of dropdown when user resizes browser window
	resetDropdown : function() {
		jQuery('#topNavigation ul.two_dimensional div.level_two_container').css('left', '0px');
	},
	
	//Function to calculate the min-width required to display the first level of navigation 
	//without it wrapping and applying that width to the body if less than normal min-width
	detectSpaceRequired : function() {
		
		//Designed minimum width of Brandworkz Container
		var brandworkzMinWidth = '960px';
		
		//Variable to store total width of all li's. Initialise with the left css property of #topNavigation
		var $ulWidth = parseInt(jQuery('#topNavigation').css('left'));
		
		//Loop over each level 1 li and add width to variable 
		jQuery('#topNavigation li.level_one').each(function() {
			//Get width of li plus li's margin-right css property
			var $thisLiWidth = jQuery(this).outerWidth() + parseInt(jQuery(this).css('margin-right'));
			
			//Add li's width to $ulWidth
			$ulWidth = $ulWidth + $thisLiWidth;											
		});
		
		//Apply a minimum width to #masthead so that the top navigation level 1 links do not wrap, as long that width is not less than agreed Brandworkz minium width
		if(parseInt($ulWidth) < parseInt(brandworkzMinWidth)) {
			$ulWidth = brandworkzMinWidth;
		}

		//Apply a min-width to #masthead for standards compliant browsers
		jQuery('#masthead').css('min-width', $ulWidth);	
		
	}
}

topNavigationFlyout = {
	
	//Function to setup section navigation
	init : function() {
	
		brandworkzUtils.applyHoverClass('#topNavigation li');
		//topNavigationFlyout.shiftDropdownRight('#topNavigation li.level_one');
	}, 
	
	//Function to shift section navigation dropdown to the right
	shiftDropdownRight : function(src) {
				
		var $brandworkzContainer;
		var $dropdown;
		
		$(src).hover(function () {
			//get the child div.level_two_container for the li that was hovered over
			$dropdown = jQuery(this).children('div');
			//Calculate offset right position of div.level_two_container
			var $dropdownOffset = jQuery($dropdown).offset({border: true}).left;
			var $dropdownWidth = jQuery($dropdown).width();
			var $dropdownOffsetRight = $dropdownOffset + $dropdownWidth;

			//Calculate the offset right position of the masthead
			$brandworkzContainer = jQuery('#brandworkzContainer');
			var $brandworkzContainerOffset = jQuery($brandworkzContainer).offset({border: true}).left;
			var $brandworkzContainerWidth = jQuery($brandworkzContainer).width();
			var $brandworkzContainerOffsetRight = $brandworkzContainerOffset + $brandworkzContainerWidth;
			
			//Calculate the offset right position of the body
			var $bodyOffset = jQuery('body').offset({border: true}).left;
			var $bodyWidth = jQuery('body').width();
			var $bodyOffsetRight = $bodyOffset + $bodyWidth;
			
			//If offset right edge of dropdown is beyond offset right edge of the body apply negative left positioning
			if($dropdownOffsetRight > $bodyWidth) {	
				//$(this).addClass('hover_right');
				var offsetDifference = $dropdownOffsetRight - $bodyWidth;
				$($dropdown).css('left', '-' + eval(offsetDifference + 10)  + 'px')
				//alert("dropdown: " + $dropdownOffsetRight + " brandworkz container: " + $brandworkzContainerOffsetRight + " body: " + $bodyWidth);
			
				
				//Check the new offset left of the dropdown is not beyond the offset ;eft of the body
				var $newDropdownLeft = jQuery($dropdown).offset({border: true}).left;
				
				if($newDropdownLeft < 0) {
					//Calculate difference to determine how far dropdown can be shifted left and apply to dropdown
					var $allowedShift = eval(offsetDifference + 10) + $newDropdownLeft - 10;
					
					$($dropdown).css('left', '-' + $allowedShift  + 'px')
				}
				
				//alert("New dropdown left: " + $newDropdownLeft  + " , Allowed shift: " + $allowedShift + " Original shift: " + eval(offsetDifference + 10));
			
			} else {	
				//$(this).addClass('hover');
			}

			//var navContainerOffset = navContainer.offset({border: true}).left + navContainer.width();
		
		}, function (){	});
		
		
	}, 
	
	//Function to reset left positioning of dropdown when user resizes browser window
	resetDropdown : function() {
		jQuery('#topNavigation ul.two_dimensional div.level_two_container').css('left', '0px');
	}
}
/*
actionbar = {
	//Function to setup actionbar dropdowns
	init : function() {
		brandworkzUtils.applyHoverClass('#action_dropdowns li');
	}
}
*/
tabbedContent = {
	
	//Function to setup section navigation
	init : function() {
		
		//Add listener for click event to tab
		jQuery('#tabs a').click(function() {
			
			ourBrand.tabSwitcher(this);
			ourBrand.contentSwitcher(this);
			return false;
		});
	},
	
	tabSwitcher : function(src) {

		//Remove active from all tabs
		jQuery('#tabs li').removeClass('active');

		//Add active class to parent li element of clicked tab
		jQuery(src).parent().addClass('active');
	},

	contentSwitcher : function(src, siblings) {
		
		//Remove tab_content_active class from all tab_content divs and add class tab_content_hidden to hide
		jQuery('.tab_content').removeClass('tab_content_active');
		jQuery('.tab_content').addClass('tab_content_hidden');
		
		//Get href for clicked tab to identify target tab_content panel
		var $target = jQuery(src).attr('href');
		
		//Add tab_content_active class to target tab_content div
		jQuery($target).removeClass('tab_content_hidden');
		jQuery($target).addClass('tab_content_active');
		
	}
	
}

showArtwork = {
	
	//Expand / close all panels based previous state
	panelsInit : function() {
		
		//Get panels to open from argument
		var panelsToOpen = showArtwork.panelsInit.arguments;
		
		//Loop over each panels passed as arguments
		jQuery(panelsToOpen).each(function() {
			
			//Replace 'state' with '#' to create selector
			var thisId = this.replace('state', '#');
			
			//Open panel
			jQuery(thisId + ' .panel_content').slideDown('medium');
			jQuery(thisId + ' .panel_content').addClass('open');
			
			//add open class to clicked panel
			jQuery(thisId + ' .panel_heading').addClass('panel_heading_open');
			
			
		});
		
	},
	
	//Apply show / hide behaviour to click event
	panelsClick : function() {
		
		//var $panelOpen = true;
		
		//Add behaviour to expand all link
		jQuery('#expandAllPanelsLink').click(function() {
			jQuery('.panel_content').show();
			jQuery('.panel_heading').addClass('panel_heading_open');
			$.cookie("statepanelAdvancedFileInfo", "expand", { path: '/', expires: 10 })
			$.cookie("statepanelTagsAndMeta", "expand", { path: '/', expires: 10 })
			$.cookie("statepanelDownload", "expand", { path: '/', expires: 10 })			
			//$panelOpen = true;
		});
		
		//Add behaviour to close all panels link
		jQuery('#closeAllPanelsLink').click(function() {
			jQuery('.panel_content').hide();
			jQuery('.panel_heading').removeClass('panel_heading_open');
			$.cookie("statepanelAdvancedFileInfo", "collapse", { path: '/', expires: 10 })
			$.cookie("statepanelTagsAndMeta", "collapse", { path: '/', expires: 10 })
			$.cookie("statepanelDownload", "collapse", { path: '/', expires: 10 })			
			//$panelOpen = false;
		});
		
		//Panel show / hide (on click)
		jQuery('.panel_heading').click(function() {			
			if(jQuery(this).siblings().hasClass('open')) {
				//is open so close panel
				jQuery(this).siblings().slideUp('fast');
				jQuery(this).siblings().removeClass('open');
				
				//remove open class to clicked panel
				jQuery(this).removeClass('panel_heading_open');
				//set cookie
				var $thisCookieName = 'state' + jQuery(this).parent().attr('id');
				$.cookie($thisCookieName, "collapse", { path: '/', expires: 10 })
			} else {
				//is closed so open panel
				jQuery(this).siblings().slideDown('medium');
				jQuery(this).siblings().addClass('open');
				
				//add open class to clicked panel
				jQuery(this).addClass('panel_heading_open');
				//set cookie
				var $thisCookieName = 'state' + jQuery(this).parent().attr('id');
				$.cookie($thisCookieName, "expand", { path: '/', expires: 10 })
			}
		});
		
		//Apply hover class for IE
		jQuery('.panel_heading').hover(function() {
			jQuery(this).addClass('panel_heading_hover');
		}, function() {
			jQuery(this).removeClass('panel_heading_hover');
		});
		
	},
	
	//Scroll to specific page in pdf storyboard
	scrollStoryboard : function(container, pageNumber) {
		
		var pageIndex = pageNumber - 1;
		
		var $paneTarget = jQuery('#pdfPageContainer');
		//reset scroll
		$paneTarget.scrollTo( 0 );

		//calculate position of pdf page relative to parent (simple method)
		var requiredScroll = pageIndex * 113;
		
		/*
		//get pdf page based on index
		var $targetPage = jQuery('#pdfStoryboard div.document_page:eq(' + pageIndex + ')');
		//Calculate width of each pdf page based on outer width plus margin-right
		var $targetPageWidth = jQuery($targetPage).outerWidth();
		var $targetPageMargin = parseFloat(jQuery($targetPage).css('margin-right'));
		var pageHorizSpace = $targetPageWidth + $targetPageMargin;
		*/
		//jQuery(...).scrollTo('#pdfPageListing',{top:'0px', left:'290px'}, 800 );	
		
		//scroll pane by required number of pixels
		$paneTarget.scrollTo(requiredScroll);	
	
	}, 
	
	scrollFullscreenPdfPages : function(pageNumber) {
		
		var pageIndex = pageNumber - 1;
		
		var $paneTarget = jQuery('#documentPagesWindow');
		//reset scroll
		//$paneTarget.scrollTo( 0 );
		//scroll pane to index
		$paneTarget.scrollTo('div.document_page:eq(' + pageIndex + ')');
	},
	
	setMinWidth : function (target, minWidth) {
		var minWidth = parseInt(minWidth);
		var currentWidth = parseInt(jQuery(target).width());
		
		if(currentWidth >= minWidth) {
			jQuery(target).css('width', currentWidth + 'px');
			//alert(currentWidth);
		} else {
			jQuery(target).css('width', minWidth + 'px');
		}
		
		//alert(currentWidth);
		
		//Force IE to check its layout
		setTimeout("jQuery('#columnContainer').css('zoom', '1');", 100);

	}
}

//find position of element
function findPosition( oElement ) {
	if( typeof( oElement.offsetParent ) != 'undefined' ) {
		
		for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
			
			posX += oElement.offsetLeft;
			posY += oElement.offsetTop;
			
		}
		
		return [ posX, posY ];
		
	} else {
		
		return [ oElement.x, oElement.y ];
		
	}
}
