// 'unity' is the global object for unity theme functions
var unity = {};
// reduce potential conflicts with other scripts on the page
unity.jQuery = jQuery.noConflict(true);
var $unity = unity.jQuery;
// Create unique object and namespace for theme functions
unity.themeFunctions = {};
// Define a closure
unity.themeFunctions = (function() {
    // When jQuery is used it will be available as $ and jQuery but only
    // inside the closure.
    var jQuery = unity.jQuery;
    var $ = jQuery;
	var $unity = jQuery.noConflict();

	function mobileViewport(){
		if(((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPod/i))||(navigator.userAgent.match(/Android/))||(navigator.userAgent.match(/webOS/))) && ($unity('#mobileTabWrapper').is(':visible'))){
			$unity('head').append('<meta name = "viewport" content = "width=device-width; initial-scale=1.0; maximum-scale=1.0;" />');
		}
	}
	$unity(function(){ mobileViewport(); });
	
	$unity(document).ready(function() {
		if(((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPod/i))||(navigator.userAgent.match(/Android/))||(navigator.userAgent.match(/webOS/))) && ($unity('#mobileMenuTab').is(':visible'))){

			mobileMenu();
			mobileImageResize();
		}
		if(((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPod/i))||(navigator.userAgent.match(/Android/))||(navigator.userAgent.match(/webOS/))) && !($unity('#mobileMenuTab').is(':visible'))){
			$unity('#logoTitle').css('background-color', 'transparent');
		}

		if( navigator.userAgent.match(/iPad/i) ) {
			iPadAdjustments();
		}
		
		extraContent();
		
		if ( ($unity('#sub2Menu ul ul li a').is(':visible') || $unity('#sub2Menu ul ul ul li a').is(':visible')) && !($unity('#mobileTabWrapper').is(':visible')) ) {
			$unity('#sub2Menu').css({marginBottom: '20px'});
		}
	});
	
	function iPadAdjustments() {
		if ( (window.orientation == 0) && ($unity('#pageWrapper').width() > '860') ) {
			$unity('#pageWrapper, #logoTitle, #mainMenu').css({width: '88%'});
		}
		if ( ((window.orientation == 90) || (window.orientation == -90)) && ($unity('#pageWrapper').width() > '960') ) {
			$unity('#pageWrapper, #logoTitle, #mainMenu').css({width: '88%'});
		}
		var contentWidth = $unity('#contentContainer #content').width();
		$unity('#content .imageStyle').each(function(){
			if(($unity(this).width()) > contentWidth){
				$unity(this).width(contentWidth);
				$unity(this).height('auto');
			}
		});
		var sidebarWidth = $unity('#sidebarContainer #sidebar').width();
		$unity('#sidebar .imageStyle').each(function(){
			if(($unity(this).width()) > sidebarWidth){
				$unity(this).width(sidebarWidth);
				$unity(this).height('auto');
			}
		});
	}
	
	function mobileMenu() {
		$unity('#mobileMenuTab, #hideMenu').click(function () {
			$unity('#mainMenuWrapper').slideToggle('medium');
		});
		$unity('#mobileSidebarTab').click(function () {
			$unity('#sidebarContainer').slideToggle('medium');
		});
	}
	
	function mobileImageResize() {
		$unity('.imageStyle').each(function(){
			if( window.orientation == 0 ){
				if( ($unity(this).width() > 280) ) {
					$unity(this).width(280);
				}
			} else if ( (window.orientation == 90) || (window.orientation == -90) ) {
				if( ($unity(this).width() > 440) ) {
					$unity(this).width(440);
				}
			}
		});
		$unity('#logo img').each(function(){
			if( window.orientation == 0 ){
				if( ($unity(this).width() > 300) ) {
					$unity(this).width(300);
				}
			} else if ( (window.orientation == 90) || (window.orientation == -90) ) {
				if( ($unity(this).width() > 460) ) {
					$unity(this).width(460);
				}
			}
		});
	}

	/* ExtraContent */
	function extraContent() {
		var extraContent =  (function() {
			var ecValue = 10;
			for (i=1;i<=ecValue;i++)
			{
				$unity('#myExtraContent'+i+' script').remove();
				$unity('#myExtraContent'+i).appendTo('#extraContainer'+i).show(0);
			}
		})();
	}

})(unity.themeFunctions);
