$(function() {
	//----------------------------------
	// load navigation
	//----------------------------------

	// header menu
	$("#header").appendTo("#header_wrapper");
	var menuID = $("#menuID").text();
	$("#main_service-"+menuID).addClass("current");
	jqueryslidemenu.buildmenu("main_service");
	// left menu
	$(".menuboxWrapper").appendTo("#menuColumn");
	var prodCategory = $("#prodCategory").text();
	$("#cat_"+prodCategory).addClass("current");
	$("#product_categories ul").accordion();
	// product search: select all when focusing
	$("#product_search .input_text")
		.attr('value', 'Search Products...')
		.focus(function(){
			$(this).select();
		});
	$("#product_search .input_image")
		.click(function(){
			if($("#product_search .input_text").val() == "Search Products...") {
				$("#product_search .input_text").val("");
			}
		});
	// change menu links from https to http
	if(location.protocol == "https:") {
		var myhostname = location.hostname;
		$("a").each(function() {
			if(!($(this).attr("href").match(/javascript/))) {
				$(this).attr("href", "http://"+myhostname+$(this).attr("href"));
			}
		});
	}

	//----------------------------------
	// adding some contents
	//----------------------------------

	// feed widget
	var feed_element = document.getElementById('TMC_feed');
	if(feed_element) {
		$('#TMC_feed').feedfetcher({
			feed_source: 'http://www.tmcfreeriderz.com/blog/syndication.axd'
		});
	}

	//----------------------------------
	// setting div tags
	//----------------------------------

	// product
	$('.product').wrap($('<p class="product">').get(0));
	// product_comment
	$('.prod_comment').wrap('<span class="prod_comment_wrapper"></span>');
	// section
	$('.section')
		.before('<div class="section_upper"></div>')
		.after('<div class="section_lower"></div>');
	// universal box
	$('.universal_box')
		.wrapInner('<div class="uMid"><p></p></div>')
		.prepend('<div class="uTop"><p class="uLeft"></p><p class="uRight"></p></div>')
		.append('<div class="uBottom"><p class="uLeft"></p><p class="uRight"></p></div>');

	// google map
	var gmap_element = document.getElementById('gmapbox');
	if(gmap_element) {
		if(GBrowserIsCompatible()) {
			var map = new GMap2(gmap_element);
			var point = new GLatLng(50.115014,-122.95458);
			var icon = new GIcon();
			icon.image = 'http://www.google.com/intl/en_ALL/mapfiles/arrow.png';
			icon.shadow = 'http://www.google.com/intl/en_ALL/mapfiles/arrowshadow.png';
			icon.iconSize   = new GSize(39,34);
			icon.shadowSize = new GSize(39,34);
			icon.iconAnchor = new GPoint(13,34);
			icon.infoWindowAnchor = new GPoint(icon.iconSize.width/3, 0);

			var marker = new GMarker(point,icon);
			map.setCenter(point,15);
			map.setMapType(G_NORMAL_MAP);
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			map.addOverlay(marker);
		}
	}

	//----------------------------------
	// adding some actions
	//----------------------------------

	// TEST ENV or not
	var myhostname = location.hostname;
	if((myhostname != "www.tmcfreeriderz.com") && (myhostname != "tmcfreeriderz.com")) {
		//$("title").text("=TEST_ENV= " + $("title").text());
		$("#wrapper").css("border","3px solid red");
	}

	// adjust margin of section h1
	$(".section h1:gt(0)").css("margin-top","1.5em");

	// add class for form-input
	$("input[type=text]").addClass("input_text");
	$("input[type=submit]").addClass("input_submit");


	// add2cart button
	$(".detailbox :image").focus(
		function(){ $(this).blur() } // remove dotted line on focus
	)
	$(".detailbox :image").hover(
		function(){ $(this).attr("src","/images/tmc/add2cart_active.gif") },
		function(){ $(this).attr("src","/images/tmc/add2cart.gif") }
	);
	// checkout button
	$(".checkout :image").focus(
		function(){ $(this).blur() } // remove dotted line on focus
	)
	$(".checkout :image").hover(
		function(){ $(this).attr("src","/images/tmc/checkout_active.gif") },
		function(){ $(this).attr("src","/images/tmc/checkout.gif") }
	);

	//----------------------------------
	// checkout
	//----------------------------------

	// cascade select for countries and states
	function commonTemplate(item) {
		return "<option value='" + item.Value + "'>" + item.Value + "</option>";
	};
	function commonMatch(selectedValue) {
		return this.When == selectedValue; 
	};
	function tobbleSelState(jquery_object) {
		// select header
		jquery_object.prepend("<option value=''>--Select Your State--</option>");
		jquery_object.find("option:first")[0].selected = true;
		// toggle visibility
		if(jquery_object.children("option").length > 1) {
			jquery_object.removeAttr("disabled");
		}
		else {
			jquery_object.attr("disabled", "disabled");
		}
	}
	// Billing Address
	if(self['list1']) {
		$("#cart_sel_state")
		.cascade("#cart_sel_country",{
			list: list1,
			template: commonTemplate,
			match: commonMatch
		})
		.bind("loaded.cascade",function(e,target) { 
			tobbleSelState($(this));
		});
	}
	// Shipping Address
	if(self['list1']) {
		$("#cart_sel_state_forShip")
		.cascade("#cart_sel_country_forShip",{
			list: list1,
			template: commonTemplate,
			match: commonMatch
		})
		.bind("loaded.cascade",function(e,target) { 
			tobbleSelState($(this));
		});
	}

	// Shipping Method
	function changePayprovider(jqeury_object) {
		if(jqeury_object.val() == "Canada" || jqeury_object.val() == "United States of America") {
			$("input[name='payprovider_forDisplay']").val(["1"]);
			$("#hidden_payprovider").val("1");
		}
		else {
			$("input[name='payprovider_forDisplay']").val(["4"]);
			$("#hidden_payprovider").val("4");
		}
	}
	$("#cart_sel_country").change(function() {
		if($("#cart_sel_country_forShip").val() == "") {
			changePayprovider($(this));
		}
	});
	$("#cart_sel_country_forShip").change(function() {
		changePayprovider($(this));
	});
	// Initialize
	if($("#cart_sel_country_forShip").val() == "") {
		changePayprovider($("#cart_sel_country"));
	}
	else {
		changePayprovider($("#cart_sel_country_forShip"));
	}
	// User Comments
	var user_comments_element = document.getElementById('user_comments');
	if(user_comments_element) {
		$("a[href*='#']").slideScroll();
	}

	//----------------------------------
	// rental reservation
	//----------------------------------
	if(location.pathname == "/en/rentals-reservation.asp") {
		$("#renter_options tr.renter_row").hide();
		$("select#renters")
			.change(function() {
				var renters = $("select#renters option:selected").text();
				$("table#renter_options tbody tr").hide();
				$("table#renter_options tbody tr:lt("+renters +")").show();
			})
			.change();
		// product search: select all when focusing
		$(".r_gears")
			.change(function() {
				if($(this).val() == 4) {
					$(this).nextAll(".r_poles").fadeOut();
				}
				else {
					$(this).nextAll(".r_poles").fadeIn();
				}
			})
			.change();
		$("#start_date,#end_date").datePicker();
	}


	//----------------------------------
	// staff
	//----------------------------------
	if(location.pathname == "/en/staff.htm") {
		$(".staff a").lightBox({
			imageLoading: '/images/tmc/lightbox/lightbox-ico-loading.gif',
			imageBtnClose:'/images/tmc/lightbox/lightbox-btn-close.gif',
			imageBtnPrev: '/images/tmc/lightbox/lightbox-btn-prev.gif',
			imageBtnNext: '/images/tmc/lightbox/lightbox-btn-next.gif'
		});
	}
	//----------------------------------
	// rising stars team
	//----------------------------------
	if(location.pathname == "/en/rising-stars-team.htm") {
		$(".rs_photo a").lightBox({
			imageLoading: '/images/tmc/lightbox/lightbox-ico-loading.gif',
			imageBtnClose:'/images/tmc/lightbox/lightbox-btn-close.gif',
			imageBtnPrev: '/images/tmc/lightbox/lightbox-btn-prev.gif',
			imageBtnNext: '/images/tmc/lightbox/lightbox-btn-next.gif'
		});
	}

});
