$(window).load(function() {
	// feature tour scripts
	if($('#toursteps').length > 0) {
		$('#toursteps li a').click(function () {
			$(this).parent().siblings('li').removeClass('selected');
			$(this).parent().addClass('selected');
			$.address.value($(this).attr('href'));
			$('.behind').load(this.href+" #content_wrapper > *",null,function () {
				$('.infront').fadeOut('normal');
				$('.behind').fadeIn('normal', function() { 
					$('.infront').addClass('behind');
					$('.infront').removeClass('infront'); 
					$(this).addClass('infront'); 
					$(this).removeClass('behind');
					$('#content_wrapper').height($(this).height());
				});
			});
			// Track the AJAX page load in GA
			//pageTracker._trackPageview('/' + $(this).attr('href'));
			return false;
		});
		
		var redirect = $.address.value();
		if(redirect == '/') 
			$($('#toursteps li a')[0]).click();
		else
			$('#toursteps li a').each(function() {
				if(('/'+$(this).attr('href')) == redirect) {
					$(this).click();
				}
			});
	}
	
	// yellow popup in feature area
	$('#actionlink').click(function() {
		// if it is already open, close it
		if($(this).attr('class').indexOf('close') >= 0) {
			$('#actiontext').slideUp('fast');
			$(this).removeClass('close');
		}
		// otherwise, open it.
		else {	
			$('#actiontext').slideDown('fast');
			$(this).addClass('close');
			// Track the popup opening event in GA
			pageTracker._trackEvent('Banner Slides', $(this).attr('title'));
		}
		return false;
	});
	
	// home page control freak box
	$('#control_freak .button_holder a').click(function() {
		$('#control_freak .image_holder').html('<img src="'+this.href+'" alt=""/>');
		return false;
	});
	
	// homeo page tv modal for video
	$('#video a').click(function () {
		var modalhtml = '<div class="modal_bg"></div><div class="modal"><a href="" class="close"><img src="cmsimages/modal-close.png" alt="X" /></a><object height="498" width="640" type="application/x-shockwave-flash" id="VideoPlayback" data="reseller-overview/Reseller Overview_controller.swf"><param name="allowfullscreen" value="true"><param name="allowscriptaccess" value="always"><param name="wmode" value="transparent"><param name="quality" value="best"></object></div>'
		$('body').append(modalhtml);
		$('.modal_bg').height($('body').height());
		$('.modal').css('left',($(window).width()-689)/2 + 'px');
		$('.modal_bg').css('display','block');
		if($.browser.msie || window.navigator.userAgent.indexOf('Chrome') >= 0)
			$('.modal').css('display','block');
		else
			$('.modal').fadeIn();
		// set up close button
		$('.modal .close').click(function() {
			$('.modal_bg, .modal').remove();
			$('.example').removeClass('selected');
			return false;
		});
		// Track the video modal event in GA
		pageTracker._trackEvent('Videos', 'Home Page - Reseller Overview');
		return false;
	});
	
	// example sites scripts
	$('.example').click(function () {
		$('.selected').removeClass('selected');	
		// mark as selected
		$(this).addClass('selected');
		// get modal image
		var imgsrc = $(this).children('img').attr('src');
		imgsrc = imgsrc.replace('.jpg','-modal.jpg');
		// add the modal html if it doesn't exisit yet
		if($('.modal_bg').length == 0) {
			// append modal html to the body element
			var modalhtml = "";
			modalhtml += '<div class="modal_bg">&nbsp;</div>';
			modalhtml += '<div class="modal"><a href="" class="close"><img src="cmsimages/modal-close.png" alt="X" /></a><a href="" class="arrow_left"><img src="cmsimages/arrow-left.png" alt="<"/></a><a href="" class="arrow_right"><img src="cmsimages/arrow-right.png" alt=">"/></a><img src="'+imgsrc+'" alt="" class="site_img"/><a href="'+this.href+'" class="site_link"><img src="cmsimages/click-view-site.png" alt="Click to view live site" /></a></div>';
			$('body').append(modalhtml);
			$('.modal_bg').height($('body').height());
			$('.modal').css('left',($(window).width()-624)/2 + 'px');
			var posfromTop;
			if($.browser.msie)
				posfromTop = document.documentElement.scrollTop;
			else
				posfromTop = $(window).attr('scrollY');
			$('.modal').css('top',posfromTop + 23 + 'px');
			$('.modal_bg').css('display','block');
			$('.modal').fadeIn();
			// set up close button
			$('.modal .close').click(function() {
				$('.modal_bg, .modal').remove();
				$('.example').removeClass('selected');
				return false;
			});
			
			// set up arrows
			$('.modal .arrow_left').click(function() {
				$('.modal img.site_img').fadeOut(function () {
					if($('.selected').prev('.example').length == 0)
						$($('.example')[$('.example').length-1]).click();
					else
						$('.selected').prev('.example').click();
				});
				return false;
			});
			$('.modal .arrow_right').click(function() {
				$('.modal img.site_img').fadeOut(function () {
					if($('.selected').next('.example').length == 0)
						$($('.example')[0]).click();
					else
						$('.selected').next('.example').click();
				});
				return false;
			});
		}
		else {
			//otherwise, just change the image and link
			$('.modal img.site_img').attr('src',imgsrc);
			$('.modal a.site_link').attr('href',this.href);
			$('.modal img.site_img').load(function() {
				$(this).fadeIn();
			});
		}
		return false;
	});
	
	/* Signup form javascript*/
	if($('#signup').length > 0) {
		$('#signup').validate({
			messages: {
				'form[company]': 'Please enter your agency/company name.',
				'form[fname]': 'Please enter your first name.',
				'form[lname]': 'Please enter your last name.',
				'form[email]': {
					required: 'Please enter your email address.',
					email: 'Please enter a valid email address.'
				},
				'form[uname]': 'Please enter a username.',
				'form[password]': 'Please enter a password.',
				'form[cpassword]': 'Please confirm your password.'
			}
		});
		$('a#submit').click(function () {
			if($('#signup').valid()) {
				$('#signup')[0].submit();
			}
			return false;
		});
	}
});