/* -------------------------------------------------------------- 
	Add This
-------------------------------------------------------------- */

var addthis_offset_top = 0;
var addthis_header_color = "#FAFAFA";
var addthis_header_background = "#59A600";
var addthis_options = 'email, favorites, facebook, myspace, stumbleupon, bebo, delicious, google, wordpress, blogger, tumblr, typepad, more';//friendfeed, technorati, digg, newsvine, reddit, 
var addthis_localize = {
    share_caption: "≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈"
}; 

function sharify()
{
	$('li.share a').click(function () {
		$(this).blur();
		var pid= $(this).parent().parent().parent().attr('id');
		if(pid == 'hi') addthis_offset_top = -50;
		if(pid == 'lo') addthis_offset_top = 1;
		return addthis_open(this, '', '[URL]', '[TITLE]');
  });
}

/* -------------------------------------------------------------- 
	Flyouts
-------------------------------------------------------------- */
function flyopen()
{
	$(this).addClass('open');
	$(this).children('.flyout').addClass('open');
}
function flyclose()
{
	$(this).removeClass('open');
	$(this).children('.flyout').removeClass('open');
}
function flyoutify()
{
	$('ul#geo > li, ul#filter > li').each(function ()
	{
		if($(this).children('ul').size() > 0)
		{
			$(this).addClass('flyout');
			$(this).children('a').eq(0).addClass('flyout');
			$(this).children('ul').eq(0).addClass('flyout');
		}
	});
	$('li.flyout').hoverIntent(flyopen, flyclose);
}

/* -------------------------------------------------------------- 
	Tabs
-------------------------------------------------------------- */

function tabify()
{
  var links = '';
	$('.tabititle').eq(0).before('<ul id="soltype" class="tabs">' + links + '<\/ul>');
	$('.tabititle').each(function (i) {
   	var liclass = '';
    var rev = $(this).attr('rev');
    var aclass = 'cat_'+rev+'_node';
    if(i == 0) aclass+=' current';
    var ref = '#solution_' + rev;
		var link = '<a href="'+ref+'" class="'+aclass+'">'+$(this).text()+'</a>';
		if(i != 0) $(ref).hide();
    links += '<li'+liclass+'>' + link + '<\/li>';
    $(this).remove();
    i++;
  }); // alert(links);
	$('#soltype').append(links);
  
	$('#soltype').find('li').each(function (i) { 	//alert($(this).next().size())
		if(!$(this).next().size()) $(this).addClass('last')
	});
	
	$('#soltype').find('a').click(function () {
		var oldid = $('#soltype a.current').attr('href');
    oldid = oldid.split('#')
    oldid = '#'+oldid[1]
		
		var newid = $(this).attr('href');
    newid = newid.split('#')
    newid = '#'+newid[1]

    $('#soltype a.current').removeClass('current');
		$(this).addClass('current');
    $(this).blur();
    $(oldid).fadeOut('fast', function(){
			$(newid).fadeIn('fast'); 
		}); // alert(oldid+' '+newid)
    
    return false;
  });
}


/* -------------------------------------------------------------- 
	Cycle
-------------------------------------------------------------- */

function cyclify()
{
	$('#cycle')
	.css('overflow', 'hidden')
	.after('<div id="cycle-pager">')
	.cycle({
	    fx:     'cover',
	    speed:   600,
	    timeout: 6000,
	    pause:   1,
	    pager:  '#cycle-pager'
	})
	.find('div').each(function (i) {
		$(this).mouseover(function () {
			$(this).addClass('hovering');
		});
		$(this).mouseout(function () {
			$(this).removeClass('hovering');
		});
		$(this).click(function () {
			var url = $(this).find('a').eq(0).attr('href');
	    location.href = url;
		});
	});
}

/* -------------------------------------------------------------- 
	Slideshow
-------------------------------------------------------------- */

function slidefy()
{
	$('.slide')
	.css('overflow', 'hidden')
	.each(function (i) {
		var slideid = $(this).attr('id');
		if($(this).find('a').size() > 1)
		{
			var extra = '<a class="slide-caption" rel="'+slideid+'"><\/a>' + '<div class="slide-pager" rel="'+slideid+'"><\/div>';
			if($(this).parent().attr('class') == 'spot') $(this).after(extra);
			else																				 $(this).parent().append(extra);
			$(this).cycle({
			    fx:     'fade',
			    speed:   500,
			    timeout: 5000,
			    pause:   1,
			    pager:  ".slide-pager[rel='"+slideid+"']",
			    after: function () {
				    $(".slide-caption[rel='"+slideid+"']").text(this.title);
				    $(".slide-caption[rel='"+slideid+"']").attr('href', this.href);
			    }
			})
		}
	})
}

/* -------------------------------------------------------------- 
	Picasa Gallery
-------------------------------------------------------------- */

function glryfy()
{
	$("a[rel^='prettyPhoto']").prettyPhoto();
	$("a[rel^='prettyPhoto']").each(function (i) { 	//alert($(this).next().size())
		var img_src = $(this).find('img').eq(0).attr('src');
		$(this).css('background', 'transparent url('+img_src+') no-repeat center center');
		$(this).find('img').eq(0).attr('src', '/wp-content/themes/surfcamp/jcss/zoom.png').addClass('zoom');
	});
	$(" h2.gallery a").click(function () {
			$("ul.gallery li.hidden").removeClass('hidden');
			$("ul.gallery li a").eq(0).click();
			return false;
  });
  $(".detail .slide a, .spot .slide a").each(function (i) {
			var img_src = $(this).find('img').eq(0).attr('src');
			$(this).css('background', 'transparent url('+img_src+') no-repeat center center');
			$(this).css('height','185px');
			$(this).css('width', '248px');
			$(this).css('display', 'block');
			$(this).find('img').eq(0).attr('src', '/wp-content/themes/surfcamp/jcss/zoom.png').addClass('zoom');
			$(this).click(function () {
				var pos = parseInt($(this).attr('rev'));
				$("ul.gallery li a").eq(pos).click();
				return false;
	  	});
	});

}

/* -------------------------------------------------------------- 
	You Tube
-------------------------------------------------------------- */

function videofy()
{
	$("h2.video a").click(function () {
		$("img.max").css('display','none');
		$("div.video").css('display','block');
		return false;
  });
	$("a.video-pre").click(function () {
		$(this).toggleClass('video-pre').toggleClass('video-caption');
		$("div.video").toggle();
		return false;
  });
}

/* -------------------------------------------------------------- 
	Misc
-------------------------------------------------------------- */

function swtify()
{
	$('#nav_switch').click(function() {
		if($(this).attr('class') == 'open')
		{
			$('#nav').slideUp();
			$('ul.breadcrumbs a[id!=nav_switch]').removeClass('dark');
			$(this).text($(this).attr('rel'));
		}
		else
		{
			$('#nav').slideDown();
			$('ul.breadcrumbs a[id!=nav_switch]').addClass('dark');
			$(this).attr('rel', $(this).text());
			$(this).text(hidemenu);
		}
		$(this).toggleClass('open');
		$(this).blur();
		return false;			
	});
}

/*  ---------------------------------------------------------- */

function logofy()
{
	$('a#logo').mouseover(function () {
		$('li.extra-home a').addClass('hovering');
  });
	$('a#logo').mouseout(function () {
		$('li.extra-home a').removeClass('hovering');
  });
}

/*  ---------------------------------------------------------- */

function formify()
{
	$('form.subscribe input.email').attr('rev', $('form.subscribe input.email').attr('value'));
	$('form.subscribe input.email').focus(function () {
		$(this).attr('value', 'e-mail');
		$(this).select();
		return true;
  });
	$('form.subscribe input.email').blur(function () {
		var test = $(this).attr('value');
		if(test == 'e-mail' || test.length == 0) $(this).attr('value', $(this).attr('rev'));
		return true;
  });
}

/*  ---------------------------------------------------------- */

function modalise()
{
	$('li.check a').each(function(i){
			var href = this.href;
	    if(href.indexOf('?') > 0) this.href+= '&ajax';
		  else this.href += '?ajax';
		}
	);

	$('#modal')
	.jqDrag('.jqmDrag')
	.jqm(
		{
			//focus: true,
			//overlay: 0,
			target: 'div.jqmContent',
			trigger: 'li.check a',
			ajax: '@href',
			ajaxText: '<img id="loader" src="http://book.surfcamp.it/jcss/ajax-loader.gif" />',
		  onShow: function(hash)
			{ /* definition of an "on open" function */
				var vh = Math.round($(window).height() * 0.78);
				$('.jqmContent').css('max-height', vh);
				hash.w.show();
			}
		}
	);

}

