var FirstRun=true;
var rel;
var prevrel;
var Timer;
var PrevItem;
var CurItem=0;
var CurScroll=0;
var CurMapItem=1;
var ScrollAmount;
var DrawerUp=false;
var DrawrHover=false;


function InitScroller(){
	$('#InnTabs').html('');
	$('#RotationImage').css('background-image','url('+Images[0][2]+')');
	$('#RotationText .ButtonContent').html('<a href="'+Images[0][1]+'">Visit '+Images[0][0]+'</a>');
	$('#RotationStage li[rel="'+CurItem+'"]').css('background-color','#849d76');
	Timer=setTimeout('StartRotation()',WaitTime);
	for(i=0;i<Images.length;i++){
		$('#InnTabs').html($('#InnTabs').html()+'<li id="Item_'+i+'" style="background-color:'+Images[i][3]+'"><p>'+Images[i][0]+'</p></li>');
	}
	$('#InnTabs li').css('opacity',.6);
	ScrollAmount = $('#InnTabs li:first').width();
	$('#InnTabs #Item_'+CurItem).css('opacity',1);
	$('#InnTabs li').click(
		function(){
			var ID=$(this).attr('id');
			ID=ID.replace('Item_','');
			OnHold=true;
			StartRotation(ID);
			clearTimeout(Timer);
			setTimeout('clearHold()',8000);
		}
	);
	if(Images.length>5){
		PrepPaging();
	}
	else{
		$('#BannerRight').css('opacity',.3);
		$('#BannerLeft').css('opacity',.3);
	}
	SortPageButtons();
}

function clearHold(){
	OnHold=false;
	Timer=setTimeout('StartRotation()',WaitTime);
}

function PrepPaging(){
	$('#BannerLeft').click(
		function(){
			if(CurScroll>0){
				$('#InnTabs').animate(
					{
						left	:	'+='+ScrollAmount+'px'
					}
					,500
				);
				CurScroll-=ScrollAmount;
				SortPageButtons();
			}			
		}
	);
	$('#BannerRight').click(
		function(){
			if(CurScroll<(ScrollAmount*(MaxItem-5))){
				$('#InnTabs').animate(
					{
						left	:	'-='+ScrollAmount+'px'
					}
					,500
				);
				CurScroll+=ScrollAmount;
				SortPageButtons();
			}	
		}
	);	
}

function SortPageButtons(){
	if(CurScroll>=(ScrollAmount*(MaxItem-5))){
		$('#BannerRight').css('opacity','.3');
		$('#BannerRight').removeClass('current');		
	}
	else{
		$('#BannerRight').css('opacity','.8');
		$('#BannerRight').addClass('current');
	}
	if(CurScroll<=0){
		$('#BannerLeft').css('opacity','.3');
		$('#BannerLeft').removeClass('current');		
	}
	else{
		$('#BannerLeft').css('opacity','.8');
		$('#BannerLeft').addClass('current');		
	}

	$('#BannerRight.current').hover(function() {
		$('#BannerRight.current').css('opacity','1');
	},function() {
		$('#BannerRight.current').css('opacity','.8');
	});	

	$('#BannerLeft.current').hover(function() {
		$('#BannerLeft.current').css('opacity','1');
	},function() {
		$('#BannerLeft.current').css('opacity','.8');
	});		
}

function ChangeText(x){
	
	$('#RotationText').stop().animate(
		{
			opacity: '0'
		}
		,500
		,function(){
			$('#RotationText .ButtonContent').html('<a href="'+Images[CurItem][1]+'">Visit '+Images[CurItem][0]+'</a>');
			$('#RotationText').animate(
					{
						opacity: '1'
					}
					,500
				)
			}		
		)
}

function StartRotation(x){
	if(x){
		CurItem=parseInt(x);
		PrevItem=CurItem-1;
		if (PrevItem<0){
			PrevItem=(Images.length-1);
		}
	}
	if(!OnHold){		
		clearTimeout(Timer);
		CurItem++;
		if (CurItem>(Images.length-1)){
			CurItem=0;
		}
		PrevItem=CurItem-1;
		if (PrevItem<0){
			PrevItem=(Images.length-1);
		}
	}
	ChangeText(CurItem);
	
	//Check to see if we need to scroll the paging..

	var ActualPos=((CurItem+1)*ScrollAmount)-(5*ScrollAmount);
	if(ActualPos > CurScroll){
		$('#BannerRight').click();
		SortPageButtons();
	}
	
	if((((CurItem+1)*ScrollAmount)-(5*ScrollAmount)) < CurScroll && CurScroll!=0){
		$('#InnTabs').animate(
			{
				left	:	'0px'
			}
			,500
		);
		CurScroll=0;
		SortPageButtons();
	}
	
	
	//console.log(PrevItem);
	$('#RotationImageBG').css('background-image','url('+Images[PrevItem][2]+')');
	$('#RotationImage').fadeOut(
		500,
		function(){
			$('#RotationImage').css('background-image','url('+Images[CurItem][2]+')');
			$('#RotationImage').fadeIn(500);
			$('#InnTabs li').css('opacity',.6);
			$('#InnTabs #Item_'+CurItem).css('opacity',1);
		}
	);
	//$('#RotationStage li').css('background-color','#c4bea8');
	//$('#RotationStage li[rel="'+CurItem+'"]').css('background-color','#849d76');
	if(!OnHold){
		Timer=setTimeout('StartRotation()',WaitTime);
	}
	
}

$(document).ready(
	function(){
		PopulateArray();
		if(PageID=='Home'){			
			InitScroller();
		}
		
		InitMapScroller();
		InitMap();
		if(PageID=='Contact'){
			InitMainMap();
		}
		if(PageID=='NewsView'){
			$('.page-item-16').addClass('current_page_item');
		}
	}
)

function InitMapScroller(){
	$('#MapScroller #LeftArrow').css('opacity','.5');
	$('#MapScroller #LeftArrow').click(
		function(){
			if(CurMapItem>1){
				$('.LocationScroller').animate(
					{
					'left'	:	'+=200px'
					}
					,500
				)
				CurMapItem--;
				if(CurMapItem<=1){
					$('#MapScroller #LeftArrow').css('opacity','.5');
				}
				else{
					$('#MapScroller #LeftArrow').css('opacity','1');
				}
			}
			$('.MapMarker').removeClass('On');
			$('#Icon'+CurMapItem).addClass('On');
		}
	);
	$('#MapScroller #RightArrow').click(
		function(){
			if(CurMapItem<MaxItem){
				$('.LocationScroller').animate(
					{
					'left'	:	'-=200px'
					}
					,500
				)
				CurMapItem++;
				if(CurMapItem>=MaxItem){
					$('#MapScroller #RightArrow').css('opacity','.5');
				}
				else{
					$('#MapScroller #RightArrow').css('opacity','1');
				}
			}
			$('.MapMarker').removeClass('On');
			$('#Icon'+CurMapItem).addClass('On');
		}
	);
	$('.MapMarker').removeClass('On');
	$('#Icon'+CurMapItem).addClass('On');
	
	$('.MapMarker').click(
		function(){
			var ID=$(this).attr('id');
			ID = ID.replace('Icon','');
			if (CurMapItem<ID && CurMapItem<MaxItem){ //ScrollRight
				var Diff=ID-CurMapItem;
				$('.LocationScroller').animate(
					{
					'left'	:	'-='+(Diff*200)+'px'
					}
					,500
				)
				CurMapItem+=Diff;
				if(CurMapItem>=MaxItem){
					$('#MapScroller #RightArrow').css('opacity','.5');
				}
				else{
					$('#MapScroller #RightArrow').css('opacity','1');
				}
				$('.MapMarker').removeClass('On');
				$('#Icon'+CurMapItem).addClass('On');
			
			}
			if (CurMapItem>ID && CurMapItem>1){
				var Diff=CurMapItem-ID;
				$('.LocationScroller').animate(
					{
					'left'	:	'+='+(Diff*200)+'px'
					}
					,500
				)
				CurMapItem-=Diff;
				if(CurMapItem>=MaxItem){
					$('#MapScroller #RightArrow').css('opacity','1');
				}
				else{
					$('#MapScroller #RightArrow').css('opacity','.5');
				}
				$('.MapMarker').removeClass('On');
				$('#Icon'+CurMapItem).addClass('On');
			}
		}
	);
	
}

function InitMainMap(){
	map2 = new GMap2(document.getElementById("MapMain"));
	map2.setUIToDefault();
	map2.setCenter(new GLatLng(54.237602, -2.694576), 7);
	
	var ii=MaxItem;
	var MyMarkers = new Array();
	while(ii--){
		MyMarkers[ii] = new Array(3);
		MyMarkers[ii][0]=Images[ii][4];
		MyMarkers[ii][1]=Images[ii][5];
		Images[ii][6] = Images[ii][6].replace('<![CDATA[','')
		Images[ii][6] = Images[ii][6].replace(']]>','')
		var icon3 = new GIcon();
		icon3.image = "http://innsofcharacter.co.uk/wp-content/themes/InnsParentTheme/images/MapMarkerLarge.png";
		icon3.iconSize = new GSize(32, 32);
		icon3.iconAnchor = new GPoint(16, 32);
		icon3.infoWindowAnchor = new GPoint(16, 32);
		
		var point = new GLatLng(Images[ii][5],Images[ii][4]);
		var markerPin = new GMarker(point,icon3);		
		markerPin.title=Images[ii][0];
		markerPin.id=ii;
		markerPin.address=Images[ii][6];
		map2.addOverlay(markerPin);	

		GEvent.addListener(markerPin, "click", function(point) {
			var htmlData = '<h1>'+Images[this.id][0]+'</h1><p>'+Images[this.id][6]+'</p><div class="Button"><a href="'+Images[this.id][1]+'">View</a></div>';
			$('#ContactInfo').html(htmlData);
		});		
	}
	var htmlData = '<h1>'+Images[0][0]+'</h1><p>'+Images[0][6]+'</p><div class="Button"><a href="'+Images[0][1]+'">View</a></div>';
	$('#ContactInfo').html(htmlData);
	//GEvent.addListener(map2, "click", function() {
	//	alert(map2.getBounds())
	//});
	
	

	
}

function InitMap(){
	
}

