var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;
var homeSlideShow;
var homwSlideLock = false;

$(document).ready(function() {
  $('#menu-tabs > li').bind('mouseover', menu.menu_open);
  $('#menu-tabs > li').bind('mouseout',  menu.menu_timer);
	$('#brand-more-container').bind('mouseover', menu.menu_open);
	$('#brand-more-container').bind('mouseout', menu.menu_timer);
	$(document).click(menu.menu_close);
	
	/*
	$(document).find('#slideshow').cycle({ 
    fx:    'fade', 
    speed:  2500 
  });
	*/
	
	if ( $("#bannerRotate").length > 0 ) {
  	$('#bannerRotate').cycle({ 
      fx:    'cover',
  		direction: 'down',
      speed:  500 ,
  		timeout: 6000
    });
	}
	
	homeSlideShow = $("#home-intro-rotate");
	if ( homeSlideShow.length > 0 ) {
		slideShow.init();
	}
 
  $(document).find('.whats-new-slot:odd').css("background","#485340");
	
	$("#twitter_div a").attr("target","_blank");
	
	if ( $("#cbuild-infobox-wrapper").length > 0 ) {
	  cbuild.init();
	}
	
	if ( $("table#community-table").length > 0 ) {
	  community.init();
	}

	if ( $("table.custom-budget").length > 0 ) {
		budgetEst.init();
	}
	
	if ( $("ul#touring-tabs").length > 0 ) {
		touringcontent.init();
	}
	
	$("#weather-div a").attr("target","_blank");
	
	$("table.#labor-table tr:odd").css("background","#424242");
	
	$("a.fbox").fancybox(
		fbSettings
	);
	
	$("a.fvidbox").click(function() {
			$.fancybox({
  			'padding'		: 0,
  			'autoScale'		: false,
  			'transitionIn'	: 'none',
  			'transitionOut'	: 'none',
  			'title'			: this.title,
				'overlayColor'		: '#000',
				'overlayOpacity'	: 0.85,
  			'width'		: 780,
  			'height'		: 426,
  			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
  			'type'			: 'swf',
  			'swf'			: {
  			   	 'wmode'		: 'transparent',
  					 'allowfullscreen'	: 'true'
  				}
			});

			return false;
	});
	
	newscontent.init();
	
});

// fancybox settings
var fbSettings = {
			'titlePosition'		: 'over',
			'overlayColor'		: '#000',
			'overlayOpacity'	: 0.65,
			'centerOnScroll': true
		};
		
var cbuild = {
  init: function() {
	    var firstTab = $("#cbuild-infobox-tabs ul li:first").addClass("active");
      var brand = firstTab.attr("id").replace("cbuild-","");
      $("#"+brand+"-info").show();
			$("#cbuild-infobox-body").height($("#"+brand+"-info").height()+30);
    $("#cbuild-infobox-tabs ul li a").click(function(e) {
        e.preventDefault();
          $("#cbuild-infobox-tabs ul li").removeClass("active");
          $(this).parent().addClass("active");
          var brand = $(this).parent().attr("id").replace("cbuild-","");
          cbuild.showCbuilInfo(brand);
      });
    },
  showCbuilInfo: function(brand) {
      $("#cbuild-infobox-body div").fadeOut(700);
      $("#"+brand+"-info").fadeIn(700);
			$("#cbuild-infobox-body").animate({
		    height: $("#"+brand+"-info").height()+30
		  },700);
    }
};

var slideShow = {
  init: function() {
		homeSlideShow.cycle({ 
      fx:    'scrollLeft',
  		direction: 'down',
      speed:  500 ,
  		timeout: 7000,
  		pager: '#slide-nav'
    });
		
		$("#home-intro-rotate, map").hover(function(){
			slideShow.pause();
		},function() {
			if ( !homwSlideLock ) {
				slideShow.resume();
			}
  	});
		
		$("map").click(function(e) {
			e.preventDefault();
			homwSlideLock = true;
			slideShow.pause();
			slideShow.displayInfo($(this));
		});
		
		$("#slide-info-box, #slide-lightbox, a.close").click(function(e) {
			e.preventDefault();
			$("#slide-info-box, #slide-lightbox").fadeOut();
			slideShow.resume();
			homwSlideLock = false;
		});
		
	},
	pause: function() {
		homeSlideShow.cycle("pause");
	},
	resume: function() {
		homeSlideShow.cycle("resume");
	},
	displayInfo: function(o) {
		var mapName = o.attr("name");
		$("#slide-info-box p").text($("#"+mapName+"-info").html());
		$("#slide-info-box, #slide-lightbox").fadeIn();
	}
}

var curImg;
var community = {
  init: function() {
    $(".gallery-pics img, .manuf-feature-pix img").live("click",function() {
		  $("#lightbox").css({"width":$(document).width(),"height":$(document).height()}).show();
  		curImg = $(this);
			community.displayImg();
  		$("#img-window").css({"top":$(document).scrollTop()+60,"left":$(document).width()/2-200}).show();
  	}, function() {
  	  $("div.larger-preview").remove();
  	});
  	
  	$("#img-win-nav-bar span").click(function(e) {
  	  e.preventDefault();
  	  switch ($(this).attr("id")) {
  		  case "prev":
  			  if (curImg.prev("img").length == 0) {
  				  curImg = curImg.parent().find("img:last");
  				} else {
  				  curImg = curImg.prev("img");
  				}
  				community.displayImg();
  				break;
  			case "close":
  			  $("#lightbox").hide();
  				$("#img-window").hide();
  				$("#img-window img").attr("src","");
  				break;
  			case "next":
  				if (curImg.next("img").length == 0) {
  				  curImg = curImg.parent().find("img:first");
  				} else {
  				  curImg = curImg.next("img");
  				}
  				community.displayImg();
  				break;		
    	}
    });
  },
  displayImg: function() {
  	var imgCap = curImg.attr("alt");
  	var curImgUrl = curImg.attr("src").replace("-f.","-z.");
  	$("#img-window img").attr("src",curImgUrl);
		$("#img-window #img-caption").html(imgCap);
  }
};

var newscontent = {
	init: function() {
		$("ul.home-news-header a").click(function(e) {
			e.preventDefault();
			if ( !$(this).closest("li").hasClass("active") ) {
			 $("ul.home-news-header li").removeClass("active");
			 $(this).parent("li").addClass("active");
			 newContent = $(this).parent("li").attr("id") + "-news";
			 $(".news-content").hide();
			 $("#"+newContent).fadeIn(700);
			}
		});
	}
}

var touringcontent = {
	init: function() {
		var tabId = document.location.hash;
		tabId = tabId.replace("-h","");
		if ($(tabId).length > 0) {
			touringcontent.updateTab($(tabId));
		  var tabContent = $(tabId + "-content");
		} else {
			var tabContent = $(".t-content:first");
		}
  	tabContent.show();
		$("ul#touring-tabs li a").click(function(e) {
			e.preventDefault();
			touringcontent.updateTab($(this));
			touringcontent.showContent($(this));
		});
	},
	updateTab: function(o) {
		$("ul#touring-tabs li a").removeClass("active");
		document.location.hash = "#"+o.attr("id") + "-h";
		o.addClass("active");
	},
	showContent: function(o) {
		$(".t-content").hide();
		var newId = o.attr("id") + "-content";
		$("#"+newId).fadeIn(700);
	}
}

var budgetEst = {
  init: function() {
		$("form.budget-form").submit(function(e) {
		  e.preventDefault();
	    budgetEst.calculate();
		});
		$("input#b-total").focus(function() {
		  budgetEst.reset();
		});
	},
  calculate: function() {
		var bTotal = $("input#b-total").val();
		if (isNaN(bTotal)) {
		  alert("Please enter a valid total.");
		} else {
			$("table.custom-budget tr").each(function(index) {
			  if ( index > 0 && index < ($("table.custom-budget tr").length-1) ) {
				  var rowPerc = parseFloat($(this).find("td:eq(2)").text());
					$(this).find("td:eq(1)").text( (bTotal * rowPerc / 100).toFixed(2) );
				}
		  });
		}
	},
	reset: function() {
	  $("table.custom-budget tr").each(function(index) {
		    var lastRow = $("table.custom-budget tr").length-1;
			  if ( index > 0 && index < lastRow ) {
					$(this).find("td:eq(1)").text("0.00");
				} else if ( index == lastRow ) {
				  $("input#b-total").val("");
				}
		});
	}
};

$(window).load(function() {
  //loadWeather();
});

var menu = {
  menu_open: function() {
		menu.menu_canceltimer();
    menu.menu_close();
    ddmenuitem = $(this).find('ul').width($(this).width()).show();
	},
	menu_close: function() {
		if(ddmenuitem) {
	  	ddmenuitem.hide();
		}
	},
	menu_timer: function() {
		closetimer = setTimeout(menu.menu_close, timeout);
	},
	menu_canceltimer: function() {
		if(closetimer) {
      clearTimeout(closetimer);
        closetimer = null;
    }
	},
	brand_menu_open: function() {
		menu.menu_canceltimer();
    menu.menu_close();
    ddmenuitem = $(this).next('ul').show();
	}
};

function showLightbox() {
  $("body").append('<div id="lightbox"></div>');
	$("#lightbox").css({"width":$(document).width(),"height":$(document).height()}).show();
}

function closeLightbox() {
  $("#lightbox").remove();
}

function popImg(make,model) {

	var imgUrl = "images/" + make + "/" + make + "_" + model + "_z.jpg";
  	var x = screen.width/2 - 285;
  	var y = screen.height/2 - 175;

	imgWin = window.open("","imgWindow","width=570,height=350,resizable=no,srollbars=no,status=no,left="+x+",screenX="+x+",top="+y+",screenY="+y);
	imgWin.focus();
	imgWin.document.write("<html><head><title>Topanga Creek Bicycles</title></head><body bgcolor=#A7A7A7 topmargin=10 leftmargin=10 marginwidth=10 marginheight=10>");
	imgWin.document.write("<table border=0 cellpadding=0 cellspacing=0>");
	imgWin.document.write("<tr><td align=center>");
	imgWin.document.write("<img src=" + imgUrl + " border=0 width=550 height=330><br>");
	imgWin.document.write("<a href=javascript:window.close()><img src=images/main/close_window.gif border=0 width=70 height=10></a>");
	imgWin.document.write("</td></tr>");
	imgWin.document.write("</table>");
	imgWin.document.write("</body></html>");
	imgWin.document.close();
	
}

function enlgImg(path,model) {

	var imgUrl = "images/" + path + "/" + model + "-z.jpg";
  	var x = screen.width/2 - 285;
  	var y = screen.height/2 - 175;

	imgWin = window.open("","imgWindow","width=570,height=350,resizable=no,srollbars=no,status=no,left="+x+",screenX="+x+",top="+y+",screenY="+y);
	imgWin.focus();
	imgWin.document.write("<html><head><title>Topanga Creek Bicycles</title></head><body bgcolor=#A7A7A7 topmargin=10 leftmargin=10 marginwidth=10 marginheight=10>");
	imgWin.document.write("<table border=0 cellpadding=0 cellspacing=0>");
	imgWin.document.write("<tr><td align=center>");
	imgWin.document.write("<img src=" + imgUrl + " border=0 width=550 height=330><br>");
	imgWin.document.write("<a href=javascript:window.close()><img src=images/main/close_window.gif border=0 width=70 height=10></a>");
	imgWin.document.write("</td></tr>");
	imgWin.document.write("</table>");
	imgWin.document.write("</body></html>");
	imgWin.document.close();
	
}

function imgZoom(model) {
	  var imgUrl = "images/" + model;
  	var x = screen.width/2 - 285;
  	var y = screen.height/2 - 175;

	imgWin = window.open("","imgWindow","width=550,height=350,resizable=no,srollbars=no,left="+x+",screenX="+x+",top="+y+",screenY="+y);
	imgWin.focus();
	imgWin.document.write("<html><head><title>Topanga Creek Bicycles</title></head><body bgcolor=#A7A7A7 topmargin=10 leftmargin=10 marginwidth=10 marginheight=10>");
	imgWin.document.write("<table border=0 cellpadding=0 cellspacing=0>");
	imgWin.document.write("<tr><td align=center>");
	imgWin.document.write("<img src=" + imgUrl + " border=0 width=550 height=330><br>");
	imgWin.document.write("<a href=javascript:window.close()><img src=images/main/close_window.gif border=0 width=70 height=10></a>");
	imgWin.document.write("</td></tr>");
	imgWin.document.write("</table>");
	imgWin.document.write("</body></html>");
	imgWin.document.close();
	
}

function enlargeImg(pic) {

	  var imgUrl = "img_popup.php?images/shop/" + pic + "_z.jpg";
  	var x = screen.width/2 - 285;
  	var y = screen.height/2 - 200;

	imgWin = window.open(imgUrl,"imgWindow","width=550,height=350,resizable=yes,scrollbars=yes,left="+x+",screenX="+x+",top="+y+",screenY="+y);
	imgWin.focus();
	
}

function enlargeCustomImg(pic) {

	  var imgUrl = "img_popup.php?images/custom/" + pic + "_z.jpg";
  	var x = screen.width/2 - 285;
  	var y = screen.height/2 - 200;

	imgWin = window.open(imgUrl,"imgWindow","width=550,height=350,resizable=yes,scrollbars=yes,left="+x+",screenX="+x+",top="+y+",screenY="+y);
	imgWin.focus();
	
}
	
function resizeWindow() {

	var NS = (navigator.appName=="Netscape") ? true : false;
	
	iWidth = (NS) ? imgWin.window.innerWidth : imgWin.document.body.clientWidth; 
  iHeight = (NS) ? imgWin.window.innerHeight : imgWin.document.body.clientHeight; 
  iWidth = imgWin.document.images[0].width - iWidth; 
  iHeight = imgWin.document.images[0].height - iHeight; 
  imgWin.resizeBy(iWidth, iHeight); 
	
}

function checkData(form) {
	if (form.realname.value == "") {
		alert ("Please enter your name.");
		return false;
		}
	else if (form.email.value == "") {
		alert ("Please enter your e-mail address.");
		return false;
		}
	else if (form.comments.value == "") {
		alert ("Please fill out your comments.");
		return false;
		}
	else { return true; }
}

function loadWeather() {
  $.get("/includes/weather.php",function(data) {
	  var weatherDiv = '<div id="weather">'+data+'</div>';
		//$("body").append(weatherDiv);
		//$("#tpg-weather").text($("#weather").html());
	});
}

