var currentProductImageUrl;
var currentProductView;
var currentImageElement;

function setCurrentProductImageUrl( url ) {
	currentProductImageUrl = url;	
}

function swapProductImage( imageUrl, url ) {
	var imageElement = $('img#product_image');
	currentProductImageUrl = url;
	imageElement.fadeOut("fast", function() {
								imageElement.attr( 'src', '/site_cms/sites/all/themes/fabrik/images/loading.gif' );
								
								imageElement.fadeIn("fast", function() {
													imageElement.attr( 'src', imageUrl );
														});
								//currentImageElement = imageElement;
								//setTimeout( performFadeIn, 200, imageUrl );
							}
						);
	
	return false;
}

function performFadeIn( imageUrl) {
	currentImageElement.attr( 'height', 250 );
	currentImageElement.attr( 'src', imageUrl );
	currentImageElement.fadeIn("fast");
}

function gotoProductImageUrl() {
	document.location.href = currentProductImageUrl;
	return false;
}

function setViewImage( imageFilename ) {
	swapProductImage( '/site_cms/sites/all/themes/fabrik/images/products/special/normal/' + imageFilename, '' );
	$('a#glass_button').attr('href', '/site_cms/sites/all/themes/fabrik/images/products/special/full/' + imageFilename );
	$('img#full_image_view').attr('src', '/site_cms/sites/all/themes/fabrik/images/products/special/full/' + imageFilename );
	return false;
}
function setDynamicViewImage( imageFilename, imageFilenameLarge ) {
	swapProductImage( imageFilename, '' );
	$('a#glass_button').attr('href', imageFilenameLarge );
	$('img#full_image_view').attr('src', imageFilenameLarge );
	return false;
}

$(document).ready(function(){


// HOMEPAGE DHTML (EXPLORE OUR SUITE) 
		function hover_image_in(e){
		image_src = $(this).attr('src');
		var image_src_new = image_src.substring(0, image_src.lastIndexOf(".gif")) + "_o.gif";
		$(this).attr('src',image_src_new);
		}
		
		function hover_image_out(e){
		image_src = $(this).attr('src');
		var image_src_new = image_src.substring(0, image_src.lastIndexOf("_o.gif")) + ".gif";
		$(this).attr('src',image_src_new);
		}
		
		function hover_image_in2(e){
		image_src = $(this).attr('src');
		//var image_src_new = image_src.substring(0, image_src.lastIndexOf(".gif")) + "_o.gif";
		$(this).attr('src','/site_cms/sites/all/themes/fabrik/images/homepage/dhtml/' + $(this).attr('id') + '_on.jpg');
		}
		
		function hover_image_out2(e){
		image_src = $(this).attr('src');
		//var image_src_new = image_src.substring(0, image_src.lastIndexOf("_o.gif")) + ".gif";
		$(this).attr('src','/site_cms/sites/all/themes/fabrik/images/homepage/dhtml/' + $(this).attr('id') + '.jpg');
		}
		
		function show_content(e){
			content_div = $(this).find('img').attr('id');
			
				// Hide them all
				$('#minimenu_store').hide();
				$('#minimenu_default').hide();
				$('#minimenu_backup').hide();
				$('#minimenu_share').hide();
				
				// Show the one we want
				$('#minimenu_' + content_div).show();
		}
		
		
		function show_default(e){
			content_div = $(this).find('img').attr('id');
			
				// Hide them all
				$('#minimenu_store').hide();
				$('#minimenu_default').hide();
				$('#minimenu_backup').hide();
				$('#minimenu_share').hide();
				
				// Show the one we want
				$('#minimenu_default').show();
		}
		
		function show_store_content(e){
			content_div = $(this).find('img').attr('id');
			
				// Hide them all
				$('#store_content_sigmini').hide();
				$('#store_content_simpledrive').hide();
				$('#store_content_redrive').hide();
				$('#store_content_prodrive').hide();
				
				// Show the one we want
				$('#store_content_' + content_div).show();
		}
		
		function hide_store_content(e){
				$('#minimenu_store').hide();
				$('#minimenu_share').hide();
				$('#minimenu_backup').hide();
				$('#minimenu_default').show();
		}
		
		$('#store_head img').hover(hover_image_in2,hover_image_out2);
		
		$('#minimenu_navigation img').hover(hover_image_in,hover_image_out);

		$('#minimenu_navigation a').mouseover(show_content);
		
		$('#store_head a').hover(show_store_content,function(){});
		
		$('#minimenu').hover(function(){},function(){hide_store_content();});
		
		

	function colorHoverOn(e){
		colors_div_y = $('div.select-colors').find('span').offset().top;	// Keeps it so no matter where the colors div is placed on the page\
		
		// Moves the tooltip
		$(this).mousemove( function(cursor_move) {
			// Offsets the tooltips
			offset_y = -30;
			offset_x = cursor_move.pageX - 305;// - 0; //cursor_move.pageX - 0;
			
			$(this).find("span").css("position","absolute");
			$(this).find("span").css("top", offset_y + "px"); //$(this).parent().find("span").css("top",(cursor_move.pageY - offset_y) + "px");
			$(this).find("span").css("left", offset_x + "px");
			
			// Displays the tooltip
			$(this).find('span').show();
		});
	}
	
	function colorHoverOn2(e){
		colors_div_y = $('div.select-colors2').find("div.select_color_item").offset().top;	// Keeps it so no matter where the colors div is placed on the page\
		
		// Moves the tooltip
		$(this).mousemove( function(cursor_move) {
			// Offsets the tooltips
			offset_y = -25;
			offset_x = cursor_move.pageX - $(this).offset().left - ($(this).find("div.select_color_info").find('img').attr('width') / 2) + 2;
			
			$(this).find("div.select_color_info").css("position","absolute");
			$(this).find("div.select_color_info").css("top", offset_y + "px");
			$(this).find("div.select_color_info").css("left", offset_x + "px");
			
			// Displays the tooltip
			$(this).find('div.select_color_info').show();
		});
	}
	
	function colorHoverOff(e){
		// Hides the tooltip
		$(this).find(".select_color_info").hide();
	}
	
	$('div.details a').toggle(function(){display_details(this);},function(){hide_details(this);});
	
	$("div.select-colors a").hover(colorHoverOn,colorHoverOff);
	$("div.select-colors2 div.select_color_item").hover(colorHoverOn2,colorHoverOff);
	
	var currentTimer;
	var currentOtherPopup;
	
	function otherProductsHoverOn(e) {
		if( currentOtherPopup != undefined ) {
			clearTimeout( currentTimer );
			currentOtherPopup.hide();
		}
	
		// Offsets the tooltips
		var offset_y = $(this).offset().top - 215;
		var offset_x = $(this).offset().left + 10;
		
		$(this).find("div.product_teaser_container").css("position","absolute");
		$(this).find("div.product_teaser_container").css("top",(offset_y) + "px");
		$(this).find("div.product_teaser_container").css("left",(offset_x) + "px");
		
		// Displays the tooltip
		$(this).find('div.product_teaser_container').show();
		currentOtherPopup = $(this).find('div.product_teaser_container');
		
	}
	
	function otherProductsHoverOff(e) {
		
		var targetObj = $(this).find("div.product_teaser_container");
		//$(this).oneTime(2000, hideTeaserContainer( targetObj ) );
		currentTimer = setTimeout( hideTeaserContainer, 750 );
	}
	
	
	$("ul.other_products_ul li").mouseover(otherProductsHoverOn);
	$("ul.other_products_ul li").hover( otherProductsHoverOn, otherProductsHoverOff );
			
	function hideTeaserContainer( thisObj ) {
		clearTimeout( currentTimer );
		currentOtherPopup.hide();
		
	}
			
	/* ** PRODUCT OPTIONS CODE *** */
	
	
	var currentPopup;
	var popupShadows = [];
	var closeProductOptionTimer;
	var isMouseOnProductOption;
	var isProductOptionOpen = false;
	
	function closeProductOptionPopupOnTimer() {
		if( !isProductOptionOpen || isMouseOnProductOption ) {
			clearTimeout( closeProductOptionTimer );
			return;
		}
		
		if( closeProductOptionTimer != undefined ) {
			clearTimeout( closeProductOptionTimer );
		}
		
		closeProductOptionPopup();
	}
	
	function closeProductOptionPopup() {
		//popupShadows[currentPopup.find('div.product_options_popup').shadowId()].hide();
		currentPopup.find('div.product_options_popup').hide();
		currentPopup.css('z-index', 1);
		
		isProductOptionOpen = false;
	}
	
	function productOptionHoverOn() {
		if( currentPopup != undefined ) {
			closeProductOptionPopup();
			/*popupShadows[currentPopup.find('div.product_options_popup').shadowId()].hide();
			currentPopup.find('div.product_options_popup').hide();
			currentPopup.css('z-index', 1);*/
		}
		
		try {
			isMouseOnProductOption = true;
			
			clearTimeout( closeProductOptionTimer );
			
			$(this).css('z-index', 99);
			
			with( $(this).find('div.product_options_popup') ) {
				show();
				if( shadowId() == undefined ) {
					//var currentPopupShadow = dropShadow();
					//popupShadows[shadowId()] = currentPopupShadow;
					
				} else {
					//popupShadows[shadowId()].show();
				}
			}
			currentPopup = $(this);
			isProductOptionOpen = true;
		} catch( error ) {}
	}
	
	function productOptionHoverOff() {
		isMouseOnProductOption = false;
		if( isProductOptionOpen ) {
			closeProductOptionTimer = setTimeout( closeProductOptionPopupOnTimer, 500 );
		}
	}
	
	$('div.product_options div.product_options_item').hover( productOptionHoverOn, productOptionHoverOff );
	
	/* ** END PRODUCT OPTIONS CODE *** */
	
	/**** TABS ***/
	$('a#tab1a').click( function() {
		$('#tab-2-div').hide();
		$('#tab2img').attr('src', '/site_cms/sites/all/themes/fabrik/images/subheaders/product_detail.jpg' );
		$('#tab1img').attr('src', '/site_cms/sites/all/themes/fabrik/images/subheaders/features_selected.jpg' );
		$('#tab-1-div').show();
		
		return false;
	} );
	
	$('a#tab2a').click( function() {
		$('#tab-1-div').hide();
		$('#tab1img').attr('src', '/site_cms/sites/all/themes/fabrik/images/subheaders/features.jpg' );
		$('#tab2img').attr('src', '/site_cms/sites/all/themes/fabrik/images/subheaders/product_detail_selected.jpg' );
		$('#tab-2-div').show();
		
		return false;
	} );
	
	/*** END TABS ***/
	
	/******* SUPPORT SECTION *********/
	
	var ignoreSupportMenu = false;
	
	function supportMenuHoverOn() {
		var targetId = $(this).find('img').attr('id');
		var currentImage = $(this).find('img').attr('src');
		var selectedImage = '/site_cms/sites/all/themes/fabrik/images/support_menu/' + targetId + '_selected.gif';
		if( currentImage != selectedImage ) { 
			$('ul#support_links_list img#' + targetId).attr( 'src', selectedImage );
			ignoreSupportMenu = false;
		} else {
			ignoreSupportMenu = true;
		}
	}
	
	function supportMenuHoverOff() {
		var targetId = $(this).find('img').attr('id');
		var selectedImage = '/site_cms/sites/all/themes/fabrik/images/support_menu/' + targetId + '.gif';
		if( !ignoreSupportMenu ) { 
			$('ul#support_links_list img#' + targetId).attr( 'src', selectedImage );
		}
	}
	
	$('ul#support_links_list li a').hover( supportMenuHoverOn, supportMenuHoverOff );
	
	function handleFaqClickOn() {
		$('div.faq_subcontent#' + $(this).attr('id') ).show();
	}
	
	function handleFaqClickOff() {
		$('div.faq_subcontent#' + $(this).attr('id') ).hide();
	}
	
	$('div.faq_head').toggle( handleFaqClickOn, handleFaqClickOff );
	
	/****** END SUPPORT SECTION *********/
	
	/***** MEMORY MENU *****/
	var alreadySelected = false;
	function handleMemoryHoverOn() {
		if( $(this).attr('class') == 'selected' ) {
			alreadySelected = true;
		} else {
			alreadySelected = false;
		}
		
		$(this).attr('class', 'selected');
	}
	
	function handleMemoryHoverOff() {
		if( !alreadySelected ) {
			$(this).attr('class', '');
		}
	}
	
	$('div.categories a').hover( handleMemoryHoverOn, handleMemoryHoverOff );	
	/******** END MEMORY MENU **********/
} );

/***** SUPPORT TABS *******/
	var currentTabIdOpened;
	function swapTab( currentId ) {
		if( currentTabIdOpened != undefined ) {
			$('img#' + currentTabIdOpened).attr( 'src', '/site_cms/sites/all/themes/fabrik/images/support/tabs/' + currentTabIdOpened + '.gif' );
			$('div.support_tab_content#' + currentTabIdOpened).hide();
		}
		currentTabIdOpened = currentId;
		try {
			$('div.support_tab_content#defaultsupport').hide();
			$('img#' + currentId).attr( 'src', '/site_cms/sites/all/themes/fabrik/images/support/tabs/' + $('img#' + currentId).attr('id') + '_selected.gif' );
			$('div.support_tab_content#' + currentId).show();
		} catch( e ) {}
		
		return false;
	}
	
	
	
	
	/****** TABS **********/