$(document).ready(function(){
if($('#blog_preview_content').length != 0){
if($('#blog_preview_content').children().length>2){
var count=1;
$('#blog_preview_content').children().each(function(){
	if(count>2)
		$(this).css('display','none');

	if($(this).is('p'))
		count++;
});

}
}
if($('.search_form')){
  $('.search_form').submit(function(e){
    var ret = true;
    $('.search_textfield', this).each(function(){
      if($(this).val().length<4){
		  alert('Search keyword needs to be longer than 3 characters!');
		  ret = false;
	  }
      });
    return ret;
  });
}

if($('.arc_head')){
if($('.post').length == 0 && $('.app_call').length == 0){
$('.no_posts').show();
$('.no_posts').html('<p>No posts here, search the menu to the left!</p>');
}
}


$('input,textarea').focus(function(){
$(this).css('color', '#000000');
});
$('input,textarea').blur(function(){
$(this).css('color', '#878787');
});

if($('.page_news_item') || $('.page_blog_recent')){
$('.page_news_item, .page_blog_recent').click(function(){
document.location = $('a:first', this).attr('href');
});
}


$('a[href^="\#"]:not(.mode_toggle)').click(function(e){
 	e.preventDefault();
	document.location.hash=this.href.substr(this.href.indexOf('#')+1);
  });

  if($('.quip')){
    if($('.quip h3:first').text().search('(0)')>0){
	$(document.createElement('h3')).text('No comments yet').addClass('withMargins').insertBefore('.quip');
	
    $('.quip').hide();
    }
  }
  if($('.arc_head')){
    if($('.arc_head').text().length<4){
    	$('.arc_head').text('Latest Headlines');
    }
  }

    if($('strong')){
$('strong').each(function(){
$(this).replaceWith('<span class="strong">' + $(this).text() + '</span>');
});
    }
    if($('em')){
$('em').each(function(){
$(this).replaceWith('<span class="italic">' + $(this).text() + '</span>');
});
    }
if($('#example_error_post')){
      $('#example_error_post').hide();
      $('#show_example_error_post').toggle(function(){
      	$('#example_error_post').show();
      },function(){
      	$('#example_error_post').hide();
      });
    }
    if($('.search_box')){
	$('.mode_toggle').click(function(e){
	e.preventDefault();
	});
	$('.simple_mode').hide();
	$('.search_box').each(function(i, box){
	    $('.mode_toggle', this).toggle(function(){
		$('.simple_mode', box).show();
		$('.adv_mode', box).hide();
		$('.mode_toggle', box).html(' Advanced search');
	    }, function(){
	    	$('.simple_mode', box).hide();
		$('.adv_mode', box).show();
		$('.mode_toggle', box).html(' Simple search');
	    });
	});
    }
    if($('input:text')){
    $('input:text').each(function(i){
    $(this).attr('title', $(this).val());
    });
    
    }
    if($('textarea')){
    $('textarea').each(function(i){
    $(this).attr('title', $(this).val());
    });
    
    }
      $('input:text, textarea').focus(function(){
      if($(this).val()==$(this).attr('title')){
$(this).val('');
      }
      });
$('input:text, textarea').blur(function(){
      if($(this).val()==''){
$(this).val($(this).attr('title'));
      }
      });

    if($('.sitemap_column')){
	    var scols = $('.sitemap_column');
      for(var i=1;i<scols.length+1;i++){
	if(i%3 == 0){
		var bdiv = document.createElement('div');
		bdiv.className = 'clear_both';
	 	scols[i-1].className='sitemap_column_right';
		scols[i-1].parentNode.insertBefore(bdiv, scols[i]);
	}
      }
    }


    if(document.getElementById('search_button')){
    document.getElementById('search_button').onclick=submit_top_search_form;
    }

    $("#banner_slider").mbScrollable({
       width:990,
       elementsInPage:1,
       elementMargin:0,
       height:"auto",
       controls:"#banner_slider_controls",
       slideTimer:500,
       autoscroll:true,
       scrollTimer:14000
    });
    if(document.getElementById('map_canvas')){
	    initializeMap();
    }
  if(document.getElementById('sci_news_feed')){
  	remakeRss();
  //function is in rss_edit.js
  }
  
});
var map;
var mgr;
var geocoder;
var map;
//geocoder = new google.maps.Geocoder();

  function codeAddress(country) {
    geocoder.geocode( { 'address': country}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
	$('#latLongDiv').html( oldHtml + results[0].geometry.location.toString());
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }
function initializeMap() {
if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(7.188, 21.093), 2);
    map.setUIToDefault();
    G_PHYSICAL_MAP.getMinimumResolution = function () { return 2 };
                G_NORMAL_MAP.getMinimumResolution = function () { return 2 };
                G_SATELLITE_MAP.getMinimumResolution = function () { return 2 };

                G_PHYSICAL_MAP.getMaximumResolution = function () { return 5 };
                G_NORMAL_MAP.getMaximumResolution = function () { return 5 };
                G_SATELLITE_MAP.getMaximumResolution = function () { return 5 };
    mgr = new MarkerManager(map);
    GEvent.addListener(mgr, 'loaded', function() { 
	alert('manager loaded');
	});
}else{
alert('Your browser is not compatible with this version of google maps.');
}
}
function setupMapMarkers() {

  for (var i in mapLayer) {
    if (mapLayer.hasOwnProperty(i)) {
      var layer = mapLayer[i];
      var markers = [];
      for (var j in layer["places"]) {
        if (layer["places"].hasOwnProperty(j)) {
          var place = layer["places"][j];
	  var desc = place["desc"];
          var title = place["name"];
          var posn = new GLatLng(place["posn"][0], place["posn"][1]);
          var marker = createMarker(posn, title, desc);
          markers.push(marker);
        }
      }
      mgr.addMarkers(markers, layer["zoom"][0], layer["zoom"][1]);
    }

  }
  mgr.refresh();
}

function createMarker(posn, title,desc) {
var marker = new GMarker(posn,{ title: title });
  var infoBoxStructure = '<div class="map_info_box"><h3>'+title+'</h3><p>'+desc+'</p></div>';
  GEvent.addListener(marker, 'click', function() {
  map.openInfoWindow(posn,infoBoxStructure);
});

  return marker;
}


function submit_top_search_form(){
document.forms['top_search_form'].submit();
}

function decide_size (image) {
alert(image.offsetHeight + " || " + image.offsetWidth);
}
