/*-----------------------------------------------
A HotHouse Production
Virgin Mobile
Copyright 2009
author: Tim Gleeson
email: 	timg@hothouse.com.au
----------------------------------------------- */

/*search functions*/
function getDocVariables() {
	var docVariables = { };
	var searchString = (location.search.length > 0 ? location.search.substring(1) : null);
	if (searchString != null) {
		var pairs = searchString.split('&');
		var curPair = { };
		for (var i = 0; i < pairs.length; i++) {
			curPair = pairs[i].split('=');
			if (curPair.length == 2) {
				docVariables[curPair[0]] = curPair[1];
			}
		}
	}
	return docVariables;
}

function autoSearch(q) {
	var query = q;
	$('input[name="search"]').val(query);
	$('input.gsc-search-button').click();
}

function globalSearch() {
	var docVariables = getDocVariables();	
	$('input[name="q"]').val(docVariables.q);
	
	$('input[name="btnSearch"]').click(function() {
			var globalSearchValue = $('input[name="q"]').val();
			if((globalSearchValue == '') || (globalSearchValue == 'Search Virgin Mobile')) {
				alert('Oops! Please enter a keyword to search for.');
				$('input[name="q"]').focus();
				return false;
			} else {
				var t=setTimeout(function(){ autoSearch(docVariables.q) }, 400);  
			}
			
	});
	
	$('input[name="q"]').focus(function() {
		var globalSearchValue = $(this).val();
	    if(globalSearchValue == 'Search Virgin Mobile') {
		 $(this).val('');
		}
	});	
	$('input[name="q"]').blur(function() {
		var globalSearchValue = $(this).val();
		if(globalSearchValue == '') {
			$(this).val('Search Virgin Mobile');
		}	
	});
}

/* Global Navigation */
function globalNavigation() {
	$('.main-nav > ul > li').hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	);
}

function homepagePromoRotator() {
	
	var rotatorItemLength = $('.promo-rotator .promo-item').length;
	var movement = 780;
	var currentPosition = 0;
	var time = 350;
	
	$('.promo-rotator').css('position', 'relative');
	$('.promo-rotator .inner').css({'position': 'absolute', 'top': '0', 'left': '0'})
	$('.promo-rotator .inner div:first-child').addClass('active');
	$('.promo-controls ul li:first-child').addClass('active');
	$('.promo-rotator .inner div').each(function(i,n) {
		$(n).attr('rel', i);
	});
	$('.promo-controls ul li').each(function(i,n) {
		$(n).attr('rel', i);
	});
	
	$('.promo-controls ul li').each(function(i,n) {
		$(n).find('a').click(function() {
			var currentItem = $('.promo-rotator .inner div.active').attr('rel');
			currentPosition = $('.promo-rotator .inner').position();
			nextPosition = i * movement;
			$('.promo-rotator .inner').animate({'left': '-'+nextPosition}, 200);
			$('.promo-rotator .inner div[rel='+currentItem+']').removeClass('active');
			$('.promo-rotator .inner div[rel='+i+']').addClass('active');
			$('.promo-controls ul li[rel='+currentItem+']').removeClass('active');
			$('.promo-controls ul li[rel='+i+']').addClass('active');
			return false;
		});
	});
	
	var timeOut = setTimeout(scrolling, 7000);
	
	function scrolling() {
		
		var currentItem = $('.promo-rotator .inner div.active').attr('rel');
		
		currentItem = currentItem * 1;
		
		if(currentItem != '3') 
		{
			var nextItem = currentItem + 1;
			$('.promo-rotator .inner div[rel='+currentItem+']').removeClass('active');
			$('.promo-rotator .inner div[rel='+nextItem+']').addClass('active');
			
			$('.promo-controls ul li[rel='+currentItem+']').removeClass('active');
			$('.promo-controls ul li[rel='+nextItem+']').addClass('active');
			
			currentPosition = $('.promo-rotator .inner').position();
			nextPosition = nextItem * movement;
			$('.promo-rotator .inner').animate({'left': '-'+nextPosition}, 200);
		}
		else
		{
			var nextItem = 0;
			$('.promo-rotator .inner div[rel='+currentItem+']').removeClass('active');
			$('.promo-rotator .inner div[rel='+nextItem+']').addClass('active');
			
			$('.promo-controls ul li[rel='+currentItem+']').removeClass('active');
			$('.promo-controls ul li[rel='+nextItem+']').addClass('active');
			
			$('.promo-rotator .inner').animate({'left': '0'}, 200);
		}
		
		var timeOut = setTimeout(scrolling, 7000);
		
	}
	
}

function accordian() {
	
	var collapseableItemLength = $('.collapse-item-a').length;
	
	$('.collapse-item-a').each(function(i,n) {
		$(n).find('h3').toggle(
			function() {
				$(n).find('.content').slideDown('fast');
			}, 
			function() {
				$(n).find('.content').slideUp('fast');
			}
		);
	});
	
}

$(document).ready(function() {
	globalNavigation();	
	globalSearch();
	homepagePromoRotator();
	accordian();
});
