/**
 * Piero jQuery functions
 * @version 090616.1611
 * @author Richard Najdowski
 *
 */

//-- onload
jQuery(document).ready(function()
{
	themePath = 'sites/default/themes/piero';
	
	/******************************************************* REPOSITIONING */
	//products contact us block
	$sports_block = $("#rbm_products_sports");
	$contact_block = $("#index-products #block-block-5");
	
	$contact_block.css("float", "left");
	$contact_block.css("position", "absolute");
	$contact_block.css("margin-top", $sports_block.height());
	$contact_block.css("width", "400px");
	
	/******************************************************* carousel */
	$(function() {
		for ( $i=0; $i<6; $i++ ) {
			if ($(".rbm_carousel-"+$i).length > 0) {
				$(".rbm_carousel-"+$i).jCarouselLite({
					btnNext: ".rbm_carousel_right-"+$i,
					btnPrev: ".rbm_carousel_left-"+$i,
					visible: 4,
					scroll: 1,
					circular: false
				});
			}
			if ($(".rbm_carousel-b-"+$i).length > 0) {
				$(".rbm_carousel-b-"+$i).jCarouselLite({
					btnNext: ".rbm_carousel_b_right-"+$i,
					btnPrev: ".rbm_carousel_b_left-"+$i,
					visible: 4,
					scroll: 1,
					circular: false
				});
			}
		}
	});
	
	/******************************************************* carousel gallery */
	$(".rbm_interface_tab_gallery_video").hide();
	//iterate photo/video switches
	for ($i=0; $i<6; $i++)
	{
		$("#interface_switch_photo-"+$i).click(function()
		{
			reset_interfaces($(this));
			$temp = $(this).attr('id').split("-");
			$id = $temp[1];
			$(".rbm_interface_tab_gallery_photo-"+$id).show();
			$(".rbm_interface_tab_gallery_video-"+$id).hide();
			return false;
		});
		$("#interface_switch_video-"+$i).click(function()
		{
			reset_interfaces($(this));
			$temp = $(this).attr('id').split("-");
			$id = $temp[1];
			$(".rbm_interface_tab_gallery_photo-"+$id).hide();
			$(".rbm_interface_tab_gallery_video-"+$id).show();
			return false;
		});
	}
	
	
	function reset_interfaces($selected)
	{
		for ($i=0; $i<6; $i++)
		{
			$("#interface_switch_photo-"+$i).removeClass("rbm_interface_media_selected");
			$("#interface_switch_video-"+$i).removeClass("rbm_interface_media_selected");
		}
		
		$selected.addClass("rbm_interface_media_selected");
	}
	
	
	/******************************************************* TABS */
	//interface tabulation
	$tabContainer = $("div.rbm_tab"); //content
	$tabList = $("#rbm_tabs_list li"); //links
	
	//check for currently selected
	$selected_tab = $(this).find("#rbm_tabs_list .rbm_tabs_selected");
	if ($selected_tab.length > 0) {
		//hide all but selected
		$tabContainer.hide().filter("#" + $selected_tab.find("a").attr("href").substring(1, 500)).show();
	} else {
		//default show first element
		$tabContainer.hide().filter(":first").show();
		$tabList.filter(":first").addClass("rbm_interface_media_selected");
	}
	//get thumb container
	$("#rbm_tabs_list li").each(function(n)
	{
		$(this).click(function()
		{
			//clear selected & show current
			$tabList.removeClass("rbm_tabs_selected");
			//select current
			$(this).addClass("rbm_tabs_selected");
			$tabContainer.hide().filter("#" + $(this).find("a").attr("href").substring(1, 500)).show();
			//stop link
			return false;
		});
	});
	
	
	/******************************************************* CONTACT FORM ERROR MSG */
	$contact_error = $("#index-contact .messages");
	if ( $contact_error.length > 0 ) {
		//hide error message
		$contact_error.hide();
		//relocate
		$(".webform-client-form").prepend("<div id=\"rbm_form_error\">" + $contact_error.html() + "</div>");
	}
	
	
	/******************************************************* NAV HIGHLIGHTING */
	//$temp_url = location.href
	$url = window.location.pathname;
	//get nav
	$nav = $("#rbm_nav_main");
	//check exists
	if ( $nav.length>0 ) {
		
		//news story
		if ( $url.match(/\/news\//) ) {
			
			//check all sub items
			$nav.find(".menu li").each(function(n)
			{
				$link = $(this).find("a");
				if ( $link.attr("href") == '/piero/news' ) {
					//highlight
					$(this).removeClass("collapsed").find("a").css("color","#cc0000");
					//add archive link
					$link.after('<ul class="menu"><li class="leaf last" style="color:#888 !important;"><a href="/piero/news-archive" title="">NEWS ARCHIVE</a>');
				}
			});
		//sport
		} else if ( $url.match(/\/sports\//) || $url.match(/interfaces/) ) {
			//check all sub items
			$nav.find(".menu li").each(function(n)
			{
				$link = $(this).find("a");
				if ( $link.attr("href") == '/piero/products' ) {
					//highlight
					$(this).removeClass("collapsed").find("a").css("color","#cc0000");
				}
			});
		}
	}
	
	
	
	/******************************************************* ajax form */
	Drupal.Ajax.plugins.piero_newsletters = function(hook, args)
	{
		$newsletter_form = $('#newsletter-signup');
		$newsletter_block = $("#rbm_block_newsletter .content");
		//listen for ajax messages
		if (hook === 'message') {
			//failing to set data values, so using actions instead
			if ( args.options.action=='notify' ) {
				//error; show input, hide thank you
				$newsletter_form.show();
			} else if ( args.options.action==='clear' ) {
				//valid, hide form, show thank you
				$newsletter_form.hide();
				$newsletter_block.append("<div id=\"rbm_newsletter_confirm\">Email added successfully.</div>");
			}
		}
	}
});
//-- /onload




/******************************************************* SIFR */
if(typeof sIFR == "function"){
	//....OVERRIDES IN piero_home_SPORTNAME.js
	//sIFR.replaceElement(named({sSelector:"#node-12 h1", sFlashSrc:"/piero/sites/default/themes/piero/js/sifr/defusedlight.swf", sColor:"#86C002", sLinkColor:"#86C002", sBgColor:"#FFFFFF", sHoverColor:"#86C002", nPaddingTop:20, nPaddingBottom:20, sWmode:"transparent", sFlashVars:"textalign=left&offsetTop=0"}));
	//sIFR.replaceElement(named({sSelector:"#node-14 h1", sFlashSrc:"/piero/sites/default/themes/piero/js/sifr/defusedlight.swf", sColor:"#86C002", sLinkColor:"#86C002", sBgColor:"#FFFFFF", sHoverColor:"#86C002", nPaddingTop:20, nPaddingBottom:20, sWmode:"transparent", sFlashVars:"textalign=left&offsetTop=0"}));
};


/******************************************************* shadowbox */
Shadowbox.init({ 
	language: 'en', 
	players:  ['img', 'html', 'iframe'] 
});

window.onload = function(){
	//get url
	var url = (window.location.hash).substring(1, 100);
	//check param exists
	if ( url != undefined && url.length > 5 ) {
		//check its a video request
		if ( url.substring(0, 5) == "video" ) {
			//get id
			var video_id = url.substring(5, 10);
			if ( video_id.length > 0 ) {
				//auto open shadowbox
				Shadowbox.open({
					player:		'iframe',
					content:	'http://piero2.redbeemediadev.com/piero/videoplayer/default/'+video_id+'/play.html',
					width:		500,
					height:		400
				});
			}
		}
	}
};