var i = 0;

function init() {
	initLogo();
	initCampaignModule();
	externalLinks();
	initWorkCarousel();
	initWorkLinks();
	initEngineFooterLink();
	initVideos();
}

//opens links in new window with rel="external" to validate xhtml strict
function externalLinks() { 
	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a"); 
 	for (var i=0; i<anchors.length; i++) { 
   			var anchor = anchors[i]; 
 			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external"){
 				anchor.target = "_blank"; 
       		} else if(anchor.getAttribute("href") && anchor.getAttribute("rel") == "thiswindow")anchor.target = "_top";
 	} 
}

//opens links in new window with rel="external" to validate xhtml strict
function initLogo() {
	var logo = $('#logo h1 a');
	var cycle = false;
	$.each(window.logos, function(index, value) {
		$('#logo h1 a').append('<img src="'+value+'" alt="WCRS&amp;Co" title="WCRS&amp;Co" />');
		cycle = true;
	});
	if (cycle) {
		logo.cycle({
			fx: 'fade',
			timeout: 0, 
		    cleartype: true,
			cleartypeNoBg: true
		});
	}
	logo.hover(
    	function() {
    		$(this).cycle('next');
    	},
    	function() {
    		// do nothing on mouseout
    	}
	);
}

//opens links in new window with rel="external" to validate xhtml strict
function initCampaignModule() {
	$('#campaign.module img').css({
		'position' : 'absolute',
		'z-index' : '-1'
	});
	if ($('.module#campaign div').length > 1) {
		$('.module#campaign').cycle({
	    	fx: 'fade',
	    	timeout: 5000,
		    cleartype: true,
			cleartypeNoBg: true,
			onAfter: function(curr, next, opts, fwd) {
				var $ht = $(this).height();
				$(this).parent().animate({height: $ht});
			}
		});
	}
}

function initWorkCarousel() {
	if ($('#carousel .workpiece').length > 1) {
		$('#carousel').cycle({ 
		    fx:      'fade', 
		    speed:   0, 
		    timeout: 0, 
		    next:    '#next', 
		    prev:    '#prev',
		    cleartype: true,
		    cleartypeNoBg: true,
		    after: onAfter
		});
	}
}

function onAfter() {
	if($('a[rel=youtube]').parent().css('display') == "block") {
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
			$('a[rel=youtube]').parent().css('height', '459px');
			$('a[rel=youtube]').html("");
			initYouTube();
		}
	}
}

function initVideos() {
	// html5 video
	$('video').mediaelementplayer({
	    defaultVideoWidth: 720,
	    defaultVideoHeight: 404,
	    // initial volume when the player starts
	    startVolume: 1,
	    // enables Flash and Silverlight to resize to content size
	    enableAutosize: true,
	    // the order of controls you want on the control bar (and other plugins below)
	    features: ['playpause','current','progress','duration','volume','fullscreen'],
		success: function(player, node) {
			$('#' + node.id + '-mode').html('mode: ' + player.pluginType);
		}
	});
	// check for any YouTube Links (we can still use these for certain vids such as TFL viral)
	$('a[rel=youtube]').youtubin({
        swfWidth : 720,
        swfHeight : 404
    });
}

function initWorkLinks() {
	$('.example h3 a').hover(
		function() {
			$(this).parent().parent().find('a.img span.overlay').addClass('off');
		},
		function() {
			$(this).parent().parent().find('a.img span.overlay').removeClass('off');
    	}
   	);
	$('.example a.img').hover(
		function() {
			$(this).parent().find('h3 a').addClass('active');
		},
		function() {
			$(this).parent().find('h3 a').removeClass('active');
	    }
	);
}

function initEngineFooterLink() {
	$('.engine-logo').hover(
		function() {
			$('#enginelink a').first().addClass('active');
		},
		function() {
			$('#enginelink a').first().removeClass('active');
		}
	);
}

$(document).ready(function() {
	init();
});
