jQuery.noConflict();
jQuery(document).ready(function($) {
	$.web_site_namespace = { 
		counter : 1
	};
	$.facebox.settings.closeImage = '/assets/images/closelabel.png';
	$.facebox.settings.loadingImage = '/assets/images/loading.gif';
	$('#loading').show();
	$('body > #loading').click(function(){
		$(this).fadeOut('slow');
	});
	$("html").removeClass('noscript');
	$('a[rel*=modal]').facebox();
	$("#menu li:last a").css("background-image","none");
	$('.truncate li:gt(3)').hide();
	$('.truncate').append('<li class="more"><a href="#more">more...</a></li>');
	$('.more').stop().live('click', function() {
		$('.truncate').append('<li class="less"><a href="#less">less...</a></li>');
		$('.more').remove();
		$('.truncate li').show();
		return false;	  
	});
	$('.less').stop().live('click', function() {
	  $('.less').remove();
		$('.truncate li:gt(3)').hide();
		$('.truncate').append('<li class="more"><a href="#more">more...</a></li>');
		return false;
	});
	$('.modal').stop().live('click', function(){
		var mclass = '.'+$(this).attr('id');
		var mhref = $(this).attr('href');
		$('#loading').show().css('opacity', '0.2');
		$('body').append('<div id="modalized"></div>');
		$('#modalized').load(mhref, function() {
		  $(mclass).fadeIn('slow');
			$(".main_image .desc").show();
			$(".main_image .block, .main_image a.collapse").animate({ opacity: 0.85 }, 1 );
			$(".image_thumb ul li:first").addClass('active');
			$('#left, #up').addClass('disabled');
			$.web_site_namespace.counter = 1;
		});
		return false;
	});
	$('#results li').hover(function(){
		$(this).find('img').fadeOut('slow');		
	}, function(){
		$(this).find('img').fadeIn('slow');
	});
	$(".image_thumb ul li").stop().live('click', function(){
			$.web_site_namespace.counter = 1;
			$.imgpreload('/assets/images/loading-fff.png',function(){});
			$('.main_image .loading').remove();
			var images = $(this).find('div.images img').clone();
	    var imgAlt = $(this).find('img').attr("alt");
	    var imgHref = $(this).find('a').attr("href");
	    var imgDesc = $(this).find('.block').html();
	    var imgDescHeight = $(".main_image").find('.block').height();
			var image = new Image();
			image.src=imgHref;
	    if ($(this).is(".active")) {
	        return false;
	    } else {
				$('.main_image').prepend('<div class="loading"></div>');
	      $(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250 , function() {
					$('.main_image .images').empty().append(images);
	       	$(".main_image .block").html(imgDesc).animate({ opacity: 0.85,  marginBottom: "0" }, 250 );
	       	$(".main_image img:first").attr({ src: imgHref , alt: imgAlt});
					if ((image.complete && image.height != 0) || this.readyState === 4) {
			      $('.main_image .loading').fadeOut(1250).remove();
			    } else {
						$('.main_image .loading').html('<span>Error loading image</span>');
					}
					$.imgpreload(imgHref,
					{
						each: function() {
				      $('.main_image .loading').fadeOut(1250).remove();
						}
					});
				});
	    }
	    $(".image_thumb ul li").removeClass('active');
	    $(this).addClass('active');
			$('#up, #down, #left, #right').removeClass('disabled');
			$('#left').addClass('disabled');
			if($('.image_thumb li').filter(':first').hasClass('active')){
				$('#up').addClass('disabled');
			} else if ($('.image_thumb li').filter(':last').hasClass('active')) {
				$('#down').addClass('disabled');
			}
			$('.main_image > .images').css({left: "0"});
	    return false;
	}).live('mouseover', function(){
		$(this).addClass('hover'); 
	}).live('mouseout', function() {
	    $(this).removeClass('hover');
	});
	$("a.collapse").stop().live('click', function(){
	    $(".main_image .block").slideToggle();
	    $("a.collapse").toggleClass("show, disabled");
			return false;
	});
	$("a.close").stop().live('click', function(){
		$('#modalized').fadeOut('slow').remove();
		$('#loading').fadeOut('slow');
		return false;
	});
	$('.controls a').stop().live('mouseover', function(){
		var message = $(this).attr('title');
		$('.message').html(message);
	}).stop().live('mouseout', function() {
		$('.message').html('controls');
	});
	$('.down').stop().live('click', function(){
		$('.image_thumb li.active').next('li').find('a').stop().click();
		return false;
	});
	$('.up').stop().live('click', function(){
		$('.image_thumb li.active').prev('li').find('a').stop().click();
		return false;
	});
	$('.right').stop().live('click', function(){
		var left = parseInt($('.main_image > .images').css('left'));
		var img_width = parseInt($('.main_image > .images > img').css('width'));
		var count = parseInt($('.main_image > .images img').length);
		var width = parseInt(img_width * count);
		$('.main_image > .images').css({width: width+'px'});
		if($.web_site_namespace.counter < count) {
			$('.main_image > .images').animate({left: "-="+img_width+'px'});			
			$.web_site_namespace.counter++;
		}
		$('#left, #right').removeClass('disabled');
		if($.web_site_namespace.counter == count) {
			$(this).addClass('disabled');
		}
		return false;
	});
	$('.left').stop().live('click', function(){
		var left = parseInt($('.main_image > .images').css('left'));
		var img_width = parseInt($('.main_image > .images > img').css('width'));
		var count = parseInt($('.main_image > .images img').length);
		var width = parseInt(img_width * count);
		if($.web_site_namespace.counter > 1) {
			$('.main_image > .images').animate({left: "+="+img_width+'px'});			
			$.web_site_namespace.counter--;
		}
		$('#left, #right').removeClass('disabled');
		if($.web_site_namespace.counter == 1) {
			$(this).addClass('disabled');
		}
		return false;
	});
	$('#footer a.map_modal').click(function() {
		$.facebox(function() {
			$('#facebox .content').append('<div id="map"></div>');
		  var latlng = new google.maps.LatLng(-33.978883,18.462524);
		  var options = {
		    zoom: 8,
		    center: latlng,
		    mapTypeId: google.maps.MapTypeId.ROADMAP
		  };
		  var map = new google.maps.Map(document.getElementById('map'), options);
		 	var marker = new google.maps.Marker({
		  	position: latlng,
				map: map,
		  	title: "Dot Media",
				animation: google.maps.Animation.DROP
		  });
		});
		return false;
	});
	$('#body a.map_modal').click(function() {
		$.facebox(function() {
			$('#facebox .content').append('<div id="map"></div>');
		  var latlng = new google.maps.LatLng(0,0);
		  var options = {
		    zoom: 1,
		    center: latlng,
		    mapTypeId: google.maps.MapTypeId.ROADMAP
		  };
		  var map = new google.maps.Map(document.getElementById('map'), options);
		  var markers = [
		    ['South Africa',-33.978883, 18.462524],
		    ['United Kindgom',51.506324931979179, -0.12714475393295288],
		    ['Australia', -33.867139, 151.207114],
				['Brazil', -21.91133, -49.365315],
				['United States', 36.778261, -119.417932],
				['Canada', 45.411572, -75.698194],
				['Namibia', -22.558904, 17.082481],
				['Zimbabwe', -17.82922, 31.053961],
				['Mauritius', -20.165279, 57.49638],
				['New Zealand', -43.532054, 172.636225],
				['Italy', 41.89052, 12.494249],
				['Germany', 50.940665, 6.959912],
				['Ukraine', 50.4501, 30.5234],
				['Romania', 44.437711, 26.097367]
		  ];
		  for(var i = 0; i < markers.length; i++) {
		    var store = markers[i];
						latlng = new google.maps.LatLng(store[1], store[2]);
						marker = new google.maps.Marker({
			  			position: latlng,
							map: map,
			  			title: store[0]
			  		});
		  }
		});
		return false;
	});
});
jQuery(window).load(function() {
	jQuery('#loading').fadeOut('slow');
});
