var initialised = 0;
var slides_on = 1;
var i=1;
var sponsors = new Array(20);

var images=['image/photo1.jpg','image/photo2.jpg','image/photo3.jpg','image/photo4.jpg',
				'image/photo5.jpg'];

function init_sponsors()
{
    sponsors[0] = ["http://www.grahamclarke.co.uk","image/festival_logo.gif",0];
    sponsors[1] = ["http://www.maidstone.gov.uk/","image/mbc_logo.gif",0];
    sponsors[2] = ["http://www.goldinghomes.org.uk/main.cfm","image/gh_icon.gif",0];
    sponsors[3] = ["http://www.kentideas.co.uk/","image/kent_ideas.jpg",0];
    sponsors[4] = ["http://www.waterforwork.co.uk/","image/water_for_work.gif",0];
    sponsors[5] = ["http://www.strata-online.com/","image/strata.jpg",0];
    sponsors[6] = ["http://www.medwaybridgemarina.co.uk/","image/medway_bridge_marina.jpg",0];
    sponsors[7] = ["http://www.wickes.co.uk/","image/wickes.gif",0];
    sponsors[8] = ["http://www.fremlinwalk.co.uk/","image/fremlin_logo.gif",0];
    sponsors[9] = ["http://www.maidstonescaffolding.co.uk/","image/mstone_scaffolding.gif",0];
    sponsors[10] = ["http://www.frankieandbennys.com/","image/f_bennys.gif",0];
    sponsors[11] = ["http://www.wardhomes.co.uk/","image/ward_homes.gif",0];
    sponsors[12] = ["http://www.kentishlady.co.uk/","image/kl.gif",0];
    sponsors[13] = ["http://www.spotsvstripes.com/default.aspx", "image/spots_v_stripes_logo.gif",0];
    sponsors[14] = ["http://the-barge.sm4.biz/","image/the_barge.gif", 0];
    sponsors[15] = ["http://www.topsecurityuk.com/","image/top_security.gif", 0];
    sponsors[16] = ["http://www.mangravetgoldshop.com/", "image/mangravetgoldshop.gif", 0];
    sponsors[17] = ["http://www.sbcarandvanhire.co.uk/","image/sandbcarhire.gif",0];
    sponsors[18] = ["http://www.latasca.co.uk/","image/la_tasca.gif",0];
    sponsors[19] = ["http://www.countrystylegroup.co.uk/","image/countrystyle.gif",0];
}

function v_banner()
{
    var count = 0;
    var i;
    while(count < 20)
    {
        i = Math.floor(Math.random()*20);
        if(sponsors[i][2] == 0)
        {
            if(count < 6)
            {
                $("#v_banner").append("<div class='scroll_box'><a href='" + sponsors[i][0] + "'><img src='" + sponsors[i][1] + "' /></a></div>");
            }
            else
            {
                $("#v_banner").append("<div class='scroll_box' style='display: none;'><a href='" + sponsors[i][0] + "'><img src='" + sponsors[i][1] + "' /></a></div>");
            }
            sponsors[i][2] = 1;
            count++;
        }
    }
}

function remove_spaces(str){
	var word = str.split(" ");
	var result = word[0];
	for(var i=1; i<word.length; i++){
		result += word[i];
	}
	return result;
}

function show_event(event){
	$('#right_top').empty();
	$('#right_top').html('<img src="image/loading.gif" style="width: 41px; margin-left: 160px; margin-right: auto; margin-top: 45px" />');
	$.ajax({type: 'GET', url:'events/' + event + '.txt', dataType: 'text', success: function(content){
			$('#right_top').html(content);
		}
	});
}

function show_slides(){
	$('#map_canvas').css('display', 'none');
	$('#photo_box').css('display', 'block');
	slideshow();
}

function slideshow(){	
	if(slides_on){
		setTimeout('fade_image(i)',5000);
	}
}

function show_map(){
	$('#map_canvas').css('display', 'block');
	//show_event('eventHighlights');
	if(initialised==0){
		initialise_map();
		initialised=1;
	}
	slides_on=0;
	$('#photo_box').css('display', 'none');
}

function fade_image(index){
	$('#photo').fadeOut('300',function(){
		$(this).attr('src',images[index]);
		$('#photo_count').text('Photo ' + (i + 1) + ' of 5');
		if(i==4){i=0;}else{i++;}
		if(slides_on){
			$(this).fadeIn('300',slideshow);
		}
	});
}

function initialise_map(){
	var latlng = new google.maps.LatLng(51.276000, 0.520666);
	var myOptions = {
	  zoom: 14,
	  center: latlng,
	  mapTypeId: google.maps.MapTypeId.TERRAIN
	};
	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	var titles = ['Brenchley Gardens','Floating Soundstage', 
		'All Saints Sound Stage','Town Centre', 'Lockmeadow Centre', 'Whatman Park', 'Kentish Lady','The Barge','La Tasca','Fremlin Walk'];
	var positions = [[51.276217,0.520113],[51.274226,0.518138],
					[51.271189,0.521207],[51.275045,0.522516],[51.270075,0.518305],[51.282031,0.513697],[51.271608,0.518632],[51.270501,0.520241] ,[51.274622,0.52041]
                    ,[51.275381,0.520788]];
	var icons = ['icon_1.png','icon_2.png','icon_3.png','icon_4.png','icon_5.png','icon_6.png','boat.png','knife_fork.png','knife_fork.png','shopping.gif'];
	var marker = [];
	for(var i=0; i < 10; i++){
		var lat_lng = new google.maps.LatLng(positions[i][0],positions[i][1]);
		marker[i] = new google.maps.Marker({
			position: lat_lng,
			map: map,
			title: titles[i]
		});
		marker[i].setIcon("image/marker/" + icons[i]);
		google.maps.event.addListener(marker[i],'click',function() {
			show_event(remove_spaces(this.getTitle()));
		});
	}
    google.maps.event.addListener(marker[6], 'mouseover' , function() {
        show_event(remove_spaces(this.getTitle()));
    });
    google.maps.event.addListener(marker[7], 'mouseover' , function() {
        show_event(remove_spaces(this.getTitle()));
    });
    google.maps.event.addListener(marker[8], 'mouseover' , function() {
        show_event(remove_spaces(this.getTitle()));
    });
    google.maps.event.addListener(marker[9], 'mouseover' , function() {
        show_event(remove_spaces(this.getTitle()));
    });
}

function get_twitter_feed(posts) {
	
	var items = 0;
	//use the JQuery get to grab the URL from the selected item, put the results in to an argument for parsing in the inline function called when the feed retrieval is complete
	$.ajax({type: "GET", url:"twitterproxy.asp", dataType: "xml", success: function(xml){
		$('#twitter').empty();
		$('#twitter').append('<ul>');
		//find each 'item' in the file and parse it
		$(xml).find('item').each(function() {
			if(items==posts){
				$('#twitter').append('</ul>');
				return false;
			}
			var item = $(this);
			var title = item.find('title').text();
			var link = item.find('link').text();
			var date = item.find('pubDate').text().split(' ');
			var html = "<li><h1>"+date[0] + " " + date[1] + " " + date[2] + " " + date[3] + "</h1><a href=\"" + link + "\">" + title + "<\/a></li>\n"; 
			$('#twitter').append(html);
			items++;
		});
	}});
}

function countdown(){
	var today = new Date();
	var months = 6 - today.getMonth(); //calculate months.
	var day = today.getDate();
	var weeks = 0;
	day += 7;
	if(day == 38){
		months--;
		weeks = 4;
		days = 2;
	}
	else{
		while(day <= 30){ //calculate weeks to go.
			weeks++;
			day += 7;
		}
		day -= 7;
		var days = 30 - day; //calculate days.
	}
	if(days == 1){
		$('#countdown p').text().replace('days', 'day');
	}
	if(weeks == 1){
		$('#countdown p').text().replace('weeks', 'week');
	}
	if(months == 1){
		$('#countdown p').text().replace('months', 'month');
	}
	$('#days').html(days + ' ');
	$('#weeks').html(weeks + ' ');
	$('#months').html(months + ' ');
}
