// === Cufon Font replace === */

Cufon.set('fontFamily', 'Adobe Caslon Pro').replace;
Cufon.set('fontFamily', 'PF Champion Script Pro').replace('.page h3', {textShadow: '1px 1px #333'})('.fader-content h3', {textShadow: '1px 1px #333'})('#news-header h2', {textShadow: '2px 2px #eee'})('#respond h3', {textShadow: '2px 2px #eee'})('.our-work h2', {textShadow: '1px 1px #333'})('#frame-name')('.contact-us h2', {textShadow: '1px 1px #fff'})('.guerilla-award-winners h3', {textShadow: '1px 1px 1px #fff'});

// ========== Smooth Scroll ===========

$(function(){
    $('a[href*=#]').click(function() {
    	if($(this).hasClass('no-scroll'))return true;
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
        && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
                var targetOffset = $target.offset().top;
                $('html,body').animate({scrollTop: targetOffset}, 1000);   
                return false; 
            } 
        } 
    }); 
});

// ============ Limit Textarea ============

$(document).ready(function() {
            $('.limiter').keydown(function() {
                var len = this.value.length;
                if (len >= 348) {
                    this.value = this.value.substring(0, 348);
                }
                $('#charLeft').text(348 - len);
            });
        });

// ============ Radio Click ============ 

$(document).ready(function(){
    $("label.radio").click(function() {
	$("label.active").removeClass("active");
		$(this).addClass("active");
	});
});

// ========== Team Revealer ===========

$(function(){
	//Banner Slider
	$(".team-member div").animate({ opacity: 0}, 200);
	$(".team-member div").hover(function(){
		$(this).stop().animate({opacity:1}, 200);
		$("img", $(this).parent()).stop().animate({opacity:0}, 200);
	}, 
	function() {
		$(this).stop().animate({opacity:0}, 200);
		$("img", $(this).parent()).stop().animate({opacity:1}, 200);
	});
});

// ========== Sub Nav Fade ===========

$(function(){
   $("#nav-wrap li ul").css("opacity","0");
   $("#nav-wrap li").hover(function () {
	$(this).addClass('nav-hover');						 
	   $("#nav-wrap li ul").stop().animate({
			   opacity: 1
	   }, 300);
   },
   function () {
	   $(this).removeClass('nav-hover');
	   $("#nav-wrap li ul").stop().animate({
			   opacity: 0
	   }, 300);
   });
});


// ======== Social Media Links =========

$(function(){
	$("#nav a").css({ opacity: 0.70}, 200);
	$("#nav a").hover(function(){					  
		$(this).stop().animate({ opacity: 1}, 200);
	},
	function(){
		$(this).stop().animate({ opacity: 0.70}, 200);

	});
});

// ========== Search Active ===========

$(function(){
	$("#s").mouseover(function(){
		$(this).addClass("search-active");
	});
});
$(function(){
	$("#s").mouseout(function(){
		$(this).removeClass("search-active");
	});
});

// ========== Portfolio Category ===========

$(function(){
	$("#categories p a").click(function(){
		$("#categories ul").slideDown();
	});
	$("#categories").mouseleave(function(){
		$("#categories ul").fadeOut(function(){
			$(this).hide()
		});
	});		
});

// ========== Navigation Fade ===========

$(function(){
	$("ul#menu-main-navigation li ul li").hover(
		function(){
			$(this).stop().animate({ paddingLeft: "2px"}, 200)
		},
		function(){
			$(this).stop().animate({ paddingLeft: "0"}, 200);
		}
	);
});	

$(function(){
	//$("#nav-wrap").css({ backgroundColor: "#000", borderTop:"1px solid #3e3e3e", opacity: '0.90'}, 200);
	if (!$.browser.msie) {
		$("#nav-wrap").css({ backgroundColor: "#000", borderTop:"1px solid #3e3e3e", opacity: '0.90'});
	}
	else{
		$("#nav-wrap").css({ backgroundColor: "#000", borderTop:"1px solid #3e3e3e"});
	}
	$("#nav-wrap").hover(function(){
		$(this).addClass("nav-hover-fix");						  
		$(this).stop().animate({ backgroundColor: "#000", borderTop:"1px solid #3e3e3e", opacity: '1'}, 200);
	},
	function(){
		$(this).stop().animate({ backgroundColor: "#000", borderTop:"1px solid #3e3e3e", opacity: '0.90'}, 200);
		$(this).removeClass("nav-hover-fix");
	});
});

// ========== The Fader ===============

$(function() {
    $.fn.heroSwap = function(options) {
        var isworking=false;
		// add to defaults
        var o = $.extend({}, $.fn.heroSwap.defaults, options);
        // check we have the right id
        o.container = '#' + $(this).attr('id');
        o.container_static = $(this).attr('id');
        o.animation_ref = o.container_static + '_' + o.animation_ref;

        // some default setting
        var timer, continue_cycle = true, next_hero, prev_hero, current_hero = o.default_hero;
        // list of all the available heroes
        var heroes = $(this).find(o.content_class + " li");
        // list of available nav items
        var navs = $(this).find(o.nav_class + " li");
        // get prev/next
        var nextbtn = $(this.find(o.next));
        var prevbtn = $(this.find(o.prev));

        // swap two images
        var swap = function(a, b) {
            // make sure the new one isnt the original
            if (a != b) {
                isworking=true;
				
				// set the new current hero
                set_active(b);
				
                // easy use
                var fout = $(heroes[a]);
                var fin = $(heroes[b]);
				
                // and the old one out then fade the new item in
                fout.fadeOut(o.fade_time,function(){
					fin.fadeIn(o.fade_time,function(){
						isworking=false;								
					});							  
				});
            }
        }
        // 
        var set_active = function(i) {
            // remove the active state from the current hero
            $(navs[current_hero]).removeClass('main-active');

            // make whatever was clicked the new hero
            current_hero = i;

            // add an active class to the selected item
            $(navs[i]).addClass('main-active');
        }

        // get the next hero
        var get_next_hero = function(i) {
            // get the next index
            var next = current_hero + 1;

            // make sure the next one exists
            if (next + 1 > heroes.length) {
                // if not, start at the start
                next_hero = 0;
            }
            else {
                // otherwise make the next one next
                next_hero = next;
            }
        }

        // get the prev hero
        var get_prev_hero = function(i) {
            var prev = current_hero - 1;
            if (prev < 0) {
                prev_hero = heroes.length - 1;
            }
            else {
                prev_hero = prev;
            }
        }

        // anything to do if auto is set?
        var cycle = function(type) {
            // if we should continue the cycle
            if (continue_cycle == true) {
                // no point fading if theres only one item
                if (heroes.length >= 1) {
                    // get the next hero
                    get_next_hero(current_hero);

                    timer = setTimeout(function() {
                        swap(current_hero, next_hero);
                        cycle();
                    }, o.cycle_delay);
                }
            }
        }

        // if we want to cycle
        if (o.cycle == true) {
            cycle(o.type);
        }
        
        // this was added, incase there was no nav items, needs to be reviewed
        $(this).find(o.content_class + " li").each(function(i) {
            if (i != current_hero) {
                $(heroes[i]).css({
                    'display': 'none'
                }); 
            }
        });

    // get all the nav items
    $(this).find(o.nav_class + " li").each(function(i) {
        $(heroes[i]).css({
            'position': 'absolute',
            'top': '0',
            'left': '0',
			'overflow': 'hidden'
        });
        if (i != current_hero) {
            $(heroes[i]).css({
                'display': 'none'
            });
        }

        // set clickable for each nav item
        $(this).click(function() {
            var clicked_nav = navs.index(this);
            // stop cycling
            if (o.cycle == true) {
                clearTimeout(timer);
                continue_cycle = false;
            }
            // fade them over
            swap(current_hero, clicked_nav);

            // stop the anchor being followed
            return false;
        });
    });

    // trying to do prev/next links
    if (nextbtn != undefined) {
        $(nextbtn).click(function() {
        	if(isworking==true){
				return false;
			}
			// stop cycling
            if (o.cycle == true) {
                clearTimeout(timer);
                continue_cycle = false;
            }
            get_next_hero(current_hero);
            swap(current_hero, next_hero);
            return false;
        });
    }
    if (prevbtn != undefined) {
        $(prevbtn).click(function() {
            if(isworking==true){
				return false;
			}
			if (o.cycle == true) {
                clearTimeout(timer);
                continue_cycle = false;
            }
            get_prev_hero(current_hero);
            swap(current_hero, prev_hero);
            return false;
        });
    }

    // the current hero
    set_active(o.default_hero);

};

$.fn.heroSwap.defaults = {
    default_hero: 0,
    container: "#fader",
    nav_class: ".fader-nav",
    content_class: ".fader-content",
    animation_ref: "in_anim_0o0o",
    cycle: true,
    cycle_delay: 2500,
    type: 'fade',
    fade_time: 1500,
    prev: '#fader-prev',
    next: '#fader-next'
	};
});

// ========== The Portfolio Fader ===============

/*
* @package Guerilla_jQuery_Portfolio_Fader
* 
* Description: Guerilla portfolio fader plugin for jQuery
* Version: 1.0
* Author: Guerilla Communications Ltd.
* Author URI: http://guerilla.co.uk
*/
	
jQuery(function($){

//
	$.fn.slider = function(){
		
		// check for the required jQuery version
		if($.fn.jquery < '1.4.2') {return false}
		
		// extend the default settings
		var defaults = {}	
		
		// set an animation marker
		var isAnimating = false;
		
		// hide all images withinthis instance of the selection
		$(this).find('div.container img').hide();
		
		// show the first image in the selection
		$(this).find('div.container img:first').show().addClass('active');
	
		// add a classname to each image in the container div, 
		// we're starting a fresh loop count here using each
		$(this).find('div.container').each(function(){
			$('img',$(this)).each(function(i){
				$(this).addClass('item-'+i);
			});
		});
		
		// flag as active the first link in the pagination
		$(this).parent().parent().find('div.pagination a:first').addClass('active');
		
		// add an href to each image in the pagination div, 
		// again we're starting a fresh loop count here using each
		$(this).parent().parent().find('div.pagination').each(function(){
			$('a',$(this)).each(function(i){
				$(this).attr('href','#item-'+i);
			});
		});
		
		// attatch an onclick action to each link in the navigation
		$(this).parent().parent().find('div.pagination a').click(function(event){
			
			// stop anchor action
			event.preventDefault();
			
			// dont follow through on any requests until we're done animating the current one.
			if(isAnimating)return false;
			
			// check if this is the active elements, if so take no action
			if($(this).hasClass('active'))return false;
			
			// mark the animation as started
			isAnimating = true;
			
			// update the activa class on the pagination
			$(this).parent().parent().find('a.active').removeClass('active');
			$(this).addClass('active');
			
			//get the target classname from the href
			var target = $(this).attr('href').replace(/#/,'');
			
			// animate out the existing active image and animate in the target image once complete
			// classname active is also removed from the origional image and added to the active one.
			if(false){
				$(this).parent().parent().parent().find('div.container img.active').fadeOut(function(){
					$(this).parent().parent().find('img.'+target).fadeIn().addClass('active');																				 
				}).removeClass('active');
			}else{
				
				// first we need to display the target image and start to slide it in from the east... the overflow hidden
				// on the wrapper acts like a mask on the frame
				$('div.slider img.'+target).show().animate({left:'0'},'normal');
				
				// now the current active exit's stage left and then hides itself.
				$('div.slider img.active').animate({left:'-=586px'},'normal',function(){
					// the hidden image then sneaks over to the right hand side again ready to slide in if needed, then 
					// removed it's active class
					$(this).hide().animate({left:'+=1172px'},1,function(){
						// lastly mark as completed animating
						isAnimating = false;
					});
				}).removeClass('active');
				
				// finaly, set the new image as the active one and everyting gona be airie 
				$('div.slider img.'+target).addClass('active');
				
			}
		});
		
	}

});

/*
* @package Guerilla_jQuery_ajax_fader
* 
* Description: Guerilla ajax'ified fader plugin for jQuery
* Version: 1.0
* Author: Guerilla Communications Ltd.
* Author URI: http://guerilla.co.uk
*/
	
jQuery(function($){

	//
	$.fn.ajaxFader = function(options){
		
		// check for the required jQuery version
		if($.fn.jquery < '1.4.2') {return false}
		
		// debug function...
		var d = function(object){
		//console.debug(object);	
		}
		
		// extend the default settings
		var defaults = $.extend({
			nextlink:'next',
			prevlink:'prev',
			sourceuri:'/',
			categoryslug:''
		}, options);
				
		// attatch an action to both next and previous item buttons/links
		$('#'+defaults.nextlink+', #'+defaults.prevlink).click(function(event){
			
			// stop the anchor action
			event.preventDefault();
			
			// get the target ID
			var targetPost = $(this).attr('href');
			
			// send the AJAX request
			$.ajax({
			  type: 'POST',
			  url: defaults.sourceuri,
			  data: {ajaxfader:targetPost,category:defaults.categoryslug},
			  success: function(data){
				  
				  d(data);
				  
				  // if there was a bad response report the error via common debug
				  if(data==null || data.status != 'ok'){
				  	d('Oops, there was a problem');
				  	return false;
				  }
				  
				  // fade out the slider, remove the content and replace with the new content from the response data
				  // we need to call the slider function for the new content also to enable the gallery slider
				  $('#slider-nav').fadeOut();
				  $('div.slider:first').fadeOut(function(){
				  	$(this).html(data.slider).fadeIn();
					$('#slider-nav').html(data.nav).fadeIn();
				  	$(this).slider();
				  });
				  
				  // fade out the URL and replace with content from request data
				  $('div.portfolio-url:first').fadeOut();
				  
				  // fade out the blockquote, then replace with content from request data
				  $('div.portfolio-quote:first').fadeOut();
				  
				  // fade out the content and replace with content from request data
				  $('div.portfolio-content:first').fadeOut(function(){
				  	$(this).html(data.content).fadeIn();
					
					// portfolio elements need to fade in at the same time as body copy
					$('div.portfolio-url:first').html('');											
					if(data.websiteurl.length>0){										
						$('div.portfolio-url:first').html('<a href="http://'+data.websiteurl+'" title="Visit the website">Visit the website</a>').fadeIn();
					}
					
					// again, portfolio items need to fade in at the same time :)
					if(data.quote.length>0){
						$('div.portfolio-quote:first').html('<blockquote><p>'+data.quote+'<em>'+data.quoteby+'</em></p></blockquote>').fadeIn();
					}else{
						$('div.portfolio-quote:first').html('');
					}
					
				  });
					  
				  // fade out the title and replace with content from request data
				  $('div.portfolio-intro:first h4').fadeOut(function(){
				  	$(this).html(data.title).fadeIn();
				  });
				  
				  // fade out the client and category, then replace with content from request data
				  $('div.portfolio-intro:first h5').fadeOut(function(){
				  	$(this).html('Client: <strong>'+data.client+'</strong><br />Category: <strong>'+data.category+'</strong>').fadeIn();
				  });
				  
				  // update the previous and next links with the data from the request
				  $('#'+defaults.nextlink).attr('href','#'+data.nextlink);
				  $('#'+defaults.prevlink).attr('href','#'+data.prevlink);
				  
			  },
			  dataType: 'json'
			});
			
		});
		
	}

});