var general = { 

	isChrome: navigator.userAgent.toLowerCase().indexOf('chrome') > -1,
	isSafari: navigator.userAgent.toLowerCase().indexOf('safari') > -1,
	
	// OnLoad functions
	init: function(){
		this.bubbleEvt();
	},
	
	bubbleEvt: function() {
		
		if($("#content-rotator").length)
		{
			$("#content-rotator").flash(
				{
					swf: "/gui/flash/FlashFile.swf",
					height: 300,
					width: 940
				}
			);
		}

		$(".sorter-link a").click(function(){
			return false;
		})

		$(".portfoliopage .logo-list a, .teampage .logo-list a").click(function(){
			general.changePortTeamContent(this);
			return false;
		})
		
		$(".startpage .logo-list li").click(function(){
			general.logoListStartPageClick(this);
			return false;
		})		
		
		$(".logo-list li").mouseenter(function(){
     		general.hoverPortTeamContent(this,true)
	    }).mouseleave(function(){
	    	general.hoverPortTeamContent(this,false)
	    })
		
		if ($('.deep-linkage').length) {
			general.deepLinkage(this);
		}
		
		$(".sorter-link").click(function(){
			general.doSorter(this);
			return false;
		})
		
		$(".sort-link-placer").mouseenter(function(){
			general.showHideSortLayer(this,true);
			return false;
	    }).mouseleave(function(){
	    	general.showHideSortLayer(this,false)
	    })

		$("ul.menu li").not($("ul.menu li ul, ul.menu li ul li")).mouseenter(function(){
     		general.hoverShowMenu(this,true)
	    }).mouseleave(function(){
	    	general.hoverShowMenu(this,false)
	    })		
		
		//if browser chrome or safari and element '.logo-list' exists
		if ((general.isChrome || general.isSafari) && $('.logo-list').length) {
			general.checkBrowserSafariChrome();
		}
			
	},
	
	hoverShowMenu: function(e,state) {
		
		var $subMenuElm = $(e).find('ul')
		var paddingValue = 20;
		var marginValue = ($subMenuElm.width() / 2) - (($(e).width() - paddingValue) / 2)
		if (state) {
			$subMenuElm.show().css('right',-marginValue);
		} else {
			$subMenuElm.hide();
		}
		
	},
	
	changePortTeamContent: function(e) {
		
		var $targetParentElm = $('.blurbrow2');
		$targetParentElm.css('background','none');
				
		var $content = $(e).parent().find('div.content').html();	
		var $targetElm = $('.blurbrow2 .blurb1 .blurb-content');
		$targetElm.html($content);
		
		var $imageSrc = $(e).parent().find('img.right-column-image').attr('src')
		
		var $imageContentPlacer = $("<div/>")
			.addClass('content-image-placer')
		    .prependTo($targetElm)		
		
		$("<img/>")
		    .attr({ src: $imageSrc})
		    .prependTo($imageContentPlacer)
	},
	
	hoverPortTeamContent: function(e,state) {
		
		var $overlayElm = $(e).find('div.list-overlay');
		
		var $linkElm = $(e).find('a:first');
		
		var $creListFrameElm = $("<div/>")
			.addClass('clicked-logo-list-frame')
		    
		if (state) {
			if ($overlayElm.css('display') == 'none') {
				$overlayElm.css({'opacity' : '0', 'display' : 'block'}).animate({opacity: 0.90}, 200)
				if ((general.isChrome || general.isSafari) && $('.logo-list').length) {
					$linkElm.find('img').css({'margin': '0 0 0 -1px'})
				}
			}			
		} else {
			$overlayElm.css('display','none')
		}
		
		$overlayElm.unbind('click');
		
		$overlayElm.click(function(){		
			$('.clicked-logo-list-frame').remove();		
	    	$linkElm.click();
			window.location.hash = '#'+$(e).attr('class');	
			$overlayElm.before($creListFrameElm)

				if ((general.isChrome || general.isSafari) && $('.logo-list').length) {
					$linkElm.find('img').css({'margin': '0 0 0 -1px'})
				}

			return false;
	    })
		
	},	
	
	deepLinkage: function(e) {
		var hashValue = window.location.hash.slice(1);
		if (hashValue) {
			$('.'+hashValue ).find('a:first').click();
			
		var $creListFrameElm = $("<div/>")
			.addClass('clicked-logo-list-frame')			
			
			$('.'+hashValue ).find('div.list-overlay').before($creListFrameElm)			
			
		}
	},
	
	doSorter: function(e) {
		$sortValue = $(e).attr('title')
		
		$('#category').text(" - "+$sortValue)
		
		
		if ($sortValue == 'all') {
			$('.list-caterories').parent('li').animate({opacity: 1}, 200).show();
		} else {
			$('.list-caterories').not('.'+$sortValue).parent('li').animate({opacity: 0}, 500).hide();	
			$('.'+$sortValue).parent('li').animate({opacity: 1}, 200).show();			
		}
	},
	
	showHideSortLayer: function(e,state) {
		if (state) {
			$('.sort-link-layer').show();
		} else {
			$('.sort-link-layer').hide();
		}
	},
	
	logoListStartPageClick: function(e) {
		
		window.location = $(e).find('a').attr('href')
		return false;
	},
	
	checkBrowserSafariChrome: function(){
		$('.startpage .logo-list li, .portfoliopage .logo-list li, .teampage .logo-list li').not('.content li').css({'width' : '140px', 'height' : '97px'})               
	} 	
	
}

$(document).ready(function(){			   
	general.init();
});
