var locations = new Object();
var tabs;
var logoNum = 1;

$(document).ready(function () {

	if (navigator.userAgent.indexOf('Opera/8.5') == -1 ) { 
	// if not opera 8.5 (stb) run js as usual  

    // allows for no js css targetting
  	$('body').removeClass('noJavascript');
  	// allows for page targetting
    var template = $('body').attr('id');
    
    // all pages:
    initCufon(); 
    
    // tabs:
    if ($('#tabs')) { initTabs(); }
  
    // image swapper:
    if ($('#imageSwapper')) { initSwapper('#imageSwapper',15000); }  
  
    // specific page:
    if ( template == 'contactUs' ) {
      // dont call google unless your doing maps (google JS is only loaded on contact page)
      populateLocations();
      initMaps();
    }

  }

});


function initCufon () {
	
	var elements = new Array (
				'#nav li > a', 
        '#imageSwapper .slide .content h3',
        '#imageSwapper .slide .content p',
        '#imageSwapper .slide .content a',
				'h2',
        '.quote p span',
        '#home .quote p',
     //   '#tabs li span',        
        '.widgets h4'
			);
	
	Cufon.replace(elements, {forceHitArea:true} );
}


function initSwapper(swapId,slideDelay) {

    $(swapId).anythingSlider({
      // Navigation
      width               : 940,      // Override the default CSS width
      height              : 280,      // Override the default CSS height
      startPanel          : 1,         // This sets the initial panel
      hashTags            : true,      // Should links change the hashtag in the URL?
      buildArrows         : false,      // If true, builds the forwards and backwards buttons
      buildNavigation     : false,      // If true, buildsa list of anchor links to link to each panel
      navigationFormatter : null,      // Details at the top of the file on this use (advanced use)
    
      // Slideshow options
      autoPlay            : true,      // This turns off the entire slideshow FUNCTIONALY, not just if it starts running or not
      startStopped        : false,     // If autoPlay is on, this can force it to start stopped
      pauseOnHover        : true,      // If true & the slideshow is active, the slideshow will pause on hover
      resumeOnVideoEnd    : true,      // If true & the slideshow is active & a youtube video is playing, it will pause the autoplay until the video has completed
      stopAtEnd           : false,     // If true & the slideshow is active, the slideshow will stop on the last page
      playRtl             : false,     // If true, the slideshow will move right-to-left
      startText           : "Start",   // Start button text
      stopText            : "Stop",    // Stop button text
      delay               : slideDelay,      // How long between slideshow transitions in AutoPlay mode (in milliseconds)
      animationTime       : 800,       // How long the slideshow transition takes (in milliseconds)
      easing              : "swing"    // Anything other than "linear" or "swing" requires the easing plugin
    });  
      // Appearance
      //resizeContents      : true,      // If true, solitary images/objects in the panel will expand to fit the viewport
      //forwardText         : "&raquo;", // Link text used to move the slider forward (hidden by CSS, replaced with arrow image)
      //backText            : "&laquo;", // Link text used to move the slider back (hidden by CSS, replace with arrow image)


}


function initTabs() {
  $("ul#tabs").tabs("div.panes > div"); //, {effect: 'slide'});
  tabs = $("ul#tabs").data("tabs"); // expose the tabs api for later google maps use
}


function initMaps() {

  if (defaultSite = window.location.hash) {
    defaultSite = defaultSite.replace(/^\#/,'');
  } else {
    for (defaultSite in locations) {}
  }

  var myOptions = {
    zoom: 15,
    mapTypeControl: false,
    center: locations[defaultSite].latLng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  map = new google.maps.Map(document.getElementById('map1'), myOptions);
  
  $.each(locations, function(siteName, site) {
  
    var marker = new google.maps.Marker({
      map: map,
      draggable: false,
      animation: google.maps.Animation.DROP,
      position: site.latLng
    });

    var infowindow = new google.maps.InfoWindow({ content: site.content,position: site.latLng });

    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map);
      map.setCenter(site.latLng);
    });
    
  });

  tabs.onClick(function(event,index) {
  //  for (prop in this.getTabs()) alert (prop);
    var siteName = ''+this.getTabs()[index];
    siteName = siteName.replace(/^.+#/,'');
    map.setCenter(locations[siteName].latLng);
  });


}

function populateLocations () {
    locations = { 
                head: new Object({
                    content: "<h3>Swift Networks</h3><p>Ground Floor<br />201 Adelaide Terrace<br />Perth, 6004<br />Western Australia</p>",
                    latLng: new google.maps.LatLng(-31.959653,115.868753)
                    }), 
                sales: new Object({
                    content: "<h3>Swift Networks</h3><p>Ground Floor<br />201 Adelaide Terrace<br />Perth, 6004<br />Western Australia</p>",
                    latLng: new google.maps.LatLng(-31.959653,115.868753)
                    }) 

    };
}


