Cufon.replace('#nav a, h1');

//thanks to php.js for rand, in_array & relative_time functions
function rand (min, max) {
    var argc = arguments.length;
    if (argc === 0) {
        min = 0;
        max = 2147483647;
    } else if (argc === 1) {
        throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
    }
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
function in_array (needle, haystack, argStrict) {
    var key = '', strict = !!argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
 
    return false;
}
function relative_time(time_value) {
	var values = time_value.split(" ");
	time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	var parsed_date = Date.parse(time_value);
	var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	delta = delta + (relative_to.getTimezoneOffset() * 60);
	
	var r = '';
	if (delta < 60) {
	r = 'a minute ago';
	} else if(delta < 120) {
	r = 'couple of minutes ago';
	} else if(delta < (45*60)) {
	r = (parseInt(delta / 60)).toString() + ' minutes ago';
	} else if(delta < (90*60)) {
	r = 'an hour ago';
	} else if(delta < (24*60*60)) {
	r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
	} else if(delta < (48*60*60)) {
	r = '1 day ago';
	} else {
	r = (parseInt(delta / 86400)).toString() + ' days ago';
	}
	
	return r;
}
function moveBear() {
	$("body").append('<div id="bear" style="position: absolute;bottom: 16px;left: -302px;z-index: 3;"><img src="http://i38.tinypic.com/6yi13a.png" alt="" /></div>');
	$("#bear").animate({left: 10}, 10000);
}
$(function() {
	//IE6 transparent repeating PNG hack
	if($.browser.msie && parseInt($.browser.version, 10) == 6) {
		width = screen.width;
		if (width > 1024) {
			$("#grass").after('<div id="grass2"></div>');
		}
		alert('You\'re running a 10 year old browser, Internet Explorer 6. For a fully functioning site please upgrade your browser or use an alternative such as firefox. Thank you.');
	}
	var i = 0;
	var top = 80;
	var left = 60;
	var thumbArray = new Array(5);
	while (i < 5) {
		wt = rand(0,4);
		if (thumbArray[wt]) {
			//Do nothing!
		} else {
			thumbArray[wt] = true;
			num = wt + 1;
			if (i == 1) {
				top = 95;
				left = 150;
			}
			if (i == 2) {
				top = 200;
				left = 80;
			}
			if (i == 3) {
				top = 270;
				left = 150;
			}
			if (i == 4) {
				top = 300;
				left = 40;
			}
			$("#snaps").append("<div class='thumb'><a href='images/full/"+num+".jpg' class='lightbox'><img id='thumb"+i+"' src='images/thumbs/"+num+".jpg' alt='' /></a></div>");
			$("#thumb"+i).parent().parent().css({'top' : top, 'left' : left});
			i++;
		}
	}
	$(document).pngFix();
	$('#snaps a').lightBox({
		overlayOpacity: 0.5,
		imageLoading: 'library/lightbox/images/lightbox-ico-loading.gif',
		imageBtnClose: 'library/lightbox/images/lightbox-btn-close.gif',
		imageBtnPrev: 'library/lightbox/images/lightbox-btn-prev.gif',
		imageBtnNext: 'library/lightbox/images/lightbox-btn-next.gif',
		imageBlank: 'library/lightbox/images/lightbox-blank.gif',
		fixedNavigation: true
	});
	$('#board a, #fakecontact').click(function() {
		$("#black").show().css("opacity", 0).animate({ 'opacity' : 0.5 }, function() {
			$("#contact").show();
			$("#name").focus();
		});
	});
	$("#contact input, #contact textarea").focus(function() {
		$(this).css("border" , "2px solid #F00");
	}).blur(function() {
		if ($(this).val() != '')
			$(this).css("border" , "2px solid #CCC");
		else
			$(this).css("border" , "2px solid #000");
	});
	$('#black').click(function() {
		$("#contact").hide();
		$("#black").animate({ 'opacity' : 0 }, function() {
			$(this).hide();
		});
	});
	$("#nav li").hover(function() {
		$(this).animate({'paddingTop' : 10}, 200);							
	}, function() {
		if ($(this).attr('class') != 'current')
			$(this).animate({'paddingTop' : 50}, 75);							
	}).click(function() {
		location.href = $(this).children().attr('href');
	});
	$.getJSON("http://twitter.com/status/user_timeline/rockingrascals.json?count=1&callback=?", function(data) {
		$.each(data, function(i, item) {
			$("#twitterbubble span").fadeOut(function() {
				$(this)
					.html('')
					.append(item.text + "<span id='created_at'> Posted " + relative_time(item.created_at) + " on twitter. <a href='http://twitter.com/rockingrascals'>Follow us</a>.</span>")
					.fadeIn();					  
			});
		});
	});
	if (sPage == 'centres') {		
		var map1 = new GMap2(document.getElementById('map1'));
		var hut = new GLatLng(53.41449, -2.17803);
   		map1.setCenter(hut, 15);
		map1.setMapType(G_HYBRID_MAP);
		
		var map2 = new GMap2(document.getElementById('map2'));
		var church = new GLatLng(53.366643, -2.186849);
   		map2.setCenter(church, 15);
		map2.setMapType(G_HYBRID_MAP);
	}
	$("#send").click(function() {
		var name = $("#name").val();	  
		var subject = $("#subject").val();	  
		var email = $("#email").val();	  
		var number = $("#number").val();	  
		var message = $("#message").val();	  
		
		if (name == '') {
			alert('Please enter your name');
		} else if (subject == '') {
			alert('Please enter your subject');
		} else if (email == '') {
			alert('Please enter your email');
		} else if (message == '') {
			alert('Please enter your message');
		} else {
			$.post("contact.php", { name: name , subject: subject, email: email, number: number, message: message }, function(data) { 
				if (data == 'ok') {
					alert('Form successfully sent');
					
					$("#contact").hide();
					$("#contact input[type='text'], #contact textarea").val('').css("border" , "2px solid #000");
					$("#black").animate({ 'opacity' : 0 }, function() {
						$(this).hide();
					});
				} else
					alert('Error, please email rockingrascals@hotmail.com instead. Sorry!');
			});
		}
	});
});
