var visibleMainMenu = false;
var keepMainMenu = false;



function addSectionsMenu(containerName) {
	return;
	if($("#"+containerName).size()>0) {
		flashembed(containerName, {
				id: "flash_"+containerName,
				src: "/media/img/common/menuSecciones.swf",
				w3c: false,
				wmode: "transparent",
				width: "950",
				height: "90",
				salign: "tl",
				base: "/media/img/common/",
				allowScriptAccess: "always",
				allowFullScreen: "true"
			}
		);
	}

}

function sendRecomendar() {
	$("#recomendarFormSubmit").click();
}

function sendConsultar() {
	$("#consultarFormSubmit").click();
}

function setUpRecomendar(containerName, lnkName) {
	var container = jQuery("#"+containerName);
	if (container.length > 0) {
		var center=function(hash){ hash.w.show(); $('#'+containerName).centerInClient();};	
		container.jqm({toTop:true,onShow:center});
		$("#"+lnkName).attr('href', '#');
		container.jqmAddTrigger($("#"+lnkName));
		container.jqmAddClose($("#"+containerName + 'Close'));
		$("#recomendarFormSubmit").hide();
		$("#recomendarSendButton").click(function() { sendRecomendar(); });	
	}
}

function setUpConsultar(containerName, lnkName) {
	var container = jQuery("#"+containerName);
	if (container.length > 0) {
		var center=function(hash){ hash.w.show(); $('#'+containerName).centerInClient();};
		container.jqm({toTop:true,onShow:center});
		$("#"+lnkName).attr('href', '#');
		container.jqmAddTrigger($("#"+lnkName));
		container.jqmAddClose($("#"+containerName + 'Close'));
		$("#consultarFormSubmit").hide();
		$("#consultarSendButton").click(function() { sendConsultar(); });
	}
}

function addMainMenu(src, base, xmlPath, backColor) {
	$(function() {
		makeRolloversWithOffset("#header_menu", "png");
		addSectionsMenu("siteButtons");
		$("#menuFlashContainer").hide();
		if($("#menuFlashContainer").size()>0) {
			flashembed("menuFlashContainer", {
					id: "flash_menu",
					src: src,
					w3c: false,
					wmode: "transparent",
					width: "950",
					height: "200",
					salign: "tl",
					base: base,
					allowScriptAccess: "always",
					allowFullScreen: "true"
				}
				, {
					xmlPath: xmlPath,
					backGroundColor: backColor
				}
			);
		}
		$("#flash_menu").mouseover(
				function() {
					if (!keepMainMenu) {
						keepMainMenu = true;
						setInterval('removeKeepMenu()',2000);
					}
				}
		);
		$("#flash_menu").mouseleave(
				function() {
					tryToHideMenu();
				}
		);
		$("#lnkCalle").mouseover(function() { ShowFamily(1);});
		$("#lnkOffroad").mouseover(function() {ShowFamily(2);});
		$("#lnkAtv").mouseover(function() {ShowFamily(3);});
		$("#lnkMarine").mouseover(function() {ShowFamily(1);});
		$("#lnkFuerza").mouseover(function() {ShowFamily(1);});
	});
	addExtendedInfoBar("#infobar");
	addToolboxButtons();
}

function ShowFamily(num)
{
	showMenu();
	if ($("#flash_menu").length>0) {
		$("#flash_menu")[0].SetVariable("_root.ShowFamily", num);
	}
}
function showMenu()
{
	visibleMainMenu = true;
	if (!keepMainMenu) {
		keepMainMenu = true;
		setInterval('removeKeepMenu()',2000);
	}
	$("#menuFlashContainer").css("z-index","100");
	$("#menuFlashContainer").show();
}

function removeKeepMenu()
{
	keepMainMenu = false;
}

function tryToHideMenu()
{
	if (visibleMainMenu && !keepMainMenu) {
		hideMenu();
	}
}

function hideMenu()
{
	$("#menuFlashContainer").css("z-index","-100");
	$("#menuFlashContainer").hide();
	visibleMainMenu = false;
}

function makeRotator(idRotator) {
	$('#'+idRotator+' li').css({opacity: 0.0});
	
	$('#'+idRotator+' li.firstRotator').css({opacity: 1.0});
	
	setInterval('rotateImagesRotator("'+idRotator+'")',6000);
}

function rotateImagesRotator(idRotator) {
	var current = ($('#'+idRotator+' li.showRotator')?  $('#'+idRotator+' li.showRotator') : $('#'+idRotator+' li.firstRotator'));

	var next = ((current.next().length) ? ((current.next().hasClass('showRotator')) ? $('#'+idRotator+' li.firstRotator') :current.next()) : $('#'+idRotator+' li.firstRotator'));	

	next.css({opacity: 0.0})
	.addClass('showRotator')
	.animate({opacity: 1.0}, 1000);

	current.animate({opacity: 0.0}, 1000)
	.removeClass('showRotator');
	
};

var keepInfo = false;
var lastPanel = null;

function addToolboxButtons() {
	$(".previewPanelInfo").each(function() { $(this).css("visibility", "visible"); });
	$(".previewPanelInfo").each(function() {$(this).hide();} );
	var lastButtonInfo = null;
	$(".previewPanelInfo").each(function() {$(this).mouseover(function(){
			keepInfo = true;
		});
	});
	$(".previewPanelInfo").each(function() {$(this).mouseleave(function(){
			keepInfo = false;
			hideFinanceInfo(true);
		});
	});
	$("#buttonFooter li").each(function(){
		$(this).addClass("toolboxButtonUnSelected");
		$(this).mouseover(function(event) {
			keepInfo = true;
		});
		$(this).mouseenter(function(event) {
			lastButtonInfo = $(this);
			$("#buttonFooter li").each(function(){
				$(this).removeClass("toolboxButtonSelected");
				$(this).addClass("toolboxButtonUnSelected");
			});
			$(this).removeClass("toolboxButtonUnSelected");
			$(this).addClass("toolboxButtonSelected");
			$(".previewPanelInfo").each(function() {$(this).hide();});
			$(".previewPanelInfo").html($("#toolboxInfo"+$(this).attr("id")).html());
			
			pos = $("#buttonFooter").height() + $("#buttonFooter").position().top;
			$("#miniPanelInfoGral").css('top', pos+"px");
			
			$("#miniPanelInfoGral").slideDown("fast");
			if (!keepInfo) {
				setTimeout('hideFinanceInfo(false)', 200);
			}
			keepInfo = true;
			$('.linkPlanAhorro2').popupWindow({ windowURL:'/sp/planDeAhorro/', windowName:'micrositePlan', height:650, width:1000 });
			$('.linkMantenimientoFuerza').popupWindow({ windowURL:'http://www.css-club.net/honda/pp-service/info/index.asp?Ref=har', windowName:'Mantenimiento', scrollbars:1, height:600, width:800 });
		});
		$(this).mouseleave(function(event) {
			hideFinanceInfo(true);
		});
	});
}

function hideFinanceInfo(reset) {
	if (!keepInfo) {
		$(".previewPanelInfo").each(function() {$(this).slideUp("fast");});
		$("#buttonFooter li").each(function(){
			$(this).removeClass("toolboxButtonSelected");
			$(this).addClass("toolboxButtonUnSelected");
		});
	}
	else {
		setTimeout('hideFinanceInfo(false)',200);
	}
	if (reset) {
		keepInfo = false;
	}
}

function addExtendedInfoBar(infobarSelector) {
	ieoffset = 0;
	pos = $(infobarSelector).parent().height() - $(infobarSelector).offset().top + ieoffset;
	$("#infobarExtended").css('bottom', pos+"px");
	$("#btnNews").click(
		function() {
            if ($("#infobarExtended").is(":hidden"))
            {
                 $("#infobarExtended").slideDown("fast");
            } else {
                 $("#infobarExtended").slideUp("fast");
            }
		}
	);
}

function makeRollovers(container, gfxExt) {
	
	$(container).find("span").filter('.rolloverButtonImage').each(function() {
		$(this).css("background-image", "url(" + $(this).attr('id') + ")");
	});
	
	// Navigation rollovers
	$(container).find("span").filter('.rolloverButtonImage').mouseover(function(){
		imgsrc = $(this).css("background-image");
		matches = imgsrc.match(/_over/);
		// don't do the rollover if state is already ON
		if (!matches) {
			imgsrcON = imgsrc.replace(new RegExp("."+gfxExt, "gi"),"_over."+gfxExt); // strip off extension
			$(this).css("background-image", imgsrcON);
		}
		
	});
	$(container).find("span").filter('.rolloverButtonImage').mouseout(function(){
		$(this).css("background-image", imgsrc);
	});

	$(container).find("span").filter('.rolloverButtonText').each(function(){
		$(this).css("display", "none");
	});
}

function makeRolloversWithOffset(container, gfxExt) {
	
	$(container).find("span").filter('.rolloverButtonImage').each(function() {
		$(this).css("background-image", "url(" + $(this).attr('id') + ")");
	});

	// Navigation rollovers
	$(container).find("span").filter('.rolloverButtonImage').mouseover(function(){
		buttonHeight = $(this).css("height");
		//alert("0px -"+buttonHeight);
		$(this).css("background-position", "0px -"+buttonHeight);
	});
	$(container).find("span").filter('.rolloverButtonImage').mouseout(function(){
		$(this).css("background-position", "0px 0px");
	});
	
	$(container).find("span").filter('.rolloverButtonText').each(function(){
		$(this).css("display", "none");
	});
}

function setUpLogin(containerName) {
	if($("#popupLogin").length > 0) {
		var center=function(hash){ hash.w.show(); $('#'+containerName).centerInClient();};
		var container = $("#"+containerName);
		container.jqm({toTop:true,onShow:center});
		$(".mustLogin").each(function(){
			$($(this)).click(function() {
				$("#returnurl").attr('value', $(this).attr('href'));
			});
		});
		$(".mustLogin").each(function(){
			container.jqmAddTrigger($(this));
		});
		container.jqmAddClose($("#"+containerName + 'Close'));

		$('#'+containerName+' form').each(function() {
			$(this).ajaxForm(
					{
						dataType: 'json',
						beforeSubmit: loginRequest,
						success: loginDone
					}	
			);
		});
		
		$("#SendButton").click(function() { 
			$("#popupLogin form").each( function(){ $(this).submit();} );
		 });
		$("#submit").hide();
	}
}

function loginRequest(formData, jqForm, options) { 
    var queryString = $.param(formData);
    return true; 
}

function loginDone(data, responseCode)  {
	if (data.result == 'alreadyLogin') {
		$("#popupLogin").hide();
	}
	if (data.result == 'fail') {
		$("#popupLoginError").hide();
		$("#popupLoginError").html('Email o contraseña incorrectos');
		$("#popupLoginError").fadeIn('slow');
	}
	if (data.result != 'fail' && data.urlDestination != '') {
		window.location.replace(data.urlDestination);
	}
}

function startup() {
	makeRotator("rotator");
}

	function play_360() {
    jQuery('#image_360').trigger('play');
  }

  function stop_360() {
    jQuery('#image_360').trigger('stop');
  }
  
  function show_360() {
    jQuery('#viewer_360_link').trigger('click');
  }
  
  function create_360(link) {
    link.fancybox(
  	  {
  	    'padding': 5,
  	    'overlayColor': '#111',
  	    'overlayOpacity': 0.9,
  	    'type': 'inline',
  	    'scrolling'		: 'no',
  	    'titleShow': true,
  	    'titlePosition' : 'inside',
  	    'titleFormat' : function () {
            return jQuery('#control_panel_360').html();
          },
  	    'onStart': function() {
  	      jQuery('#image_360').show();
  	    },
  	    'onClosed': function() {
  	      stop_360();
      	  jQuery('#image_360').hide();
      	}
      }
  	);
  }

  function setup_barousel(target) {
    jQuery(target).barousel({
        navWrapper: '#thslide_barousel_nav .thslide_list',
        manualCarousel: 0,
        navType: 3,
				contentResize: 0
    });

		var items_container = jQuery('.thslide_list');
		var items = items_container.find('ul li');
		var cant_items = items.length;
		if (cant_items > 0) {
			var width_container = items_container.width();
			var width_items = 0;
			var width_item = jQuery(items[0]).width();
			items.each(function(index) {
				width_items += jQuery(this).width();
			});
			items_list = jQuery('.thslide_list ul');
			if (width_items > width_container) {
		    jQuery('#thslide_barousel_nav').thslide({
	        itemOffset: width_item,
	        infiniteScroll: 1,
	        scrollOver: 1,
	        slideSpeedFast: 100
		    });
			}
			else {
				var margin = 20;
				jQuery('.thslide_nav_previous').hide();
				jQuery('.thslide_nav_next').hide();
				items_list.width(width_items + margin);
				items_list.css('margin-left', (width_container - width_items - margin) / 2);
			}
			items_list.find('a').each(function() {
				jQuery(this).bind('click', function() {
					jQuery('.barousel_content > div').each( function() {
							var elem = jQuery(this);
							if (elem.css('display') == 'block') {
								link = elem.find('a');
								window.location = link.attr("href");
							}
						}
					);
				});
			});
		}

  }


jQuery(document).ready(function () {
	jQuery('a[href][rel=external]').attr('target', '_blank');
	setUpRecomendar('popupRecomendar', 'lnkRecomendar');
	setUpConsultar('popupConsultar', 'lnkConsultar');
	setUpLogin('popupLogin');
	jQuery('.circle').mosaic({
		opacity : 0.8
	});
	jQuery('input[title!=""]').hint();
});
