$(function() {
    /*
        1.) Form Field Value Swap
    */

    swapValues = [];
    $(".swap_value").each(function(i){
        swapValues[i] = $(this).val();
        $(this).focus(function(){
            if ($(this).val() == swapValues[i]) {
                $(this).val("");
            }
        }).blur(function(){
            if ($.trim($(this).val()) == "") {
                $(this).val(swapValues[i]);
            }
        });
    });

	if ($('.tabbed_content').length > 1) {
		handleTabs();
	}

	if ($('.expandable_wrapper').length > 1) {
		handleExpandable();
	}
	
	if ($('.fancyzoom').length) {
		handleImageZoom();
	}
	
	if ($('#accordion_content').length) {
		handleStandardShowHide();
	}
	
	if ($('.rotating_ad').length) {
		handleRotatingAds();
	}


});

function tabTo(i) {
	if (!$('#' + i).is(":visible")) {
		$(".tabbed_content").hide();
		$('#' + i).show();
		$('#tabs ul li').removeClass('active');
		$('#tabs ul li#' + i + '_link').addClass('active');
	}
}

function handleTabs() {
	tabcount = 0;
	tabs_html = "";
	$(".tabbed_content_inner").each(function (i) {
		$(this).parent('.tabbed_content').attr("id", "tab_" + tabcount);
		
		if ($(this).children('.tab_tooltip').children('h3').length) {
			tabs_html = tabs_html + '<li id="tab_' + tabcount + '_link"><a href="javascript:tabTo(\'tab_' + tabcount + '\');" title="' + $(this).children('.tab_tooltip').children('h3').html()+ '">' + $(this).children('.tab_title').children('h2').html() + '</a></li>';
		} else {
			tabs_html = tabs_html + '<li id="tab_' + tabcount + '_link"><a href="javascript:tabTo(\'tab_' + tabcount + '\');">' + $(this).children('.tab_title').children('h2').html() + '</a></li>';
		}
		tabcount++;
	});
	
	tabs_html = '<div id="tabs"><ul>' + tabs_html + '</ul></div>';
	
	$('.tabbed_content:first').before(tabs_html);
	$('.tabbed_content:last').after('<div class="SF_clear">&nbsp;</div>')
	$('.tabbed_content .tab_title, .tabbed_content .tab_tooltip').hide();
	
	$('.tabbed_content').hide();
	$('.tabbed_content:first').show();
	$('#tabs ul li:first').addClass('active');
}

function handleExpandable() {
	
	$('.expandable_wrapper').accordion(
		{
			header: 'h2.expandable_header',
			alwaysOpen: false,
			active: false,
			autoheight: false
		}
	);
	
//	expandable_count = 0;
//	$('.expandable_wrapper .expandable').hide();
//	$(".expandable_wrapper").each(function (i) {
//		$(this).children('.expandable').attr('id', 'expandable_' + i);
//		$(this).children('.expandable_header').children('h2').wrapInner('<a href="javascript:expandableToggle(\'' + i +'\');"></a>');
//	});
}

function expandableToggle(i) {
//	if ($('#expandable_' + i).hasClass('expanded')) {
//		$('#expandable_' + i).slideUp("slow").removeClass('expanded');
//	} else {
//		$('#expandable_' + i).slideDown("slow").addClass('expanded');
//	}
	
	$('.expandable_wrapper').accordion(
		{
			header: '.expandable_header h2',
			alwaysOpen: false,
			active: false,
			autoheight: false
		}
	);
}

function handleImageZoom() {
	//Set the default directory to find the images needed
	//by the plugin (closebtn.png, blank.gif, loading images ....)
	$.fn.fancyzoom.defaultsOptions.imgDir='http://www.engineersireland.com/media/engineersireland/fancyzoom/'; //very important must finish with a /
	
	// Select all links with tozoom class, set the open animation time to 1000
	$('a.tozoom').fancyzoom({Speed:1000});

	// Select all links set the overlay opacity to 80%
	$('a').fancyzoom({overlay:0.8});
	
	//new rev > 1.2
	//apply fancyzoom effect on all image whose class is fancyzoom !!
	$('img.fancyzoom').fancyzoom();
}

function handleStandardShowHide() {
	jQuery('#disable').click(function() {
		jQuery('#accordion_content').accordion("destroy");
	});


	if($("#accordion_content").length > 0) {	
		$('#accordion_content').accordion(
			{
				header: 'a.head',
				alwaysOpen: false,
				active: false,
				autoheight: false
			}
		);
	}
}


function handleRotatingAds(){
	var total_ads = $('.rotating_ad').length;
	if (total_ads > 0) {
		var random_ad_num = Math.floor(Math.random()*total_ads);
		$('.rotating_ad:eq(' + random_ad_num + ')').show();
	}
}
