var map = null;
var marker = null;
var mapPoint = null;

// Share This Visible
var shareThisVis = false;

// Sets the map
function initMapDisplay(lat, lng, title, address) {
	
	if (GBrowserIsCompatible()) {
		
		mapPoint = new GLatLng(lat, lng);
		
		// Set the map options
		map = new GMap2(document.getElementById("map-display"));
		map.setCenter(mapPoint, 14);
		map.setUIToDefault();
		map.disableScrollWheelZoom();
		
		marker = new GMarker(mapPoint);
		map.addOverlay(marker);
		marker.openInfoWindowHtml(title + "<br />" + address);
		
	}
}

/**
 * Adds the region to the google map
 */
function addRegionMarker(lat, lng, regionInfo) {
	
	if (GBrowserIsCompatible()) {

		if(map == null) {
			mapPoint = new GLatLng(-26.431228, 133.945313);
			
			// Set the map options
			map = new GMap2(document.getElementById("region-map-display"));
			map.setCenter(mapPoint, 4);
			map.setUIToDefault();
			map.disableScrollWheelZoom();
		}
		
		var regionMapPoint = new GLatLng(lat, lng);	
		var regionMarker = new GMarker(regionMapPoint);
		
		GEvent.addListener(regionMarker, "click", function() {
        	regionMarker.openInfoWindowHtml(regionInfo);
        });
		
		map.addOverlay(regionMarker);
		//regionMarker.openInfoWindowHtml(name);
		
	}
	
}

/**
 * Perform stuff when document is ready
 */
jQuery(document).ready(function() {

    /**
    * Load the image gallery hooks
    */
    galleryOnLoad("#article-image-gallery");
    galleryOnLoad("#wineries-content .image-gallery");

    // Set the tabs
    jQuery("#tabulated-content div.tab-item-content").hide();
    jQuery("#tabulated-content div:first").show();

    jQuery("#section-header ul li .share-this").click(function() {

        if (shareThisVis) {

            jQuery("#share-this-links").removeClass("share-this-links-active");
            shareThisVis = false;

        } else {

            jQuery("#share-this-links").addClass("share-this-links-active");
            shareThisVis = true;

        }

        return false;
    });

    // brand tabs
    // CB: gotta use CSS display rather than show/hide due to a bug in IE8 that buggers the redraw when you expand the bottom item
    jQuery("#brand-tabs .tab-container .tab-content").addClass("indented").css("display", "none");
    jQuery("#brand-tabs .tab-container:first .tab-content").css("display", "block");
    jQuery("#brand-tabs .tab-container:first .tab-header a").addClass("active");
    jQuery("#brand-tabs .tab-container .tab-header").css("display", "block");

});

/**
 * Closes the Share This box when called.
 */
function closeShareThis() {
	jQuery("#share-this-links").removeClass("share-this-links-active");
	shareThisVis = false;
}

/**
* Handles the loading & interaction of the gallery items
*/
function galleryOnLoad(initialControl) {

	if (jQuery(initialControl).length == 0) {
		return;
	}

	// Set the initial gallery index
	jQuery("#hdnGalleryIndex").val("0");

	var galIndex = parseInt(jQuery("#hdnGalleryIndex").val());

	// Hide the backbutton first
	jQuery(initialControl + " #the-controls .left").hide();

	//alert(initialControl);
	
	// Set the initial image
	setGalleryImage(galIndex, initialControl, true);

	// Get the amount of images in the list
	var imageCount = jQuery(".gallery-images li").length;
	
	if (imageCount == 1) {

		// Hide the controls box
		jQuery(initialControl + " #the-controls").hide();

	} else {

		// Set the next button event
		jQuery(initialControl + " #the-controls .next").click(function() {
	
			// Get the gallery index
			var galIndex = parseInt(jQuery("#hdnGalleryIndex").val());
	
			// Increment the index
			galIndex = (galIndex + 1);
	
			// set the gallery image
			setGalleryImage(galIndex, initialControl, false);
	
			// If where at the last image , hide the next button
			if ((galIndex + 1) >= imageCount) {
				jQuery(initialControl + " #the-controls .right").fadeOut(200);
			}
	
			// Set the current gallery index
			jQuery("#hdnGalleryIndex").val(galIndex);
	
			// Do not execute the link...
			return false;
	
		});
	
		// Set the next button event
		jQuery(initialControl + " #the-controls  .back").click(function() {
	
			// Get the gallery index
			var galIndex = parseInt(jQuery("#hdnGalleryIndex").val());
	
			// Increment the index
			galIndex = (galIndex - 1);
	
			// If where at the last image , hide the next button
			if (galIndex <= 0) {
				jQuery(initialControl + " #the-controls .left").fadeOut(200);
			}
			
			// If were not the last image, show the next button, if it has been hidden before
			if ((galIndex + 1) < imageCount) {
				jQuery(initialControl + " #the-controls .right").fadeIn(200);
			}
			
			// set the gallery image
			setGalleryImage(galIndex, initialControl, false);
	
			// Set the current gallery index
			jQuery("#hdnGalleryIndex").val(galIndex);
	
			// Do not execute the link...
			return false;
	
		});
		
	}

}

// Sets the gallery image
function setGalleryImage(galIndex, initialControl, initalImage) {

	// Show the back-next buttons
	if (galIndex > 0) {
		jQuery(initialControl + " #the-controls .left").fadeIn(200);
	}

	// Get the first image from the image gallery.
	var theImage = jQuery(".gallery-images li:eq(" + galIndex + ") a").clone(true);

	// Se the title of the image
	var theTitle = jQuery(".gallery-images li:eq(" + galIndex + ") a img").attr("alt");

	if (!initalImage) {

		// If not IE
		if (navigator.appName != "Microsoft Internet Explorer") {

			// Fade Out the image and text
			jQuery("#the-image").fadeOut(300, function() {

				// Loads the image gallery image
				jQuery("#the-image").html(theImage);

			});
			jQuery("#the-title").fadeOut(300);

			// Fade In the image and text
			jQuery("#the-image").fadeIn(300);

		} else {

			// We are ie, so just replace.
			// Loads the image gallery image
			jQuery("#the-image").html(theImage);

		}
		
		jQuery("#the-image a").click(function() {
			NOWbox.view(this.href, this.title, this.rel);
		});

	}

}

function setTab(tabItem, caller) {

	var tabSelector = "#tabulated-content div." + tabItem;
	
	// Hide all the tabs, and show the relevant
	jQuery("#tabulated-content div.tab-item-content").hide();
	jQuery(tabSelector).show();
	
	jQuery("#tabulated-content .tabs-container a").removeClass("active");
	jQuery(caller).addClass("active");

    // do the sifr again (doesn't work in tabs)
	sIFR.replace(tahoma, {
	    selector: 'body #tabulated-content h3',
	    css: '.sIFR-root { color: ' + highlightColor + '; display: block; }',
	    wmode: 'transparent'
	});
}

function setBrandTab(tabItem, caller) {

    var tabSelector = "#brand-tabs .tab-container." + tabItem;

    // hide all content tabs
    jQuery("#brand-tabs .tab-container .tab-content").hide();

    // now show the relevant one
    jQuery(tabSelector + " .tab-content").show();

    // add active class
    jQuery("#brand-tabs .tab-container .tab-header a").removeClass("active");
    jQuery(caller).addClass("active");

    // do the sifr again (doesn't work in tabs)
    sIFR.replace(tahoma, {
        selector: 'body #brand-tabs .tab-content h3',
        css: '.sIFR-root { color: ' + highlightColor + '; display: block; }',
        wmode: 'transparent'
    });
}

var McWilliam = McWilliam || {};

McWilliam.Timeline = {
	init : function(){
		if(jQuery('#timeline')){
			
			/*hide overflow*/
			jQuery('#timeline-inner').css('overflow', 'hidden');
			
			//set ul li height to 100%
			jQuery('#timeline > li').css('height', '100%');
			
			//fix z-indexes for IE
			var zind = jQuery('#timeline li').length;			
			for(ii = 0, ll = jQuery('#timeline li').length; ii < ll; ii++){
					jQuery('#timeline li:eq('+ii+')').css('z-index', zind);
					zind = zind + 1;					
			}
			
			//move divs vertically depending on their index
			var top = 10;
			for(i = 0, l = jQuery('.timeline-item').length; i < l; i++){
				
				//if no img, delete img div and remove margin-top from date
				if(jQuery('.timeline-item:eq('+i+') img').length === 0){
					jQuery('.timeline-item:eq('+i+') .timeline-thumb').remove();
					jQuery('.timeline-item:eq('+i+') .timeline-date').css('margin-top', 0);
				}
				
				jQuery('.timeline-item:eq('+i+')').css('top', top + 'px');
				
				//every 5 divs, restart from top and work way down
				if(i % 5 == 0 && top !== 10){
					top = 10;
				} else {				
					top += jQuery('.timeline-item:eq('+i+')').height() + 10;
				}
			}
			
			if (jQuery('#timeline').length > 0) {
			
				//define draggability
				jQuery('#timeline').draggable({ axis: 'x' });
				jQuery('#timeline').draggable('option', 'grid', [111, 0]);
				jQuery('#timeline').draggable('option', 'cursor', 'col-resize');
				jQuery('#timeline').bind('drag', function(){
				});
				
				//check if ul is out of bounds when dragging is stopped
				jQuery('#timeline').bind('dragstop', function(){
					var left = parseInt(jQuery(this).css('left'));
					if(left > 0){
						jQuery('#timeline').css('left', 0 + 'px');
					} else if(left < -(jQuery('#timeline').width() - jQuery('#timeline-inner').width())){
						jQuery('#timeline').css('left', -(jQuery('#timeline').width() - jQuery('#timeline-inner').width()) + 'px');	
					}
				});
				
			}
			
		}
	}
}

jQuery(document).ready(function(){
				
	McWilliam.Timeline.init();	
				
});