/* Author: 
Riaan Pietersen
Etienne Olivier
*/
var positionCount = 0;
var totalItems = 0;
var offset = 0;
var prevItem = 0;

// INIT functions
function setupScroller(){
	if (!exists('#content_area_slider')) return;
	// Get total number of elements
	totalItems = jQuery('#content_area_slider').children().length;
	offset = parseFloat(jQuery('#content_area_slider').css('margin-left'));
	assignButtonActions();
	createJumpMenu();
	scrollComplete();
}

function modalClicks(){
	jQuery('#content_area_slider a[rel|="nofollow"]').each(
														
    function(i){
		//alert (jQuery(this).attr('href'));
		jQuery(this).click(function(e){
			e.preventDefault();
			showContent(jQuery(this).attr('href'));
		});
    }
  );
  
	jQuery('#content_vacancies a[rel|="nofollow"]').each(
														
    function(i){
		//alert (jQuery(this).attr('href'));
		jQuery(this).click(function(e){
			e.preventDefault();
			showContent(jQuery(this).attr('href'));
		});
    }
  );  
}

function assignButtonActions(){
	jQuery('#nextBtn').click(function(e){
        e.preventDefault();
		
		if (positionCount >= totalItems-1) {
			return;
		}
	
		resetItem();
		positionCount++;
        scrollItems();
      });
	
	jQuery('#prevBtn').click(function(e){
        e.preventDefault();
		if (positionCount == 0) {
			return;
		}

		resetItem();
		positionCount--;
        scrollItems();
      });
}

// This makes the sidebar links clickable and the overlays
function createJumpMenu(){
	jQuery('#scrollerMenu li a').each(
    function(i){
      jQuery(this).click(function(e){
        e.preventDefault();
		resetItem();
		var pos = jQuery(this).attr('id').split("_");
        positionCount = pos[1];
        scrollItems();
      });
    }
  );
	
  jQuery('#content_area_slider .block .block_overlay').each(
    function(i){
      jQuery(this).click(function(e){
        e.preventDefault();
		resetItem();
		var pos = jQuery(this).parent().attr('id').split("_");
        positionCount = pos[1];
        scrollItems();
      });
    }
  );
}


function scrollComplete(){
	var curritem = '#item_'+positionCount+' div.block_overlay';
	jQuery(curritem).fadeOut();
	jQuery('#item_'+positionCount).removeClass('inactive');
}

function resetItem(){
	var curritem = '#item_'+positionCount+' div.block_overlay';
	jQuery(curritem).fadeIn();
	jQuery('#item_'+positionCount).addClass('inactive');
}

function scrollItems(){
	if (positionCount < 0) {
		positionCount = 0;
		return;
	}
	if (positionCount >= totalItems) {
		positionCount = totalItems-1;
		return;
	}
	var jumpsize = offset+(236*positionCount*-1);
	jQuery('#content_area_slider').stop().animate({'marginLeft':jumpsize},
										   250,
										   function(){
											   scrollComplete();
										   });
}

// Modal
function showContent(src){

	if (src.indexOf("?") == -1) {
		src = src + '?noextras=yes';
	} else {
		src = src + '&noextras=yes';
	}

	jQuery.modal('<iframe src="' + src + '" height="550" width="980" style="border:0" scrolling="auto">', {
		onOpen: function (dialog) {
			dialog.overlay.fadeIn('slow', function () {
				dialog.data.hide();
				dialog.container.fadeIn('slow', function () {
					dialog.data.slideDown('slow');
				});
			});
		},
		//closeHTML:"",
		/*containerCss:{
			backgroundColor:"#fff",
			borderColor:"#fff",
			height:550,
			padding:0,
			width:1000
		},*/
		overlayClose:true,
		opacity:80,
		overlayCss: {backgroundColor:"#666"}
	});
}

function exists(target) {
  if (jQuery(target).length > 0) return true;
  else return false;
}


function openMenu(){
	$('#header').animate({top: '0'}, 250, function() {});
}
function closeMenu(){
	$('#header').animate({top: '-45'}, 250, function() {});
}

function modalJumpTo(jump_to) {
		if (jQuery('#scrollerMenu li a').length>0) {
			jQuery('#scrollerMenu li a').each(function(index) {
			    if(this.href.indexOf(jump_to)!=-1) {
			    	showContent(this.href);
			    	
			    	var pos = jQuery(this).attr('id').split("_");
	        		positionCount = pos[1];
			    	scrollItems();
			    }
			});
		} else if (jQuery('a.readmore').length>0) {
			jQuery('a.readmore').each(function(index) {
			    if(this.href.indexOf(jump_to)!=-1) {
			    	showContent(this.href);
			    	
			    }
			});
		}
		
}

$(document).ready(function(){

	/* OPEN / CLOSE header */
	var config = {over:openMenu, timeout: 500, out: closeMenu,interval:200,sensitivity:10};
	$("#header").hoverIntent(config);
	header_top_position = $("#header").position().top;
	$("a.menu_switch").click(function (e) {
		e.preventDefault();
		if ($("#header").position().top == header_top_position) {
			$("#header").css({'top': 0});
		} else {
			$("#header").css({'top': header_top_position});
		}
	});

	/* OPEN / CLOSE sitemap */
	$("a.sitemap_switch").click(function (e) {
		e.preventDefault();
		$("#sitemap_open").toggle();
	});
	
	$("a.close").click(function (e) {
		e.preventDefault();
		$("#sitemap_open").toggle();
	});
	
	//
	setupScroller();
	//
	modalClicks();
	
	if(window.location.hash) {
  		modalJumpTo(window.location.hash.substring(1, window.location.hash.length));
	} 

}); // document ready























