//AJAX IMAGES

function cancel_anims()
{
	 $('#work_a_images').cycle('pause');
	 $('#work_b_images').cycle('pause');
	 $('#work_c_images').cycle('pause');
	 $('#work_d_images').cycle('pause');
	 $('#work_e_images').cycle('pause');
	 $('#work_f_images').cycle('pause');
	 $('#work_g_images').cycle('pause');
	 $('#work_h_images').cycle('pause');
	 $('#work_i_images').cycle('pause');
}
$(document).ready(
	function(){
	$("a[@href=#work_a]").click(function(){
		$("#work_a_images").load("includes/work_a_images.php",{},function(){
			cancel_anims();
			$('#work_a_images').cycle('resume');
			$('#work_a_images').cycle({
		    fx:    'fade', 
		    speed:  2000 ,
			timeout:  900
		    });
		});
		
	});
	$("a[@href=#work_b]").click(function(){
		$("#work_b_images").load("includes/work_b_images.php",{},function(){
			cancel_anims();
			$('#work_b_images').cycle('resume');
			$('#work_b_images').cycle({
		    fx:    'fade', 
		    speed:  2000,
			timeout:  900
		    });
		});
		
	});
	$("a[@href=#work_c]").click(function(){
		$("#work_c_images").load("includes/work_c_images.php",{},function(){
			cancel_anims();
			$('#work_c_images').cycle('resume');
			$('#work_c_images').cycle({
		    fx:    'fade', 
		    speed:  2000,
			timeout:  900
		    });
		});
		
	});
	$("a[@href=#work_d]").click(function(){
		$("#work_d_images").load("includes/work_d_images.php",{},function(){
			cancel_anims();
			$('#work_d_images').cycle('resume');
			$('#work_d_images').cycle({
		    fx:    'fade', 
		    speed:  2000,
			timeout:  900
		    });
		});
		
	});
	$("a[@href=#work_e]").click(function(){
		$("#work_e_images").load("includes/work_e_images.php",{},function(){
			cancel_anims();
			$('#work_e_images').cycle('resume');
			$('#work_e_images').cycle({
		    fx:    'fade', 
		    speed:  2000,
			timeout:  900
		    });
		});
		
	});
	$("a[@href=#work_f]").click(function(){
		$("#work_f_images").load("includes/work_f_images.php",{},function(){
			cancel_anims();
			$('#work_f_images').cycle('resume');
			$('#work_f_images').cycle({
		    fx:    'fade', 
		    speed:  2000,
			timeout:  900
		    });
		});
		
	});
	$("a[@href=#work_g]").click(function(){
		$("#work_g_images").load("includes/work_g_images.php",{},function(){
			cancel_anims();
			$('#work_g_images').cycle('resume');
			$('#work_g_images').cycle({
		    fx:    'fade', 
		    speed:  2000,
			timeout:  900
		    });
		});
		
	});
	$("a[@href=#work_h]").click(function(){
		$("#work_h_images").load("includes/work_h_images.php",{},function(){
			cancel_anims();
			$('#work_h_images').cycle('resume');
			$('#work_h_images').cycle({
		    fx:    'fade', 
		    speed:  2000,
			timeout:  900
		    });
		});
		
	});
	$("a[@href=#work_i]").click(function(){
		$("#work_i_images").load("includes/work_i_images.php",{},function(){
			cancel_anims();
			$('#work_i_images').cycle('resume');
			$('#work_i_images').cycle({
		    fx:    'fade', 
		    speed:  2000,
			timeout:  900
		    });
		});
		
	});
	
});





function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);




// SLIDING FUNCTIONS

jQuery(function( $ ){
	
	/**
	 * Most jQuery.localScroll's settings, actually belong to jQuery.ScrollTo, check it's demo for an example of each option.
	 * @see http://www.freewebs.com/flesler/jQuery.ScrollTo/
	 * You can use EVERY single setting of jQuery.ScrollTo, in the settings hash you send to jQuery.LocalScroll.
	 */
	
	var target = $('#content').get(0);//the scrolled div
	
	/**
	 * restart the scroll position to ( 0, 0 ) (Firefox doesn't reset it)
	 * could use $(target).scrollTo( 0, {axis:'xy'));
	 * but this needs to be quick(synchronous), to reset before $.localScroll.hash() begins
	 */
	target.scrollLeft = target.scrollTop = 0;
	
	//scroll initially if there's a hash (#something) in the url 
	$.localScroll.hash({
		target: target, //could be a selector or a jQuery object too.
		axis:'xy',//the default is 'y'
		queue:true,
		duration:1500
	});
	
	var $last = $([]);//save the last link
	
	/**
	 * NOTE: In the former version of the demo, I called $('#navigation').localScroll()
	 * Now I want to also affect the >> and << links, so I'll use $.localScroll() instead
	 */
	$.localScroll({
		target: target, //could be a selector or a jQuery object too.
		axis:'xy',//the default is 'y'
		queue:true,
		duration:1000,
		hash:false,	
		onBefore:function( e, anchor, $target ){//'this' is the clicked link
			$last.removeClass('scrolling');
			$last = $(this).addClass('scrolling');
			this.blur();//remove the awful outline
		}
	});
});
		
// FORM VALIDATION
       
$(document).ready(function() { 
	$('#validation').hide();
	$('#confirmation').hide();
    var options = { 
        target:        '#confirmation',   // target element(s) to be updated with server response 
        beforeSubmit:  validate,  // pre-submit callback 
        success:       function() { 
        	$('#validation').fadeOut('slow');
        	$('#commentForm').fadeOut('slow');
            $('#confirmation').fadeIn('slow'); 
        }  
    }; 
    $('#commentForm').ajaxForm(options); 
}); 
 
function validate(formData, jqForm, options) { 
    
 	$("#validation").html('');
    var form = jqForm[0]; 
    err="";
    if (!form.name.value)
    {
    	err+="<li>Please tell us your name, or we'll just have to call you Bob!</li>";
    }
    if (!form.email.value)
    { 
    	err+="<li>We'll be needing a valid e-mail address or phone number please.</li>";
    }
    if (!form.comment.value) 
    { 
        err+="<li>You forgot to let us know what you wanted! Please complete the big white box.</li>";
    } 
    if (err>'')
    {
    	$("#validation").append("<ul>" + err + "</ul>");
        $("#validation").slideDown();
        return false; 
    }
     
}

$(document).ready(function(){
	
	$("#style_switch").hover(
		function(){
			$(this).children("ul").show();
		},
		function(){
			$(this).children("ul").hide();
		}
	);

	$('.styleswitch').click(function()
	{
		setActiveStyleSheet(this.getAttribute("rel"));
		return false;
	});
	
	setActiveStyleSheet('global');

//Everything from here down is new, can be removed

	$('.primary li a').click(function()
	{
		$(this).parents('li').children('ul').slideDown();
		$(this).parents('li').siblings('li').children('ul').slideUp();
		$(this).parents('li').siblings('li').children('a').removeClass('scrolling');
	});
	
	$('ul.selected li a').click(function()
	{
		$('.primary li.home a').removeClass('scrolling');
		$('.primary li.work ul').slideDown();
		$('.primary li.work').siblings('li').children('ul').slideUp();
	});
	
	$('.primary li.work a').click(function()
	{
		$(this).next('ul').children('li').children('a').removeClass('scrolling');
	});
	
	$("a[@href=#work_a]").click(function(){
		$('.primary li.work ul li a.link_work_a').addClass('scrolling').parents('li').siblings('li').children('a').removeClass('scrolling');
	});
	$("a[@href=#work_b]").click(function(){
		$('.primary li.work ul li a.link_work_b').addClass('scrolling').parents('li').siblings('li').children('a').removeClass('scrolling');
	});
	$("a[@href=#work_c]").click(function(){
		$('.primary li.work ul li a.link_work_c').addClass('scrolling').parents('li').siblings('li').children('a').removeClass('scrolling');
	});
	$("a[@href=#work_d]").click(function(){
		$('.primary li.work ul li a.link_work_d').addClass('scrolling').parents('li').siblings('li').children('a').removeClass('scrolling');
	});
	$("a[@href=#work_e]").click(function(){
		$('.primary li.work ul li a.link_work_e').addClass('scrolling').parents('li').siblings('li').children('a').removeClass('scrolling');
	});
	$("a[@href=#work_f]").click(function(){
		$('.primary li.work ul li a.link_work_f').addClass('scrolling').parents('li').siblings('li').children('a').removeClass('scrolling');
	});
	$("a[@href=#work_g]").click(function(){
		$('.primary li.work ul li a.link_work_g').addClass('scrolling').parents('li').siblings('li').children('a').removeClass('scrolling');
	});
	$("a[@href=#work_h]").click(function(){
		$('.primary li.work ul li a.link_work_h').addClass('scrolling').parents('li').siblings('li').children('a').removeClass('scrolling');
	});
	$("a[@href=#work_i]").click(function(){
		$('.primary li.work ul li a.link_work_i').addClass('scrolling').parents('li').siblings('li').children('a').removeClass('scrolling');
	});
	
	$("a[@href=#about_alex]").click(function(){
		$('.primary li.about ul li a[@href=#about_alex]').addClass('scrolling').parents('li').siblings('li').children('a').removeClass('scrolling');
	});
	$("a[@href=#about_dave]").click(function(){
		$('.primary li.about ul li a[@href=#about_dave]').addClass('scrolling').parents('li').siblings('li').children('a').removeClass('scrolling');
	});
	
	$('.primary li.home a').addClass('scrolling');
	
	$('h1 a').click(function()
	{
		$('.primary li ul').slideUp();
		$('.primary li.home a').addClass('scrolling');
	});
	
	$('div#topbar a.button').click(function()
	{
		$('div.clients').slideToggle();
		$(this).toggleClass('panel-down');
	});
	
	$('div.worksection div div.three_col div').hide();
	
	$('div.worksection div div.one_col p.testimonial a').click(function()
	{
		$('div.worksection div div.three_col div').slideToggle('fast');
	});
	

	

//don't delete that thing below though.

});


$(document).ready( function() {
   
    $('a[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });
   
});