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


var Site = {
	
	init:function() {
	
		//HANDLE EXTERNAL LINKS
		Site.externalLinks();

		//SEARCH
		if($('#search').length != 0) {
			Site.search();
		}
		
		// NEWS FEED 
		if($('#news-feed').length != 0) {
			Site.newsFeed();
		}
		
		//HOME FEATURE
		if($('#home-feature').length != 0) {
			Site.homeFeature();
			Site.clientRating();
			//Site.homeAdvertising();
		}
		
		// ADVERTISING SLIDER
		if($('#advertising').length != 0) {
			Site.advertising();
			
			if($('#advertising ul li').length == 0) {			
				$('#advertising').hide();
			}
		}
		
		// 2ND ADVERTISING SLIDER FOR SECOND LEVEL
		if($('#advertising-2').lentgh != 0) {
			Site.advertising2();
			
			if($('#advertising-2 ul li').length == 0) {			
				$('#advertising-2').hide();
			}
		}
		
		//TEXT RESIZZLE
		if($('#usability-controls').length != 0) {
			$('#primary-inner').textResizzle();	
		}
		
		//PRINT
		if($('#usability-controls').length != 0) {
			Site.print();
		}

		//FORM VALIDATION
		if($('.val-form').length != 0){
			$('.val-form').validate();
		}
		
		//CONTACT FORM VALIDATION
		if($('#contact-form').length != 0) {
			$('#contact-form').validate();
		}
		
		// Breadcrumbs
		if($('#breadcrumbs').length != 0) {
			// Delay the breadcrumbs animation to get the correct width of the <li>s 
			// As we use cufon the li will be a bit bigger 
			window.setTimeout("$('#breadcrumbs').jBreadCrumb()",1000);
		}
		
		// Accordion
		if($('#accordion').length != 0) {
			
			if($('#accordion h2').length > 1) {
				$("#accordion").accordion({ 
									header: 'h2', 
									autoHeight: false,
									collapsible: true,
									active: false
								});
			} else {
				$('#accordion h2 a').hide();
				$('#accordion h2').css('padding-right',0);
			}
		}
		
		// Widget 
		if($('#widget-become-a-me').length != 0) {
			$('#reset-checklist').click(function() {
				$.cookies.set('BECOME_ME','0');
				$.cookies.set('BECOME_ME_NO', '0');
			});
			
			Site.MEchecklist();
		}
		
		// Open ECA membership form
		if($('#show-eca-membership').length != 0) {
			$('#show-eca-membership + div').hide();
			$('#show-eca-membership').click(function () {
				$(this).next('div').show('slow');
				$(this).children('a').addClass('show-eca-mambership-active');
				return false;
			});
			
		}
		// Twitter feed
		if ($("#twitter-feed").length != 0) {
			Site.initTweets();
		}
		
		// Photo gallery
		if($('.thumbnail-listing').length != 0) {
			Site.fancyBoxInit();
		}
	},
	
	//Still need to plug this in - just a place holder atm
	textResize:function() {
	
		$('.controls .text a').click(function() {
			var textSize = $(this).attr('class');
			$('body').removeClass('small large').addClass(textSize);
			$.cookie('TEXT_SIZE',textSize, { path: '/', expires: 10000 });
			return false;
		});
	
	},
	
	homeFeature:function() {
	
		$('#home-feature ul').carouFredSel({
			scroll: {
				//fx			: "crossfade",
				fx			: "scroll",
				easing		: "linear",
				duration	: 600
			},
			items: {
				visible: 1
			},
			pagination  : "#feature-pagination",
			auto: true
		});
	
	},
	
	clientRating:function() {
		
		$('#client-rating ul').carouFredSel({
			scroll: {
				//fx			: "crossfade",
				fx			: "scroll",
				easing		: "linear",
				duration	: 800
			},
			items: {
				visible: 1
			},
			pagination  : "#rating-pagination",
			auto: true
		});

	},
	
	advertising:function() {
		
		$('#advertising ul').carouFredSel({
			scroll: {
				//fx			: "crossfade",
				fx			: "scroll",
				easing		: "linear",
				duration	: 700
			},
			items: {
				visible: 1
			},
			next: "#ad-next",
			prev: "#ad-prev",
			auto: true
		});
	
	},
	
	advertising2:function() {
		
		$('#advertising-2 ul').carouFredSel({
			scroll: {
				//fx			: "crossfade",
				fx			: "scroll",
				easing		: "linear",
				duration	: 700
			},
			items: {
				visible: 1
			},
			next: "#ad-next-2",
			prev: "#ad-prev-2",
			auto: true
		});
	
	},
	
	search:function() {
		
		$('#search input').focus(function() {
			$(this).css({'background-position':'-280px -28px'});			
			$(this).prev('label').css({'left':'-9999px'});
		});
		
		$('#search input').blur(function() {
			$(this).css({'background-position':'-280px 0'});
			if ($(this).val() == '')
				$(this).prev('label').css({'left':'15px'});
		});
		
	},
	
	newsFeed:function() {
		
		$('#news-feed li').click(function () {
			window.location = $(this).find('a').attr('href');
		});
	},
		
	print:function() {
		
		$('#print').click(function(){
			window.print();
		});
		
	},
	
	externalLinks:function() {
	
		var extLinks = $('a.external');
			if ( extLinks.length ) {
				extLinks.each(function() { 
					$(this).attr('target', '_blank');
			});
		}
	
	},
	
	initTweets:function() {
   
        // Replace the text temporarily
        var container = $('#twitter-feed');
       
        container.html('<div class="ajax-loading"></div>');
       
        // Do the request
        $.ajax({
                        'url': '/?tweets',
                        'dataType': 'json',
                        'type': 'GET',
                        'success':     function(json) {
                                        // Take the first tweet and stick the contents in the container
                                        container.html('<p><strong>@MasterElectrics</strong> ' + json[0].text + '</p>');
										// Add target to external links
										$('#twitter-feed .external').attr('target','_blank');
                                    },
                        'error':     function(xhr, textStatus, eThrown) {
                                        //alert('error!');
                                    }
                    });
    },
	
	MEchecklist:function() {
	
		if(!$.cookies.get('BECOME_ME')) { 
			becomeME = '0';
		} else {
			becomeME = $.cookies.get('BECOME_ME');
		}
		if(!$.cookies.get('BECOME_ME_NO')) { 
			become_ME_NO = '0';
		} else {
			become_ME_NO = $.cookies.get('BECOME_ME_NO');
		}
		
		$('#widget-become-a-me').find('li').each(function (index, elem) {
			
			if(index > becomeME) {

				$(this).hide();
				
			} else if(index < becomeME) {
			
				$(this).children('div').children('a.switch-yes').addClass('btn-yes-active').removeClass('switch-yes');
				$(this).children('div').children('a.switch-no').addClass('btn-no-unactive').removeClass('switch-no');
				
			} else if(become_ME_NO != '0' && index == becomeME) {
			
				$(this).children('div').children('a.switch-yes').addClass('btn-yes-unactive').removeClass('switch-yes');
				$(this).children('div').children('a.switch-no').addClass('btn-no-active').removeClass('switch-no').after($('#reset-checklist'));
				
			}
			
		});
		
		
		$('#widget-become-a-me > li div a.switch-yes').click(function() {
			
			// If Yes button already clicked do nothing
			if($(this).hasClass('btn-yes-unactive')  || $(this).hasClass('btn-yes-active'))  {
				return false;
			} else {
				
				$(this).next('a').addClass('btn-no-unactive').removeClass('switch-no');
				$(this).addClass('btn-yes-active').removeClass('switch-yes');
				$(this).parent('div').parent('li').next().show('slow');
				
				if($(this).parent('div').parent('li').children('h4').attr('id') != 'q6') {
					$(this).next('a').after('<span class="next-question">&darr; Next question below</span>');
				}
				
				becomeME++;
				
				// Using String() because IE7 is annoying !
				// It doesn't let us pass a int type to set the cookie
				$.cookies.set('BECOME_ME', String(becomeME));

			}
			
			if(!$(this).attr('href')) {
				return false;
			}
		});
		
		$('#widget-become-a-me > li div a.switch-no').click(function() {
			
			// If No button already clicked do nothing
			if($(this).hasClass('btn-no-unactive')  || $(this).hasClass('btn-no-active'))  {
				return false;
			} 
			else {
				$(this).addClass('btn-no-active').removeClass('switch-no');
				$(this).prev('a').addClass('btn-yes-unactive').removeClass('switch-yes');
				$.cookies.set('BECOME_ME_NO','1');
			}
		});
		
		// 
		$('#widget-become-a-me > li div a.btn-no-unactive').click(function()  {	return false });
		$('#widget-become-a-me > li div a.btn-no-active').click(function() 	  {	return false });
		$('#widget-become-a-me > li div a.btn-yes-active').click(function()   {	return false });
		$('#widget-become-a-me > li div a.btn-yes-unactive').click(function() { return false });
	},
	
	fancyBoxInit:function(){

		$("a[rel=gallery]").fancybox({
			'titleShow'			: 'False',
			'overlayColor'		: 'transparent',
			'overlayOpacity'	: '1'
		});
	}
	
};
