/*
 * Copyright(c) 2009, Soft Idiom Limtied
 * www.softidiom.com and www.4dsites.com
*/

var Site = {
	slideshow: null, //gets set in startSlideShow
	slideshowData: {
			'home-menu' : {
				'bg-welcome.jpg': { caption: '<br />Global suppliers of Web Presence Excellence&#0153;<ul class="welcome-list"><li><a class="btn" href="/creative/visual-design"><img src="/site/img/icon-creative.png" width="51" height="41" alt=""/>Creative Design</a></li><li><a class="btn" href="/web/development"><img src="/site/img/icon-webdevelopment.png" width="51" height="41" alt=""/>Web Development</a></li><li><a class="btn" href="/web/features"><img src="/site/img/icon-webdesign.png" width="51" height="41" alt=""/>Web Design</a></li><li><a class="btn" href="/hosting/about-hosting"><img src="/site/img/icon-hosting.png" width="51" height="41" alt=""/>Professional Hosting</a></li><li><a class="btn" href="/marketing/internet-marketing"><img src="/site/img/icon-internetmarketing.png" width="51" height="41" alt=""/>Internet Marketing</a></li><li><a class="btn" href="/audit"><img src="/site/img/icon-audit.png" width="51" height="41" alt=""/>Website Auditing</a></li></ul>' },
				'bg-startingout.jpg': { caption: 'Starting out on the web? You\'re in good hands <a href="/starting-out">Read more...</a>' },
				'bg-standout.jpg': { caption: 'With billions of websites, how will yours stand out? <a href="/marketing/internet-marketing">Find out how...</a>' },
				'bg-ecommerce.jpg': { caption: 'Professional e-commerce solutions <a href="/ecommerce">Read more...</a>' },
				'bg-phone.jpg': { caption: '<span style="color:#000">Call +44(0)1376 350001</span> for friendly help and advice<a href="javascript:Site.chat()">or Text Chat now</a>' }
			},
			'webdesign-menu' : {
				'bg-corporate.jpg': { caption: 'Whatever the size of your organization, we have the perfect solution <a href="/organisations">Read more...</a>' }, 
				'bg-ecommerce.jpg': { caption: 'Professional e-commerce solutions <a href="/ecommerce">Read more...</a>' },
				'bg-development.jpg': { caption: 'We\'re experts in custom web development <a href="/development">Read more...</a>' }
			},
			'creative-menu' : {
				'bg-simple.jpg': { caption: 'Sometimes the best creative design starts with the simplest concept <a href="/creative-design">Read more...</a>' },
				'bg-designer.jpg': { caption: 'Let our professional in-house creative team project your brand <a href="/creative-design">Find out more...</a>' }
			},
			'hosting-menu' : {
				'bg-hosting.jpg': { caption: 'Where happy websites come to stay <a href="/hosting/about-hosting">Shared &amp; Dedicated hosting...</a>' }, 
				'bg-cloud.jpg': { caption: 'Scaleable and affordable Cloud Computing<a href="/hosting/cloud">Read more...</a>' }
			},
			'marketing-menu' : {
				'bg-seo.jpg': { caption: 'Don\'t let your website go undiscovered <a href="/search-engine-optimisation">Search Engine Optimisation...</a>' },
				'bg-corporate.jpg': { caption: 'No matter the size of your organization, we have a solution for you <a href="/organisations">Read more...</a>' }
			}				
		},
			
	start: function() {
		Site.setBrowserCSS();
		Site.startLinkAnimation();
		if(Site.hasCSS()) Site.startMenus();
		if(Site.hasCSS()) Site.highlightMenu();
		if(Site.hasCSS()) Site.startSlideShow();
		if(Site.hasCSS()) Site.startPortfolio();
		Site.startGoogleAnalytics();
	},

	startPortfolio: function() {
		if(!$('Portfolio')) return;
		Site.portfolio = new MooFlow($('Portfolio'), {
				startIndex: 3,
				useAutoPlay: true,
				togglePlay: true,
				useCaption: true,
				useViewer: true,
				bgColor: 'transparent',
				interval: 4000,
				heightRatio: 0.29,
				factor: 150,
				offsetY: -140,
				onEmptyinit: function(){
					this.loadJSON('/site/phplib/getCaseStudyImages.php');
				}
			});
		Site.portfolio.addEvent('request', function(){
			Site.portfolio.attachViewer();
			Site.portfolio.play();
		});		
	},
	
	startSlideShow: function() {
		var showCaptions = true; 
		if($('page-fade')) { //show captions only on the index page
			showCaptions = false
		}
		Site.slideshow = new Slideshow('show', Site.slideshowData["home-menu"], {
			loader:true, 
			controller: false, 
			delay:6000,
			duration: 2000,
			captions:showCaptions, 
			height: 430, 
			width: 950, 
			hu: '/site/img/', 
			thumbnails: false});
	},
		
	highlightMenu: function() {
		//set the class of the current menu item to 'selected' so it can be styled
		$$('.menu a').each(function(el){
			if(el.href == window.location.href) {
				el.className = "selected";
			}
		})	
	},

	startMenus: function() {
		var sectionCursor = $('section-cursor');
		$each($$('#sections li a'), function(el) {
			el.hideFocus = true;
			var sectionFx = new Fx.Morph(sectionCursor, {link: 'chain'});
			var menuMaskFx = new Fx.Morph($('menu'), {link: 'chain'});
			el.addEvents({
				'click': function() {
					if (Site.selectedSection == el) { //console.log('close section');
						//Site.selectedSection = null;
						//menuMaskFx.start({'height': '0px'});
					} else {
						if (Site.selectedSection) { //console.log('change section');
							var chainFx = new Chain();
							chainFx.chain(
								function() {menuMaskFx.start({'height': '0px'})},
								function() {
									$each($$('#menu ul'), function(m) {
										m.setStyle('display', 'none');
									});
									var menuname = el.href.split('#');
									menuname = (menuname[menuname.length-1])+'-menu';
									$(menuname).setStyle('display', 'inline');
									if (Site.slideshow) { 
										Site.slideshow.load(Site.slideshowData[menuname]);
									}
								},
								function() {
									var bpx = el.getPosition(sectionCursor).x;
									sectionFx.start({
										'background-position': (bpx - 874) + 'px 0px',  //this doesn't work in IE6/7. 
										'background-position-x' : (bpx - 874) + 'px',	//needed for IE6/7
										'background-position-y' : '0px'					//needed for IE6/7
									});
								},
								function(){menuMaskFx.start({'height': '60px'});}
							);
							chainFx.callChain(); //hide menu-mask
							(function(){chainFx.callChain();}).delay(300); //hide menus except selected
							(function(){chainFx.callChain();}).delay(380); //move tab
							(function(){chainFx.callChain();}).delay(600); //show menu-mask
							Site.selectedSection = el;
						} else { //console.log('open section');
							var chainFx = new Chain();
							chainFx.chain(
								function() {
									$each($$('#menu ul'), function(m) {
										m.setStyle('display', 'none');
									});
									var menuname = el.href.split('#');
									menuname = (menuname[menuname.length-1])+'-menu';
									$(menuname).setStyle('display', 'inline');
									if (Site.slideshow) { 
										Site.slideshow.load(Site.slideshowData[menuname]);
									}
								},
								function() {
									var bpx = el.getPosition(sectionCursor).x;
									sectionFx.start({
										'background-position': (bpx - 874) + 'px 0px',	//this doesn't work in IE6/7. 
										'background-position-x' : (bpx - 874) + 'px',	//needed for IE6/7
										'background-position-y' : '0px'					//needed for IE6/7
									});
								},
								function(){menuMaskFx.start({'height': '60px'});}
							);
							chainFx.callChain(); //hide menus except selected
							(function(){chainFx.callChain();}).delay(380); //move tab
							(function(){chainFx.callChain();}).delay(600); //show menu-mask
							Site.selectedSection = el;
						}
					}
					return false;
				}
			});
		});
		$('section-sel').fireEvent('click');
	},
		
	startLinkAnimation: function() {
		var badgesMorph = new Fx.Morph('badges',{ 'duration':'300', link:'cancel' });
		var footerinfoMorph = new Fx.Morph('footer-info',{ 'duration':'300', link:'cancel' });
		$('footer-logo').addEvents({
			'mouseenter' : function() { badgesMorph.start({ 'opacity':'1' }); footerinfoMorph.start({ 'color':'#888' }) },
			'mouseleave' : function() { badgesMorph.start({ 'opacity':'.4' });footerinfoMorph.start({ 'color': '#333' }) }
		});
		$('badges').addEvents({
			'mouseenter' : function() { badgesMorph.start({ 'opacity':'1' }); },
			'mouseleave' : function() { badgesMorph.start({ 'opacity':'.4' }); }
		});
		$('footer-info').addEvents({
			'mouseenter' : function() { footerinfoMorph.start({ 'color':'#888' }); },
			'mouseleave' : function() { footerinfoMorph.start({ 'color': '#333' }); }
		});
	},
		
	startGoogleAnalytics: function() {
	    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	    new Asset.javascript(gaJsHost + "google-analytics.com/ga.js", {
	        onload: function() {
	            var pageTracker = _gat._getTracker("UA-4045238-3"); // your id here
	            pageTracker._initData();
	            pageTracker._trackPageview();
	        }
	    });
	},
	
	setBrowserCSS: function() {
		var IE6 = Browser.Engine.trident && (Browser.Engine.version = 4);
		var IE = Browser.Engine.trident;
		var notIE = !IE;

		if(Site.hasCSS()){
			var pageHeight = $('page').getSize().y;
			//specific pages offsets - some pages miscalculate the page height because JavaScript changes the dom. 
			var paths = window.location.pathname.split('/');
			var pagename = paths[paths.length-1];
			if(notIE) {// not IE
				if(pagename == 'quote') pageHeight+=206;
			} else { //IE
				if(pagename == 'quote') pageHeight-=60;
				if(pagename == 'audit') pageHeight-=60;
				if(pagename == 'contact') pageHeight-=60;
				if(pagename == 'portfolio') pageHeight-=140;

			}
			if(IE6) { //IE6
				if(pagename == 'portfolio') pageHeight+=200;
			}
			$('page-mask').setStyle('height',pageHeight+'px');
			$('page-container').setStyle('height',pageHeight + 'px');
			$('page').setStyle('background-color','transparent');
			//apply opactiy (for IE since its filters are not valid CSS)
			$('page-mask').setStyle('opacity','.8');
			$('menu-mask').setStyle('opacity','.5');
			$('badges').setStyle('opacity','.3');
		}
	},
	
	hasCSS: function() {
		return ($('menu').getStyle('overflow') == 'hidden'); 
	},
		
	loadCSS: function(cssSrc) {
		var cssLink = document.createElement("link")
		cssLink.href = cssSrc;
		cssLink.rel = "stylesheet";
		cssLink.type = "text/css";
		document.body.appendChild(cssLink);
	},
	
	scrollToTop: function() {
		var fx = new Fx.Scroll(window, {duration: 200});
		fx.toTop(); 
	},
	
	chat: function() {
		javascript:openLiveHelp(1);
		//alert('Sorry, Live Chat is currently offline. Please call us or use the contact page to contact us and we will get right back to you.');
	},
		
	email: function(address) {
		window.location = 'mailto:'+address+'@4dsites.com';	
	},
	
	bookmark: function(url, title){
		bookmark_url = url ? url : window.location;
		bookmark_title = title ? title : document.title;
		if (window.sidebar) // firefox
			window.sidebar.addPanel(bookmark_title, bookmark_url, "");
		else if(window.opera && window.print){ // opera
			var elem = document.createElement('a');
			elem.setAttribute('href',bookmark_url);
			elem.setAttribute('title',bookmark_title);
			elem.setAttribute('rel','sidebar');
			elem.click();
		}
		else if(document.all)// ie
			window.external.AddFavorite(bookmark_url, bookmark_title);
		}
	}

window.addEvent('load', Site.start);