/*****************************************************************************************
 *	Site specific javascript
****************************************************************************************/

var fostersgroup = window.fostersgroup || {};

fostersgroup = (function() {
	
	var loc = location.href;
	var BASE_URL = '';	
	if(loc.indexOf('fostersgroup.dev.areeba.com.au') == -1) {
		if((loc.indexOf('http://fostersgroup') <= 0) && (loc.indexOf('localhost') <= 0)) {
			var BASE_URL = '';
		} else {
			var BASE_URL = '/fostersgroup';
		}
	}
	
/*	var initFlash = function() {
		
		if ((typeof BASE_URL == 'undefined') || (typeof isRedDot != 'undefined')) return;
		if ((typeof isRedDotPreview != 'undefined')) return;
		
		var mapwidth = '911';
		var mapheight = '502';
		
		$('body.home-page #banner').css('visibility','visible');
		$('body.home-page #banner').flash(
			{
				src: BASE_URL + '/Common/flash/swfs/homepage-banner.swf',
				id: 'homepage-banner',
				width: '958',
				height: '425',
				wmode: 'transparent',
				scale: 'noscale',
				flashvars: {
					newsFeedURL: BASE_URL + '/news/homepage-banner.xml',
					productFeedURL: BASE_URL + '/Common/xml/productfeed.xml',
					popupMapURL: BASE_URL + '/Common/map.html',
					popupMapWidth: mapwidth,
					popupMapHeight: mapheight
				}
			},
			{ version: 9 }
		);
		
		$('body.home-page #map-content').flash(
			{
				src: BASE_URL + '/Common/flash/swfs/map.swf',
				id: 'homepage-banner',
				width: mapwidth,
				height: mapheight,
				wmode: 'transparent',
				scale: 'noscale',
				flashvars: {
					mapFeedXML: BASE_URL + '/Common/xml/mapfeed.xml'
				}
			},
			{ version: 8 }
		);
		
		
		$('#main-inner h1').flashText({
			src: BASE_URL + '/Common/flash/swfs/TheSansB3Light_Regular.swf'
		});
		$('div.product-wrapper div.productcontent h2').flashText({
			src: BASE_URL + '/Common/flash/swfs/TheSansB3Light_Regular.swf'
		});
		
		/*
		$('#content-inner h2').flashText({
			src: BASE_URL + '/Common/flash/swfs/HelveticaNeueMediumCond_Regular-Bold.swf'
		});
		$('#ui-dialog-title-map-dialog').flashText({
			src: BASE_URL + '/Common/flash/swfs/HelveticaNeueMediumCond_Regular-Bold.swf'
		});
			
	};*/
	
	var initFontReplace = function() {			
			if (typeof(Cufon) != 'undefined') 
			{ 			
				Cufon.replace('#main-inner h1');
				Cufon.replace('div.product-wrapper div.productcontent h2');			
				Cufon.replace('.cufon-replaced');							
			}
	};
	
	var addNavClasses = function() {
		$('#primary-navigation ul li:last-child').addClass('last');
	};
	
	var addJoinUsClasses = function() {
		if (typeof isRedDot != 'undefined') 
		{
			$("#join-us .join-us-inner").hover(
				function() {
					$(this).addClass('arb-hover'); 
				},
				function() {
					$(this).removeClass('arb-hover'); 
				}
			 );	
		}
		else
		{
		
			$("#join-us .join-us-inner").hover(
				function() {
					$(this).addClass('arb-hover').find('> ul').hide().animate({opacity: 'show', height: 'show'}, 100); 
				},
				function() {
					$(this).removeClass('arb-hover'); 
				}
			 );	
		 }
	};

	
	var mapOverlay = function() {
		// append HTML to body at the end // 
		// make this JS a dialog 
		// open the dialog
		
		if($("#map-dialog").length == 0){
			$('body').append('<div id="map-dialog"><iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com.au/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=Msac+melbourne&amp;sll=-25.335448,135.745076&amp;sspn=97.61881,58.798828&amp;ie=UTF8&amp;cid=15926256325971222262&amp;ll=-37.831277,144.963913&amp;spn=0.023727,0.036478&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe><br /><small class="larger-map-link"><a href="http://maps.google.com.au/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q=Msac+melbourne&amp;sll=-25.335448,135.745076&amp;sspn=97.61881,58.798828&amp;ie=UTF8&amp;cid=15926256325971222262&amp;ll=-37.831277,144.963913&amp;spn=0.023727,0.036478&amp;z=14&amp;iwloc=A" style="color:#0000FF;text-align:left">View Larger Map</a></small><div class="button-wrapper clearfix"><strong class="description">Find out Tram times:</strong><div class="map-button"><a class="image-replaced" href="http://jp.metlinkmelbourne.com.au">Metlink Journey Planner<span></span></a></div></div></div>');
			$("#map-dialog").css('display','none');
			/*
			$("#map-dialog").dialog({
				bgiframe: true,
				autoOpen: false,
				height: 470,
				width: 470,
				title:'How to find us',
				modal: true,
				resizable: false
			});
			*/
		}
		//$('#map-dialog').dialog('open');
	};
	
	
	var helperHover = function(el){
		 $(el).hover(
			function() { $(this).addClass('arb-hover ui-state-hover'); },
			function() { $(this).removeClass('arb-hover ui-state-hover'); }
		);
		 
	 };

	var pageControls = function(){
		arb.functions.fontSizing('#page-controls-box', {
			controlsFontSize:12,
			insertHTML: '<ul id="page-controls" class="clearfix"><li id="font-size-down-control" class="image-replaced" title="Decrease font size">A-<span></span></li><li id="font-size-up-control" class="image-replaced" title="Increase font size">A+<span></span></li><li class="print-link"><a href="#" title="Print page">Print Page</a></li></ul>'
		});
	};
	
	var searchableSelects = function(el){
		var $thisClass = $(el).attr('class');
		
		$(el).each(function()
		{
			var $thisClass = $(this).attr('class');
			/* Hide list element */
			$(this).css("display", "none");		
			/* Create new ID for the select, use existent Id of UL but add the _Select prefix */
			var ElId = $(this).attr("id") + "_Select";
			/* Create new Select-element */
			$(this).after("<select id='" + ElId + "' class='" + $thisClass + "'></select>");
			/* Loop-through list and add children to select */
			$(this).find("li").each(function()
			{
			/* Setup values */
			var Anchor = $(this).find("a").eq(0), Value = (Anchor.length > 0 ? $(Anchor).attr("href") : '');
			var SelectedElement = ($(this).hasClass("selectedItem") ? ' selected' : '');
			/* Add child to select */
			$("#"+ElId).append("<option value='"+Value+"'"+SelectedElement+">"+$(this).html()+"</option>");
			});
		});
		/* Hook selects */
		$("select." + $thisClass).change(function()
		{
			if(this.value.length > 0) window.location.href = this.value;
		});
	};
	
	
	var hideSearchTips = function() {
		$('#search-tips').hide();
		$('#toggle-search-tips').click(function () {
			$('#search-tips').toggle("slow");
			return false;
		});
	};
	
	/* To add rollovers and click states to <img> and <input type="image"> tags  <- wont' work with jquery
	var imageHelper = function(){
		$('#newsletter-subscribe input.button').imageHelper();
		$('#search-form input.button').imageHelper();
		$('#search-block-top input.searchbutton').imageHelper();
	};*/
	
	var categoryFilter = function(el){
		$(el).show();
		$('#all').attr('selected', 'selected');
		$('div.buttons-form-item', el).remove();
		$('select', el).change(function() {
			$('ul.section-index-list-news li:first').removeClass('first');
			var selected = $("option:selected", el);
			var output = "";
//			output = "You selected: <strong>" + selected.text() + "</strong>";
			output = selected.text();
			$("h2#selectedfilter").remove();
			$('ul.section-index-list-news').before("<h2 id='selectedfilter' style='display:none;'>" + output + "</h2>");
			$('ul.section-index-list-news').hide();
			$('ul.section-index-list-news li').removeClass('first');
			if(selected.val() != 'all'){
				$('ul.section-index-list-news li').show().not('.'+selected.val()).hide();				
			}else{
				$('ul.section-index-list-news li').show();
			}
			$('ul.section-index-list-news li:visible:first').addClass('first');
			$('#selectedfilter').show();
			$('ul.section-index-list-news').slideDown('slow');
		});
	};

	return  { // Everything that is returned in this object literal will be public
		
		init: function() {
			initSlideShow();
			pageControls();
			addNavClasses();
			addJoinUsClasses();
			hideSearchTips();
			helperHover('div.button a');
			$('div.category-download ul:first').addClass('first');
			arb.functions.linkButtons('div.buttons-form-item input');
			searchableSelects('#product-navigation ul.searchableSelect');
			searchableSelects('#wine-overview ul.searchableSelect');
			categoryFilter('#categoryfilter');
			initFontReplace();
			
			//imageHelper(); <- wont' work with jquery	
			//initFlash();			
			//mapOverlay();
		}
		
	};

})(); // Invoke the function creating a closure and assign the returned object literal



/**
 * Let's get it started
 */
$(document).ready(function() {
	fostersgroup.init();
});
