// JavaScript Document

var rotateRate = 5000;
var thumb1_ar = ['frame1_img.jpg','frame1_img1.jpg','frame1_img2.jpg'];
var thumb2_ar = ['frame2_img.jpg','frame2_img1.jpg','frame2_img2.jpg'];

$(function() { 
	   
	
	$("#mainMenu ul li").bind( 'mouseover', function(){ $(this).css('background-image','url(layout/menu_bg_over.jpg)');$(this).css('background-repeat','repeat-x'); } );
	$("#mainMenu ul li").bind( 'mouseout', function(){ $(this).css('background-image',''); } );
	
	$('.rotate_7').easyRotate({degrees:3});
	$('.rotate_5').easyRotate({degrees:-7});
	
	$("#homepageImg1_inner").geoffsFlipper({
		imageArray: ['frame1_img.jpg','frame1_img1.jpg','frame1_img2.jpg'],
		timeDelay : 6500,
		startDelay: 2000
	});
	$("#homepageImg2_inner").geoffsFlipper({
		imageArray: ['frame2_img.jpg','frame2_img1.jpg','frame2_img2.jpg'],
		timeDelay : 5000
	});
	$("#homepageImg3_inner").geoffsFlipper({
		imageArray: ['frame1_img.jpg','frame1_img1.jpg','frame1_img2.jpg'],
		timeDelay : 6500,
		startDelay: 2000
	});
	
	$('#unitCatBox li, #unitCatBoxClickable li').bind( 'mouseover', function(){ $(this).css('background-color','#256a38'); });
	$('#unitCatBox li, #unitCatBoxClickable li').bind( 'mouseout', function(){ $(this).css('background-color',''); });
	$('#unitCatBox li').bind( 'click', function(){ 
		if ( $(this).hasClass('selected') ) {
			$("#unitCatBox li").removeClass('selected');
			$("#catInput").val( '' );
		} else {
			$("#unitCatBox li").removeClass('selected');
			$(this).toggleClass('selected');
			$("#catInput").val( $(this).attr('value') );
		}
		updateSearchResults();
	//	var cats = $('#catInput').val().split(",");
	//	cats.push($(this).attr('value'));
	//	$("#catInput").val( cats.join(",") );
	});
	$("#sortBox .sortOption").bind( 'click', function() {
		$("#sortBox span").removeClass('selected');
		$(this).addClass('selected');
		$("#sortBy").val( $(this).attr('value') );
		updateSearchResults();
	} );
	$("#sortDESC").bind( 'click', function() {
		$("#sortorder").val("desc");
		updateSearchResults();
	} );
	$("#sortASC").bind( 'click', function() {
		$("#sortorder").val("asc");
		updateSearchResults();
	} );

	$("input[autocomplete]").each( function() {
		var el = $(this);
			$(el).autocomplete({
			source: "autocompleteData.php?field="+$(el).attr('name'),
			minLength: 3,
			select: function( event, ui ) {
				$(this).val( ui.item.value );
			}
		} );
	} );
	
	$("#thumbContainer span img").bind( 'click', function() { 
		$("#mainImgFrame img").attr('src',$(this).attr('src')); 
	} );
	
	//$("#accordion").accordion({autoHeight:false});
	$("#tabs").tabs();
	$(".sortable").sortable();
	
	$(".ui-button").bind( 'mouseenter', function(){ $(this).addClass('ui-state-hover'); } );
	$(".ui-button").bind( 'mouseleave', function(){ $(this).removeClass('ui-state-hover'); } );
	

} );

var path = "userfiles/image/";

	$.fn.extend({
		geoffsFlipper: function(settings) {
			var obj = this;
			// default config 
			var defaults = jQuery.extend({
				fadeInDuration: 1000,
				fadeOutDuration: 200,
				timeDelay: 5000,
				startDelay: 0,
				fadeType: 'swing',
				path : "layout/",
				imageArray: []
			},settings);

			var options = $.extend(defaults, options);
			
			this.flip = function() {
				
				if (  obj.find("img:visible").attr('src') == options.path+(options.imageArray[options.imageArray.length-1]) ) {
					obj.find("img:visible").fadeOut(options.fadeOutDuration);
					obj.find("img:first").fadeIn(options.fadeInDuration);
				} else { 
					obj.find("img:visible").fadeOut(options.fadeOutDuration).next().fadeIn(options.fadeInDuration);
				}
				
			}

			return this.each(function () {
				if ( options.imageArray.length > 0 ) {
					for ( var i = 0; i < ( options.imageArray.length ); i++ ) {
						$("<img>").attr('src',options.path+options.imageArray[i]).hide().appendTo($(obj));
					}
					obj.find("img:first").show();
					//console.log( "Setting... " );
					window.setTimeout(function() {
						window.setInterval(function(){obj.flip();}, options.timeDelay);
					}, options.startDelay);
				}
			});
		}
	} );

function startHeaderAnimation() {
	setTimeout( function() {
		$("#header_telephone").animate({ left: 405 }, {queue:false,duration:500,easing:'easeOutQuad'}).fadeIn(1000); 
	}, 500 );
	setTimeout( function() {
		$("#header_hours").show().animate({ top: 120 }, {queue:false,duration:750,easing:'easeOutExpo'});
	}, 500 );
	setTimeout( function() {
		$("#header_address").show().animate({ top: 20 }, {queue:false,duration:750,easing:'easeOutExpo'});
	}, 850 );
}

function updateSearch() {
	$("#start").val('0');
	updateSearchResults();
}

function updateSearchResults() {
	$("#searchResultsBox").fadeTo('fast',0.2);
	var postAr = new Array();
	$(".searchField").each( function() {
		if ( $(this).attr('value') != "" ) {
			postAr.push($(this).attr('name') + "=" + $(this).attr('value') );
		}
	} );
	postAr.push("action=searchUnits");
	//console.log( postAr.join("&") );
	$.ajax( {
		url : "ajax.php",
		type : 'post',
		data : postAr.join("&"),
		success: function ( msg ) {
			$("#searchResultsBox").html( msg );
			$("#searchResultsBox").fadeTo('slow',1);
		}
	} );
}

function flipPage( start ) {
	$("#start").val( start );
	updateSearchResults();
}

function loadInfo(el) {
	var el2 = $(el).clone().css('width','620px').attr('id','catDescFull');
	$(el2).find(".catDescDesc").css('display','block');
	$("#catDescFull").remove();
	$(el).parent().after(el2);
}
function addToNewsletter() {
	var email = $("#newsletterEmail").val();
	$.ajax({
		url : '/ajax.php',
		type : 'post',
		data : 'action=addNewsletter&email='+email,
		success : function( msg ) {
			alert( "Your email has been successfuly added to our mail list. Thank You." );
		}
	});
}
function printFlyer( ) {
	var url = '';
	var head = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link href="'+url+'css/printstyles.css" rel="stylesheet" type="text/css" media="print" /><link href="'+url+'css/textstyles.css" rel="stylesheet" type="text/css" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Glenwood RV</title></head><body>';
	var foot = '</body></html>';
	var content = $("#printarea").html();
	WinPrint = window.open('','','left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
	WinPrint.document.write(head);
	WinPrint.document.write(content);
	WinPrint.document.write(foot);
	WinPrint.document.close();
	WinPrint.focus();
	WinPrint.print();
	WinPrint.close();
	
}

function initializeMap()
{
	var minLat = 80;
	var maxLat = 30;
	var minLong = -50;
	var maxLong = -150;

	var centerLatitude = minLat + (maxLat - minLat) / 2  
	var centerLongitude = minLong + (maxLong - minLong) / 2  
//	console.log( maxLong );
//	console.log( minLong );
//	console.log( minLat );
//	console.log( maxLat );
//	console.log( centerLatitude );
//	console.log( centerLongitude );
	var latitude1 = minLat;
	var latitude2 = maxLat;
	var longitude1 = minLong;
	var longitude2 = maxLong;
	
	var locations = new Array( new Array(52.160803382709, -106.6710877418518, "Saskatoon", "Glenwood RV Centre", "<hr />502 Cynthia Street<br />Saskatoon, SK<br />(306)242-9028") );	
	var zoom = 16;
	
	var map = new GMap2(document.getElementById("googleMap"));
		map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 10)));	
		map.setCenter(new GLatLng(52.160803382709, -106.6710877418518), zoom, G_HYBRID_MAP );
		map.addControl(new GSmallMapControl());
	
	if ( locations.length > 0 ) {
		for( var i = 0; i < locations.length; i++ )
		{
			var params = new Object;
			//showAddress( locations[i][0], locations[i][1], locations[i][0] );
			var point = new GLatLng( locations[i][0], locations[i][1] );
			
			params.infoTitle = locations[i][3];
			params.infoAddress = locations[i][2];
			params.infoDesc = locations[i][4];
			//params.icon = new_icon;
				
			var marker = new GMarker(point,params);
				marker.infoTitle = locations[i][3];
				marker.infoAddress = locations[i][2];
				marker.infoDesc = locations[i][4];
			map.addOverlay( marker );
		}
	
	
	
		GEvent.addListener(map, "click", function(marker, point) {
		  if (marker)
			marker.openInfoWindowHtml("<h1 style='padding: 0px;'>"+ marker.infoTitle + "</h1><div style='width: 300px;'>"+ marker.infoDesc +"</div>");
		});
	}
}


