$(function() {
	
	updateSizeFromCookie();
    
    if ($("#home-image-link-8027").length ) {
        $("#home-image-link-8027").attr('href', "#TB_inline?height=360&width=445&inlineId=hiddenModalContent");
        $("#home-image-link-8027").addClass("thickbox");
        tb_init('a.thickbox');
    }

	
	setInterval(slideSwitch, 5000 );
	
	$('#sizer a').click(function() {
		createCookie('size', this.className);
        updateSizeFromCookie();
		return false;
	});
	
	$('#middle *:first').addClass('first');
	
	$('input').each(function() { $(this).addClass(this.type); });
	$('#buttons img').simpleImageRollover();
	
	var cancelHide = false;
	$('#sciaf-needs-you > li').click(function() {
	    $('ul', this).slideDown('fast');
	    cancelHide = true;
	}).hover(function() {
		cancelHide = true;
	}, function() {
		cancelHide = false;
		var self = this;
		setTimeout(function() {
			if (!cancelHide) {
				$('ul', self).slideUp('fast');
			}
		}, 500);
	});
	
    var mapContainer = $('#where-we-work-map');
	if (mapContainer.length && GBrowserIsCompatible()) {
	    
	    var coords = [];
	    $('.coords li a', mapContainer).each(function() {
	        var split = $(this).text().split(',');
	        coords.push([split[0], split[1], this.getAttribute('title'), this.getAttribute('href')]);
	    });
	    $('.coords', mapContainer).remove();
	    
	    var map = new GMap2(mapContainer[0]);
		map.addControl(new GMapTypeControl());
		map.addControl(new GLargeMapControl());
	
        map.setCenter(new GLatLng(43.580391,15.46875), 1);

	    function mkMarker(a) {
	        var p = new GLatLng(parseFloat(a[0]), parseFloat(a[1]));
            var m = new GMarker(p);
            GEvent.addListener(m, 'click', function() {
               var html = "<b>"  + a[2] + "</b><br/>";
                   html += "<a href='" + a[3] + "'>Click for more information &raquo;</a>";
               map.openInfoWindowHtml(p, html);
            });
            return m;
	    }
	    
	    for (var i = 0; i < coords.length; i++) {
	        map.addOverlay(mkMarker(coords[i]));
	    }
	    
	}
	
	$('.bottom-map').each(function() {
	   var coords = $('.coords', this).remove().text().split(',');
	   var map = new GMap2(this);
	   map.addControl(new GMapTypeControl());
	   map.addControl(new GLargeMapControl());
	   var pnt = new GLatLng(parseFloat(coords[0]), parseFloat(coords[1]));
	   var zoom = coords[2] ? parseInt(coords[2]) : 6;
	   map.setCenter(pnt, zoom);
	   map.addOverlay(new GMarker(pnt));
	});
	
	$('.faq dd').hide();
	$('.faq .expand-all').click(function() {
		$('dd', $(this).parents('.faq').eq(0)).show();
		return false;
	});
	$('.faq dt').click(function() {
		$('+ dd', this).toggle();
	});
	
	var seq = 0;
	$('.video-player').each(function() {
	    var id = "generated-player-" + (seq++);
	    var movie = $('a.actuator', this).attr('href');
	    var s1 = new SWFObject('/design/sciaf/images/200805/flvplayer.swf', id, "425", "355", "7", "#ffffff");
	    s1.addParam("allowfullscreen","true");
        s1.addVariable("file", movie);
        s1.addVariable("autostart", false);
        s1.addVariable("enablejs", true);
        var c = $('<div class="video-container"><div class="wrapper"></div></div>').appendTo($('> .thumb', this));
	    c[0].id = id;
	    s1.write(c[0]);
	    $('a.actuator', this).click(function() {
	        tb_show(this.title, '#TB_inline?height=365&width=445&inlineId=' + id);
	        return false;
	    });
	});
	
});

function createCookie(name,value,days) {
	if (days) {
  		var date = new Date();
  		date.setTime(date.getTime()+(days*24*60*60*1000));
  		var expires = "; expires="+date.toGMTString();
 	}
 	else var expires = "";
 	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
 	var nameEQ = name + "=";
 	var ca = document.cookie.split(';');
 	for(var i=0;i < ca.length;i++) {
  		var c = ca[i];
  		while (c.charAt(0)==' ') c = c.substring(1,c.length);
  		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
 	}
 	return null;
}

function updateSizeFromCookie() {
	$('body')[0].className = readCookie('size') || 'medium';
}

function slideSwitch() {
  
  var links  = new Array();
  
  links['slideshow-pic-1'] = '#';
  links['slideshow-pic-2'] = 'http://www.sciaf.org.uk/get_involved/campaigning/join_the_journey_to_climate_justice';
  links['slideshow-pic-3'] = 'https://www.sciaf2.org.uk/SSLPage.aspx?pid=460';
  
  var active = $('#slideshow img.active');
  if ( active.length == 0 )
    active = $('#slideshow img:last');

  var next =  active.next().length ? active.next() : $('#slideshow img:first');
  
  active.addClass('last-active');
  next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() {
    active.removeClass('active last-active');
  });
  
  $('#slideshow-link').attr('href', links[next.attr('id')] );
  
}


