$(document).ready(function() {
    var tab = document.location.hash;
    if(tab == '') tab = '#overview-t';
    $('.un_content').css({'visibility': 'hidden', 'height': '0px', 'overflow': 'hidden'});
    $(tab+'ab').addClass("selected");
    $(tab+'ab-content').css({'visibility': '', 'height': 'auto', 'overflow': ''});	
    
	
	$("#univ_nav li").click(function() {
            $("#univ_nav li.selected").removeClass("selected");
			window.location.replace($(this).children('a').attr('href'));
            $("#univ_content div.un_content").css({'visibility': 'hidden', 'height': '0px', 'overflow': 'hidden'});
            $(this).addClass("selected");
            $("#"+this.id+"-content").css({'visibility': '', 'height': 'auto', 'overflow': ''});
            
    })
    
    $("#show_on_map").click(function() {
            $("#univ_nav li.selected").removeClass("selected");
            $("#univ_content div.un_content").css({'visibility': 'hidden', 'height': '0px', 'overflow': 'hidden'});
            $("#contact-tab").addClass("selected");
            $("#contact-tab-content").css({'visibility': '', 'height': 'auto', 'overflow': ''});
    })
    
    $('.un_content .info_block:last-child').each(function() {
            $(this).removeClass('dotted')
    });
    
    initialize("univ_map",true);	
    $('img.gal').lazyload({ 
            placeholder : "<?=STATIC_IMG_DIR?>/grey.gif",
            effect : "fadeIn" 
        });
    
    
    if (document.location.hash == '#gallery-t') {
            if ($('#gallery_1').length) {
                    $('#gallery_1 img').each(function() {
                            $(this).attr('src',$(this).attr('original'));
                    });
            }
    }
    
    Shadowbox.init();
    Shadowbox.setup("a.photos", {
                    gallery: "photos",
                    continuous: true,
                    counterType: "skip"
    });	
    
    $("#shortlist").click(function(){            
        var shortlist = $.cookie('shortlist');
        var added = $(this).attr('added');            
        if (added == 'added') {
            $(this).removeAttr('added').attr('title','Add to Shortlist ').text('Add to Shortlist ');
            var reg2 = new RegExp(","+unv_id+",","i");
            shortlist = shortlist.replace(reg2,",");
            $.cookie('shortlist', shortlist, {path: '/', domain: COOKIE_DOMAIN });                
            var sl = shortlist.split(',').length-2;
            $('#shortListCount').text(sl);
        } else {                
            $(this).attr('added','added').attr('title','Remove from Shortlist').text('Remove from Shortlist');
            if (!shortlist) var shortlist = ','; 
            shortlist = shortlist +unv_id+ ',';                
            $.cookie('shortlist', shortlist, {path: '/', domain: COOKIE_DOMAIN });
            var sl = shortlist.split(',').length-2;
            $('#shortListCount').text(sl);
        }
        
    });
    
    $("#enlarge_map").click(function(){          
            initialize_map_bigger();
    });
    
    var gallery_page = $('#gallery_page').val();
    $('#gallery_'+gallery_page).show();
    if(gallery_pages>gallery_page) $('#page_next').removeClass('dark');

	
});

function gallery_change_page (page) {
	$('.gallery').hide();
	$('.link_gallery').removeClass('dark');
	var gallery_page = $('#gallery_page').val();
	var gallery_new_page;
	
	if (page == 'previous') {
		if (gallery_page == 1) gallery_new_page = 1;
		else gallery_new_page = parseInt(gallery_page)-1;
	} else if (page == 'next') {
		if (gallery_page == gallery_pages) gallery_new_page = gallery_pages;
		else gallery_new_page = parseInt(gallery_page)+1;
	} else {
		gallery_new_page = page;
	}
	
	if (gallery_new_page == 1) $('#page_previous').addClass('dark');
	if (gallery_new_page == gallery_pages) $('#page_next').addClass('dark');
	$('#page_'+gallery_new_page).addClass('dark');
		
	$('#gallery_'+gallery_new_page).show();
	$('#gallery_page').val(gallery_new_page);
}

function initialize_map_bigger () {
	Shadowbox.open({		
		content:    '<div id="enlarged_map" class="sb-loading" style="color: white"><div class="sb-loading-inner"><span>loading</span></div></div>',
		player:     "html",
		title:      "Map",
		height:     528,
		width:      792
	});

	setTimeoutcleaner = setTimeout("map_bigger()",10);
}

function map_bigger () {
	
    if ($('#enlarged_map').length) {
            clearTimeout(setTimeoutcleaner);			
            initialize("enlarged_map",false,0);			
    } else {
    
        clearTimeout(setTimeoutcleaner);
        setTimeoutcleaner = setTimeout("map_bigger()",1000);
    }
	
}

function initialize(map,ui,click) {
      
  var latlng = new google.maps.LatLng(lat, lng);
  var myOptions = {
    zoom: 16,
    center: latlng,
    disableDefaultUI: ui,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  
  var map = new google.maps.Map(document.getElementById(map), myOptions);
  
  if (click != 0) {	
      google.maps.event.addListener(map, 'click', function() {
              initialize_map_bigger();
      });
  }
  var marker = new google.maps.Marker({
    position: latlng, 
    map: map, 
    title:marker_title
  });
}
  
$('.info_block table').each(function() {
        if($.trim($(this).text()) == '' && $(this).children().lenght)
                $(this).parent().remove();
});
      
$('.info_block:not(#introtext)').each(function() {
        if($(this).children().size() < 2)
                $(this).remove();
});
$('.un_content').each(function() {
        if($(this).children().size() == 0) {
                $('#'+this.id.substring(0, this.id.length-8)).remove();
                $(this).remove();
        }
});
