$(document).ready(function(){
	EyeHub = new OPSM.EyeHub();
});

var OPSM = (OPSM==undefined) ? {} : OPSM;

OPSM.EyeHub = function(){ 

	this.init = function() {
		this.topNav();
		this.textResize();
		this.cssEnhancements();
		this.brandListCarousel();
		this.specialOffersCarousel();
		this.tabs();
		
		$(".js_only").show();
		$("#ptPrint").click(function() {
			window.print();
			return false;
		})
		
		Reveals = new OPSM.EyeHub.RevealMe();
		Reveals.init();
	}

	this.topNav = function() {
		var config = {    
			sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
			interval: 0, //polling interval  
			over: function() {
				$(this).find(".subNav").fadeIn('fast');
			}, 
			timeout: 200, // number = milliseconds delay before onMouseOut    
			out: function(){
				$(this).find(".subNav").fadeOut('fast');
			}
		};
	
		$("#nav > ul li").hoverIntent( config );
	};
	
	
	this.cssEnhancements = function() {
		$("#pageTools li:last-child").addClass("last-child");
		$("#footer > ul > li:first-child").addClass("first-child");
		
		$("#newsList > ul > li:odd").addClass("odd");
	};
	
	this.tabs = function() {
		
		EyeHubMaps = new OPSM.EyeHub.Mapping();
		$("#storeLocator").tabs({
			show: function(e, ui) {
				EyeHubMaps.initMap($(ui.tab).attr('rel'));
			}
		});

		$(".tabbedSection").tabs({ });

		//var pageOptions = { items_per_page : 4, slideTransition: true };
		var pageOptions = { items_per_page : 4};
		that = this;
		//iterate through each tab panel and create unique pagers
		$(".tabsContainer .ui-tabs-panel").each(function(k,v) {
			that.paginate($(this), pageOptions);
		});
		
	};
	
	this.brandListCarousel = function() {
		$("#brandListing .tabCarouselList").jCarouselLite({
			visible:4,
			btnNext:'.tabCarousel .next',
			btnPrev:'.tabCarousel .prev',
			easing:'easeInOutQuad',
			speed: 800,
			scroll:2
		});
	};
	this.specialOffersCarousel = function() {
		$("#so_carousel .theCarousel").jCarouselLite({
			visible:3,
			btnNext:'#so_carousel .next',
			btnPrev:'#so_carousel .prev',
			easing:'easeInOutQuad',
			speed: 800,
			scroll:2
		}).addClass('jCarousel');
	};
		
	this.textResize = function(strSize) {
		var COOKIE_NAME = 'eyehub_tr',
				PREFIX = 'textSize_',
				options = { path: '/', expires: 10 },
				defaultSize = 2;
				
		for (var i=0;i<6;i++) {
			$('body').removeClass(PREFIX+i.toString());
		}
		if (strSize == undefined) {
			//get the current cookie and set the body css
			var cookie_val = ($.cookie(COOKIE_NAME) != '' && $.cookie(COOKIE_NAME) != undefined) ? $.cookie(COOKIE_NAME) : defaultSize;
			$('body').addClass(PREFIX+(cookie_val).toString());
			$.cookie(COOKIE_NAME, cookie_val, options);

			$("#ptTextSizeSmaller").click(function(){
				cookie_val = parseInt($.cookie(COOKIE_NAME));
				cookie_val = cookie_val >= 2 ? (cookie_val-1).toString() : '1';
				EyeHub.textResize(cookie_val);
			});
			
			$("#ptTextSizeLarger").click(function(){
				cookie_val = parseInt($.cookie(COOKIE_NAME));
				cookie_val = (cookie_val <= 4) ? (cookie_val+1).toString() : '5';
				EyeHub.textResize(cookie_val);
			});
		
		}
		else {
			$.cookie(COOKIE_NAME, strSize, options);
			$('body').addClass(PREFIX+strSize);
		}

		
	};
	
	this.paginate = function(containerSelector, options) {
		theOptions = {
										num_page_links_to_display : 5,
										items_per_page : 5,
										item_container_id : '.paged_content',
										slideTransition: false,
										slideDuration:1400,
										slideEasing:"easeInOutQuint"
									};
		theOptions = $.extend(theOptions, options);
		
		$(containerSelector).pajinate(theOptions);
	}


	this.init();
}


OPSM.EyeHub.RevealMe = function() {
	
	this.options = { }
	
	this.init = function() {
		var that = this;
		
		
		
		//find revealer sections, triggers and content and add event handlers etc.
		$(".revealers .revealContent").hide();
		
		$(".revealers .revealTrigger").click(function() {
			$next = $(this).next(".revealContent");
			$next.slideToggle("normal", function() {
				$prev = $(this).prev(".revealTrigger")

				if ($(this).css("display") == 'none') {
					$prev.removeClass("active");
					
				}
				else {
					$prev.addClass("active");
				}
			});
			
		});
		
	}
	
}

OPSM.EyeHub.Mapping = function() {
	this.initMap = function(loc) {
		this.directionsDisplay = new google.maps.DirectionsRenderer();
		this.directionsService = new google.maps.DirectionsService();
		var that = this;
		$("#calc_route").click(function(){ that.calcRoute(); });
		//$("#swap").click(function(){ that.swapDirectionValues(); });

		var latLn = new google.maps.LatLng(-33.843387, 151.212224);
		var address = '';
		var title = '';
		//if (loc == undefined) { loc = 'sydney'; }
		switch(loc) {
			case "sydney":
				latLn = new google.maps.LatLng(-33.843387, 151.212224);
				break;
			case "melbourne":
				latLn = new google.maps.LatLng(-37.643387, 145.612224);				
				address = '174-176 Burwood Road, Hawthorn, 3122, Victoria, Australia';
				title = "OPSM eyehub Melbourne"
				break;
			case "brisbane":
				latLn = new google.maps.LatLng(-27.243387, 153.212224);
				break;
			default: 
				latLn = new google.maps.LatLng(-33.843387, 151.212224);
				break;
		}
		var myOptions = {
			zoom: 15,
			center: latLn,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			mapTypeControl: true,
			mapTypeControlOptions: {
					style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
					position: google.maps.ControlPosition.TOP
			},
			navigationControl: true,
			navigationControlOptions: {
					style: google.maps.NavigationControlStyle.DEFAULT,
					position: google.maps.ControlPosition.TOP_RIGHT
			},
			scaleControl: false
		}
		var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
		
		geocoder = new google.maps.Geocoder();
		
		if (geocoder) {
			geocoder.geocode( { 'address': address}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					map.setCenter(results[0].geometry.location);
					var marker = new google.maps.Marker({
						map: map, 
						position: results[0].geometry.location,
						title: title
					});
				}
				else {
					//FAIL 
					//alert("Geocode was not successful for the following reason: " + status);
				}
			});

		}		
	
		this.directionsDisplay.setMap(map);
		
	}
	
	this.initialize = function(loc) {
			var latLn;
			//if (loc == undefined) { loc = 'sydney'; }
			switch(loc) {
				case "sydney":
					latLn = new google.maps.LatLng(-33.843387, 151.212224);
					break;
				case "melbourne":
					latLn = new google.maps.LatLng(-37.821870, 145.028851);
					break;
				case "brisbane":
					latLn = new google.maps.LatLng(-27.243387, 153.212224);
					break;
				default: 
					latLn = new google.maps.LatLng(-28.643387, 153.612224);
					break;
			}
			var myOptions = {
				zoom: 8,
				center: latLn,
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				mapTypeControl: true,
				mapTypeControlOptions: {
						style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
						position: google.maps.ControlPosition.TOP
				},
				navigationControl: true,
				navigationControlOptions: {
						style: google.maps.NavigationControlStyle.ZOOM_PAN,
						position: google.maps.ControlPosition.TOP_RIGHT
				},
				scaleControl: false
			}
			var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		
	}
	
	this.swapDirectionValues = function(locStartId, locEndId){
		var start, end;
		start = $("#location_start").val();
		end = $("#location_end").val();
		$("#location_start").val(end);
		$("#location_end").val(start);
		// if the route has been calculated at least once,
		// then do it straight away when we swap.
		if (this.routeCalculated) { this.calcRoute(); }
	}	
	this.calcRoute = function() {
		var start = document.getElementById("location_start").value;
		var end = document.getElementById("location_end").value;
		var that = this;
		var request = {
			origin:start, 
			destination:end,
			travelMode: google.maps.DirectionsTravelMode.DRIVING
		};
		
		this.directionsService.route(request, function(result, status) {
			if (status == google.maps.DirectionsStatus.OK) {
				that.directionsDisplay.setDirections(result);
			}
		});
		this.routeCalculated = true;
	}	
}




/*	we're just using a slightly simplified wrapper for SWFObject */
OPSM.EyeHub.Flash = function(theOptions) {

	this.init = function() {
		this.options =  $.extend(this.options,theOptions);
		
	}
	
	this.options =  {
		path: '',
		embedInto: '',
		width: 0,
		height: 0,
		flashvars: {},
		params: {
			wmode:"transparent"
		},
		expressInstallPath: '/public/flash/expressInstall.swf',
		version:"8.0",
		attributes: { },
		callbackFn: null
	
	}
	
	this.embed = function() {
		o = $.extend(this.options,theOptions);
		swfobject.embedSWF(o.path, o.embedInto, o.width, o.height, o.version, o.expressInstallPath, o.flashvars, o.params, o.attributes, o.callbackFn);
		
	}
	
	this.init();
}


