$(document).ready(function(){

	/* Galleries */

	$("dl.gallery dd").click(function(){
		var parent = $(this).prevAll("dt:first");
		load_img(this, parent);
		$(".desc", parent).hide().html($(".desc", this).html()).show();
		return false;
	});

	$("#product-description ul.thumbs li").click(function(){
		load_img(this, $("#product-description .preview"));
		return false;
	});

	$("#gallery ul.thumbs li").click(function(){
		gallery_showcase($(this));
		return false;
	});

	$("#gallery .showcase a.next").click(function(){
		var np = get_gallery_thumbs_next_prev($("#gallery ul.thumbs li.on"));
		gallery_showcase(np[0]);
		return false;
	});

	$("#gallery .showcase a.prev").click(function(){
		var np = get_gallery_thumbs_next_prev($("#gallery ul.thumbs li.on"));
		gallery_showcase(np[1]);
		return false;
	});


	/* Text Shadows */

	$(".shadow").each(function(){
		$(this).prepend("<em>"+$(this).text()+"</em>");
	});


	/* Clickable items */

	if($(".clickable").length) {

		$(".clickable > li").hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		});

		$(".clickable > li").each(function() {
			if($("a", this).length) {	
				$(this).css("cursor", "pointer");
			}
		});

		$(".clickable > li").click(function(){
			if($("a", this).length) {
				location.href = $("a:first", this).attr("href");
				return false;
			}
		});
	}


	//hide the all of the elements with class moretext
	$(".moretext").hide();
	
	//show the more link for users with javascript enabled
	$(".morelesslink").show();
	
	//toggle the content to be displayed
	$(".morelesslink").click(function(){
	
		if ($(this).prev(".moretext").is(":hidden")){ 
			realtext = $(this).text();
			$(this).text("View less");
			$(this).prev(".moretext").show();
		} else {
			$(this).text(realtext);
			$(this).prev(".moretext").hide();
		}
		return false;
	
	});

	/* Map */

	country = false;
	timeout = false;

	$("#map").mousemove(function(e){

		clearTimeout(timeout);

		pos_x = e.offsetX?(e.offsetX):e.pageX-Math.floor($("#map").offset().left);
		pos_y = e.offsetY?(e.offsetY):e.pageY-Math.floor($("#map").offset().top);

		if(pos_x > 175 && pos_x < 225 && pos_y > 18 && pos_y < 100) {
			if(country != "UK") {
				country = "UK";
				show_country_on_map(e, "UK");
			}
		} else if(pos_x > 230 && pos_x < 300 && pos_y > 421 && pos_y < 474) {
			if(country != "Nigeria") {
				country = "Nigeria";
				show_country_on_map(e, "Nigeria");
			}
		} else if(pos_x > 390 && pos_x < 458 && pos_y > 266 && pos_y < 349) {
			if(country != "Egypt") {
				country = "Egypt";
				show_country_on_map(e, "Egypt");
			}
		} else if(pos_x > 180 && pos_x < 244 && pos_y > 109 && pos_y < 162) {
			if(country != "France") {
				country = "France";
				show_country_on_map(e, "France");
			}
		} else {
			timeout = setTimeout(function() {
				$("#talkbox").remove();
				$("#"+country).removeClass("on");
				country = false;
			}, 100);
		}

	});


});

function show_country_on_map(e, name) {

		if( $("#"+name).is(".on") ) {
			return false;
		}

		$("#talkbox").remove();
		$("#"+country).removeClass("on");

		var contents = $("#"+name).html();
		$("#"+name).addClass("on");

		$("body").append('<div id="talkbox" class="box"><div class="before"></div><div class="inner">'+contents+'</div><div class="after"></div></div>');

		var talkbox = $("#talkbox");

		talkbox
			.css("top", (e.pageY-talkbox.innerHeight()+10) + "px")
			.css("left", (e.pageX-100) + "px")
			.show();
}


function load_img(source, destination) {
	$(".wrapper", destination).hide();
	var path = $("a:first", source).attr('href');
	$('img:first', destination).remove();
	var imgsrc = path;
	var newimg = new Image();
	newimg.src = imgsrc;
	newimg.onload = function() {
		var randomnumber = Math.floor(Math.random()*11);
		if( $(".wrapper", destination).length ) {
			$(".wrapper", destination).append('<img src="'+imgsrc+'" />');
			$(".wrapper", destination).show();
		} else {
			$(destination).append('<img src="'+imgsrc+'" />');
		}
		newimg.onload = function(){};
	}
	newimg.src = imgsrc;
}

function gallery_showcase(obj) {

	$("#gallery ul.thumbs li.on").removeClass("on");
	obj.addClass("on");

	load_img(obj, $("#gallery .main"));

	var np = get_gallery_thumbs_next_prev(obj)

	load_img(np[0], $("#gallery .col.starcol"));
	load_img(np[1], $("#gallery .col.portcol"));

}

function get_gallery_thumbs_next_prev(obj) {

	var next_li = ( !obj.next("li").length ) ? $("#gallery ul.thumbs li:first") : obj.next("li");
	var prev_li = ( !obj.prev("li").length ) ? $("#gallery ul.thumbs li:last") : obj.prev("li");

	return new Array(next_li, prev_li);

}