

$(document).ready(function(){
  
	$("#caernarfon").attr('rel','122-119');
	$("#caernarfon-box").attr('rel','128-150');
	$("#beaumaris").attr('rel','145-76');
	$("#beaumaris-box").attr('rel','151-105');
	$("#conwy").attr('rel','218-72');
	$("#conwy-box").attr('rel','224-99');
	$("#denbigh").attr('rel','293-110');
	$("#denbigh-box").attr('rel','300-139');
	$("#chester").attr('rel','449-118');
	$("#chester-box").attr('rel','457-148');
	$("#townlist_caernarfon").attr('rel','caernarfon');
	$("#townlist_beaumaris").attr('rel','beaumaris');
	$("#townlist_conwy").attr('rel','conwy');
	$("#townlist_denbigh").attr('rel','denbigh');
	$("#townlist_chester").attr('rel','chester');


    $("body").click(function(event) {
   if ($(event.target).is("a[href*=http://]")) {
   $(event.target).attr('target','_blank');
  
   }
   });
  
    $('#header_images').s3Slider({
          timeOut: 9000 
       });              
       $('#gallery div').jCarouselLite({
               btnNext: ".next",
               btnPrel: ".prel",
               vertical: true,
               visible: 3
        });        
        
    $('.gallery_thumb a').click(function() {
      $('.gallery_main').attr("src",$(this).attr("href"));
      $('.gallery_main_title').text($(this).attr("title"));
      return false;
    });
    
   if($("#mapbig").length>0){
     initializeMap();
     initializeList();
   }
  
  	function showPopup(id){  	  
  		var boxid = '#' + id + '-box';
  		var coords = $(boxid).attr('rel').split('-');
  		if($(boxid).css('display')=='none')
  		{
    		$('#mapbig div.popup').fadeOut();
    		$(boxid).css({left: coords[0] + 'px', top: coords[1] + 'px'});
    		$(boxid).fadeIn();
    	}
  	}

   	function initializeMap(){      	  
       	  loadBullets();
   	}
   	function highlightTown(id){
   	  $('.townlist').attr('class','townlist');
   	  $('#townlist_'+id).attr('class','townlist current');
   	}
   
    function highlightBullet(id){
      $('#mapbig a').removeClass('current');
      $('#'+id).addClass('current');
    }
  	function loadBullets(){  				
  			//place bullets
  			$('#mapbig').children('a.bullet').each(function(){
  				var coords = $(this).attr('rel').split('-');
  				$(this).css({left: coords[0] + 'px', top: coords[1] + 'px'})
  					   .hide()
  					   .fadeIn()  					   
  					    .hover(function(){showPopup($(this).attr('id'));
  					        highlightTown($(this).attr('id'));});
  			});
  	}
  	function initializeList(){
  	  $('#towns-choice').children('li.townlist').each(function(){
  	    $(this).hover(function(){
  	        highlightTown($(this).attr('rel'));
  	        showPopup($(this).attr('rel'));
  	        
  	        highlightBullet($(this).attr('rel'));
  	    })  	    
  	  });
  	}

});
