/*-----------------------------------------------
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');
			}
		);
	});

}

////////////////////////////////////////////////////////////////////////////////////////////////
///////////////// eShop::: Global Functions
///////////////////////////////////////////////////////////////////////////////////////////////
function minifyNoteToSelf() {
    $('#noteToSelfDiv .image').click(toggleNoteToSelf);
}

function toggleNoteToSelf() {
    $(this).next().toggle();
    if ($(this).hasClass("active")) {
        $(this).removeClass("active");
    } else {
        $(this).addClass("active");
    }
}
function beginSearchLoader() {

    aliginLoadingWindow();
    $("#modalWindow").fadeIn(1000).css('display', 'block');
    $("#modalBackground").css('display', 'block');
    document.body.style.cursor = "wait";
}
function beginSearchLoaderWithOutWait() {
    $("#modalWindow").fadeIn(1000).css('display', 'block');
}

function completeSearchLoader() {

    $("#modalWindow").fadeOut("slow").css('display', 'none');
    $("#modalBackground").css('display', 'none');
    document.body.style.cursor = "default";
}

function aliginLoadingWindow() {
    //Set the popup window to center
    $("#modalWindow").css('top', $(window).height() / 2 - $("#modalWindow").height() / 2);
    $("#modalWindow").css('left', $(window).width() / 2 - $("#modalWindow").width() / 2);
}


function IsNumeric(input) {
    return (input - 0) == input && input.length > 0;
}

function validateStreetNumber(mouseEvent, keypressValue, fieldLength, divToolTipName, top, left) {

    if (fieldLength == 0) {
        if (IsNumeric(keypressValue) == false) {
            mouseEvent.preventDefault();
            $(divToolTipName).attr('style', 'position: absolute; filter: alpha(opacity=100); ZOOM: 1; display: block;top: ' + top + 'px;left: ' + left + 'px;');
        } else {
            $(divToolTipName).hide();
        }
    } else {
        $(divToolTipName).hide();
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////
///////////////// eShop::: Load required functions.
///////////////////////////////////////////////////////////////////////////////////////////////
$(document).ready(function() {
    globalNavigation();
    globalSearch();
    homepagePromoRotator();
    accordian();
    minifyNoteToSelf();
});