/* ============ ENABLED SOLUTIONS - GLOBAL SCRIPTS ============ */

/*
*	Enabled Solutions
*
*   Commenced 2 Jun 2009
*	
*	Copyright 2009 Enabled Solutions PTY LTD
*
*   Author: Thomas Bland
*/

var $j = jQuery.noConflict();
     
// Use jQuery via $j(...)

$j(function(){
	Cufon.replace('ul#globalnav li a, ul#footernav li a, h2, h3, h4, a.k2ReadMore, a.arrow, a.calltoact, ul.navigation li a, address, a#games_link', {
		hover: true
	});
});

// Contact form

// Validation

$j(function(){
	$j('#contact_form_body').validate({ rules: {
		name: "required",
		email: {
       		required: true,
       		email: true
     		}
	}, messages:{
			name: "Please enter your name",
     		email: {
      			required: "Please enter your email address",
       			email: "Please enter a valid email address"
     		}
		}
	});
});

// Clears fields on focus
$j(function(){
	$j('div#contact_form input').bind("focus", function(){
		var val = $j(this).val();
	
		if(val == "Your name" || val == "Your email" || val == "Message subject")
			{
				$j(this).val("");
			}
	});
});


// Form submission
$j(function(){
	$j('input#submit').bind('click' , function(){
		var nameVal = $j('input#name').val();
		var emailVal = $j('input#email').val();
		var subjectVal = $j('input#subject').val();
		var textVal = $j('textarea#text').val();
		
		if( $j('#contact_form_body').valid() )
		{
			$j('div#contact_form form').slideUp(300 , function(){
				$j('p.confirm').slideDown(100 , function(){
	
						$j.post("http://www.enabled.com.au/contact_submit.php" , {
						name: nameVal,
						email: emailVal,
						subject: subjectVal,
						text: textVal
	
					});
				});
			});
			return false;
		}
		else {
			return false;
		}
	});
});

// Homepage hero area - Thanks to Adam Judd - www.kopymark.com


  var slider =  null;
  var controls = null;
  var current = null;
  var currentPosition = -1;
  var timeout = null;
  
  var prepare = function() {
    $j("#hero").wrap("<div id='sliderCrop'></div>");
    // Set up the slider
    slider = $j("#hero li");
    $j("#hero ul").css({width: (slider.length * 980) + "px"});
    // Insert the paging controls
    controlCode = '<ul id="sliderControls">';
    tooltipCode = '';
    for (var x = 0; x < slider.length; x++) {
      controlCode += '<li class="nav"><a class="control'+x+'" href="#'+x+'">'+(x+1)+'</a></li>';
    }
    controlCode += "</ul>";
    controlCode += tooltipCode;
    controls = $j(controlCode);
    $j("#sliderCrop").append(controls);
    controls.click(click);
   
      
    };
  
  var goToCurrent = function(goTo) {
    animating = true;
    $j("#sliderControls a").removeClass('current');
    
    if (goTo != undefined) {
      currentPosition = parseInt(goTo);
    } else if (currentPosition == (slider.length - 1)) {
      currentPosition = 0;
    } 
    else {
      currentPosition += 1;
    }
    
    timeout = window.setTimeout("goToCurrent()", 7000);
    $j("#hero ul").animate({left: (currentPosition * (-980))+"px"}, 1000, 'easeInOutExpo', animationComplete );
  };
  
  var animationComplete = function() {
    animating = false;
    $j(".control"+currentPosition).addClass('current');
  };
  
  var click = function(e) {
    if (!animating) {
    
      var target = e.target;
      target = target.href.match(/\#(\S+)$/)[1];
      window.clearTimeout(timeout);
      goToCurrent(target);
    }
    return false;
  };
  
  // Fire it all off
  $j(document).ready(prepare);
  $j(window).load(function() {
    goToCurrent();
  });

/*

$j(function(){
	$j('div#hero').heroSlide();
});

*/



// Adds body classes for services, contact pages 

$j(function(){
	$j('div.services').parents('body').addClass("services");
});


$j.fn.bodyClass = function(){
	var sel = $j('a.selected').attr('href');
	var myString=new Array("#enabled","#strategy", "#design","#motion","#online","#entertain");	

	if (sel == myString[1] || sel == myString[2] || sel == myString[3] || sel == myString[4] || sel == myString[5] ){
		$j('body').addClass("bg2");
	};
	
	 if (sel == myString[0]){
		$j('body').removeClass("bg2");
	};
	
	if (sel == myString[0] || sel == myString[1] || sel == myString[2] || sel == myString[3] || sel == myString[4] ){
		$j('a.scrollRight').removeClass("hide");
	};
	
	if (sel == myString[5]){
		$j('a.scrollRight').addClass("hide");
	}
	
};


$j(function(){
	$j('div#contact_page').parents('body').addClass("contact");
});



// SERVICES, TEAM SCROLLERS

// when the DOM is ready...
$j(document).ready(function () {

    var panels = $j('#slider .scrollContainer > div');
    var container = $j('#slider .scrollContainer');

    // if false, we'll float all the panels left and fix the width 
    // of the container
    var horizontal = true;

    // float the panels left if we're going horizontal
    if (horizontal) {
        panels.css({
            'float' : 'left',
            'position' : 'relative' // IE fix to ensure overflow is hidden
        });

        // calculate a new width for the container (so it holds all panels)
        container.css('width', panels[0].offsetWidth * panels.length);
    }

    // collect the scroll object, at the same time apply the hidden overflow
    // to remove the default scrollbars that will appear
    var scroll = $j('#slider .scroll').css('overflow', 'hidden');

    // apply our left + right buttons
    scroll
        .before('<a class="scrollButtons scrollLeft" href="">Left</a>')
        .before('<a class="scrollButtons scrollRight" href="">Right</a>');

    // handle nav selection
    function selectNav() {
        $j(this)
            .parents('ul:first')
                .find('a')
                    .removeClass('selected')
                .end()
            .end()
            .addClass('selected');

    }

    $j('#slider .navigation').find('a').click(selectNav);

    // go find the navigation link that has this target and select the nav
    function trigger(data) {
    	
        var el = $j('#slider .navigation').find('a[href$="' + data.id + '"]').get(0);
        selectNav.call(el);
		$j(this).bodyClass();
    }

    if (window.location.hash) {
        trigger({ id : window.location.hash.substr(1) });
        
    } else {
        $j('ul.navigation a:first').click();
    }

    // offset is used to move to *exactly* the right place, since I'm using
    // padding on my example, I need to subtract the amount of padding to
    // the offset.  Try removing this to get a good idea of the effect
    var offset = parseInt((horizontal ? 
        container.css('paddingTop') : 
        container.css('paddingLeft')) 
        || 0) * -1;


    var scrollOptions = {
        target: scroll, // the element that has the overflow

        // can be a selector which will be relative to the target
        items: panels,

        navigation: '.navigation a',

        // selectors are NOT relative to document, i.e. make sure they're unique
        prev: 'a.scrollLeft', 
        next: 'a.scrollRight',

        // allow the scroll effect to run both directions
        axis: 'x',

        onAfter: trigger, // our final callback

        offset: 0,
        
        /*
force: true,
        
        interval: 500,
*/

        // duration of the sliding effect
        duration: 500,

        // easing - can be used with the easing plugin: 
        // http://gsgd.co.uk/sandbox/jquery/easing/
        easing: 'easeInOutQuad'
    };


    // apply serialScroll to the slider - we chose this plugin because it 
    // supports// the indexed next and previous scroll along with hooking 
    // in to our navigation.
    $j('#slider').serialScroll(scrollOptions);

    // now apply localScroll to hook any other arbitrary links to trigger 
    // the effect
    $j.localScroll(scrollOptions);

    // finally, if the URL has a hash, move the slider in to position, 
    // setting the duration to 1 because I don't want it to scroll in the
    // very first page load.  We don't always need this, but it ensures
    // the positioning is absolutely spot on when the pages loads.
    scrollOptions.duration = 1;
    $j.localScroll.hash(scrollOptions);
    
    // Shows slider and hides loader
    $j('div#slider.services').css("visibility" , "visible");
	$j('div#loader').hide();
});






