/*
 * Share Button fot STANDOUTUK.com
 * Author: Aaron Yates
 * Email: aaron@standoutuk.com
 */

function saturate(img) {
	var img2 = Pixastic.process(img, "desaturate");
	img2.onmouseover = function() {
		Pixastic.revert(this);
	}
}

$(document).ready(function() {
	
	/*
	// REPLACED WITH STO_Slideshows plugin - Mat Marlow 18/03/2010
	$('ul.homePanel').innerfade({
				speed: 'normal',
				timeout: 4000,
				type: 'sequence',
				containerheight: '286px'
	});
	*/
	
	$("ul#social li p").each(function(i) {
		$(this).hide();
	});
	
	$("ul#social li").find("a.share_link").each(function(n) {
		
		$(this).click(function() {
			$("ul#social li p").each(function(j) {
				$(this).slideUp("fast");
			});
			
			if ( $(this).parent().next().next().hasClass('active') ) {
				$(this).parent().next().next().removeClass('active');
				$(this).removeClass('active');
				$(this).parent().next().next().slideUp("fast");
				
			} else {
				$("ul#social li p").each(function(a) {
					$(this).removeClass('active');
				});
				
				$("ul#social li span a.share_link").each(function(a) {
					$(this).removeClass('active');
				});
				
				$(this).parent().next().next().addClass('active');
				$(this).addClass('active');
				$(this).parent().next().next().slideDown("fast");
			}
			
			return false;
		});
	});
	
	/* Build the Our Work filters */
	$("a.ow_filter_client").mouseover(function() { 
		buildFilter($(this), 'client', filter_opts);
	});
	
	$("a.ow_filter_discipline").mouseover(function() { 
		buildFilter($(this), 'discipline', filter_opts);
	});
	
	$("a.ow_filter_sector").mouseover(function() { 
		buildFilter($(this), 'sector', filter_opts);
	});
	
	/* Fading out slideshow */
	$('ul#imgViewer').innerfade({
				speed: 'normal',
				timeout: 4000,
				type: 'sequence',
				containerheight: '250px'
	});
	
	
	/* Desaturate client logos */
	$("img.client_hover_desaturate").each(function() {
		
		var img2 = Pixastic.process(this, "desaturate");
		img2.onmouseover = function() {
			Pixastic.revert(this);
		}
	});
});










function buildFilter(owobj, type, filter_opts) {
	getOpts(type);
	var tab_title = "";
	var shift_widths = "";
	shift_widths = " "+filter_opts;
	//alert("C:"+type+" / FO:"+filter_opts);
	
	if ((type == 'client') && (filter_opts == 'bcc')) {
		tab_title = "Change Client?";
	} else if ((type == 'discipline') && (filter_opts == 'bdf')) {
		tab_title = "Change Discipline?";
	} else if ((type == 'sector') && (filter_opts == 'bss')) {
		tab_title = "Change Sector?";
	} else {
		tab_title = "By "+ucfirst(type);	
	}
	
	var filter1 = '<div class="ow_filter'+shift_widths+'" style="display: none;">'
	var filter2 = '<div class="title '+type+'_title"><span>'+tab_title+'</span></div>';
	var filter3 = '<div class="menu">';
	var filter4 = '<div class="boxed_up">';
	var filter5 = '</div>';
	//var filter4 = '<select id="ow_options" name="ow_options"><option value="na">Please choose a '+ucfirst(type)+'</option>';
	//var filter6 = '</select>';
	var filter7 = '</div>';
	var filter8 = '</div>';
	
	var filter = filter1 + filter2 + filter3 + filter4 + filter5 + filter7 + filter8;
	
	owobj.parent().append(filter);
	$(".ow_filter").fadeIn("fast");
	
	$(".ow_filter .title span").click(function() {
		var currUrl = getCurrUrl();
		window.location = currUrl+"by-"+type+"/";
	});
	
	if ($(".ow_filter").length) {
		$('body').mouseover(function(event) {
			if ( ($(event.target).is('a, .ow_filter .title, .ow_filter .title span, .ow_filter .menu, .ow_filter .boxed_up')) ) {
			//if ( $(event.target).is('.ow_filter div') ) {
				// Nada;
			} else {
				$(".ow_filter").remove();
			}
		}); 
	}
}




/* UPPERCASE FIRST CHARACTER */
function ucfirst(string) {
	return string.charAt(0).toUpperCase() + string.slice(1);
}



/* GET OPTIONS FOR FILTER  */
function getOpts(type) {
	var currUrl = getCurrUrl();
	
	$.getJSON(currUrl+"wp-content/themes/standout/_includes/php/standout_filters.php?type="+type, function(data){
		var mystr = "";
		
		for(var j in data) {
			mystr += '<a href="#'+data[j].safe+'" rel="'+data[j].safe+'">'+data[j].opt+'</a><br />';
		}
		
		$('.ow_filter .menu .boxed_up').append(mystr);
		
		$('.ow_filter .menu a').each(function () {
			$(this).click(function() {
				
				var currUrl = getCurrUrl();
				var redirectString = '';
				
				switch (type) {
					case 'client':
						redirectString = currUrl+'by-client/?client='+$(this).attr('rel');
						break;
					case 'discipline':
						redirectString = currUrl+'by-discipline/?field='+$(this).attr('rel');
						break;
					case 'sector':
						redirectString = currUrl+'by-sector/?sector='+$(this).attr('rel');
						break;
				}
				window.location = redirectString;
				return false;
			});
		});
	});
}


/* RETURN THE CORRECT URL TO USE */
function getCurrUrl() {
	var currUrl = window.location.hostname;
		
	if (currUrl == 'www.stodevelopment.com') {
		currUrl = "http://"+currUrl+"/standout/site/";
	} else {
		currUrl = "http://"+currUrl+"/";
	}
	
	return currUrl;
}


/* SUBSCRIBE TO CAMPAIGN MONITOR */

var CMSubscribeResponses = new Array();
CMSubscribeResponses[0] = "Thanks! You have been added to our mailing list for news updates.";
CMSubscribeResponses[1] = "You have submitted an invalid email address, please try again.";	

function CMSubscribe(form)
{
	var email = $(form).children("#subscribe-email").val();
	if(email==''){
		displayUserMessage("Please enter an email address.");
	}else{
		var currUrl = getCurrUrl();
		var data = {
			email:	email
		}
		$.post(currUrl+"wp-content/themes/standout/_includes/php/campaign_monitor_subscribe.php?ajax=true", data, function(data){
			if(data.Result.Code=='0'){
				location.href = "/thank-you";
			}else{
				displayUserMessage(CMSubscribeResponses[data.Result.Code],'Sorry, there was a problem...');
			}
		}, "json");
	}
}

function displayUserMessage(_message,_title)
{
	var title = typeof _title == 'undefined' ? '' : '<h3 class="grey">'+_title+'</h3>';
	var message =  typeof _message == 'undefined' ? '' : '<p class="normal">'+_message+'</p>';
	//alert(message);
	
	var buttonMarkup = '';//'<button type="button" class="ok" onclick="$.fancybox.close();">Close</button></div>';

	$.fancybox({
		'content'		: '<div align="center" class="fancyBoxContent">' + title + message + buttonMarkup
		,'showCloseButton'	: true
		,'onComplete'		: function(){Cufon.refresh();}
		,'scrolling'		: false		
		,'transitionIn'		: 'none'
		,'transitionOut'	: 'none'

	});

	
}