jQuery.noConflict();

function log(msg) {
	if (window.console && window.console.log) {
		console.log(msg);
	}
}

/* Language Selector
===========================================================================*/

jQuery(document).ready(function() {
	jQuery("#lang-select").change(function() {
		var lang = jQuery(this).val();
		var domain = document.domain;
		var tld = domain.substring(domain.lastIndexOf(".")+1);
		var redirect = 0;
		var url = '';
		
		if (tld == 'com' && lang != 'en') {
			redirect = 1;
		} else if (tld == 'de' && lang != 'de') {
			redirect = 1;
		}
		
		if (redirect) {
			switch(lang) {
				case 'en':
					url = 'http://www.topcontierra.com';
					break;
				case 'de':
					url = 'http://www.topcontierra.de';
					break;
				default:
					url = 'http://www.topcontierra.com';
			}
			window.location.href = url;
		}
	});
});

/* Site Actions Log In
===========================================================================*/

jQuery(document).ready(function() {
	jQuery("a#log-in-link").toggle(function() {
		jQuery("form#log-in-form").slideDown();
		return false;
	}, function() {
		jQuery("form#log-in-form").slideUp();
		return false;
	});
	
	jQuery("#log-in-form").submit(function() {
		if (jQuery("#wlpeUsername").val() == "" || jQuery("#wlpePassword").val() == "") {
			alert("Please enter your username and password");
			return false;
		} else {
			str = jQuery("#wlpeUsername").val();
			jQuery.cookie('securesite_username',str);

			var url = jQuery("#log-in-form").attr("action");

			if (url.indexOf("api") != -1) {
				// Temporary, post the form normally until API method is finalized
				jQuery.ajax({
					type: "POST",
					url: url,
					success: function(data) {
						return false;
					},
					error: function(data) {
						return false;
					}
				});
			}
		}
	});
	
	if (location.pathname == "/log-out/" || location.pathname == "/log-out") {
		jQuery.cookie('securesite_username',null);
		setTimeout("window.location = '/'",1500);
	}
});

/* Tertiary Nav Tabs
=========================================================================== */

/*
	Allow any reasonble number of tabs, of any name, to be used on product pages
	as needed. Tertiary nav is hidden for non-JS browsers. jQuery UI tab functionality
	has way too much overhead for our needs at this point.
*/

jQuery(document).ready(function() {
	if (jQuery("#tertiary-nav").size() != 0) {
		$contentDivs = jQuery(".tab-content");
		$navItems = jQuery("#tertiary-nav li");

		$contentDivs.hide();
		hash = String(location.hash);

		if (hash) {
			showContent(hash);
			window.scrollTo(0,0);
		} else {
			showContent(jQuery("#tertiary-nav a:first").attr("href"));
		}

		$navItems.find("a").click(function() {
			var id = jQuery(this).attr("href");
			showContent(id);
			return false;
		});
		
		jQuery("div.tab-content a[href*='#']").click(function() {
			var id = jQuery(this).attr("href");
			var path = location.pathname.substr(1);

			if (id.indexOf(path) != -1) {
				// Same page
				id = id.substr(id.indexOf('#'));
				log(id);
				showContent(id);
				return false;
			} else {
				// Load new page
				return true;
			}
		});
	}
});

function showContent(str) {
	$contentDivs.hide();
	$navItems.removeClass("active");
	
	jQuery(str).show();
	jQuery("a[href$='" + str  + "']").parent().addClass("active");
	
	// Unset div id before hash is changed to prevent scrolling to div
	var hash = str.replace(/^#/, '');
	var $fx, $node = jQuery("#" + hash);
	if ($node.length) {
		$fx = jQuery('<div></div>')
						.css({
							position:'absolute',
							visbility:'hidden',
							top: jQuery(window).scrollTop() + 'px'
						})
						.attr('id', hash)
						.appendTo(document.body);
		$node.attr('id','');
	}
	
	// Set the hash
	document.location.hash = str;
	
	// Reset the div id
	if ($node.length) {
		$fx.remove();
		$node.attr('id', hash);
	}
}

/* Gallery Thumbnails
===========================================================================*/

jQuery(document).ready(function() {
	var str = jQuery("ul.thumbs:eq(0) a").attr("href");
	var descr = jQuery("ul.thumbs:eq(0) a img").attr("alt");

	jQuery("#primary-image").attr("src", str);
	jQuery("#primary-caption").text(descr);

	jQuery("ul.thumbs a").each(function() {
		jQuery(this).click(function() {
			str = jQuery(this).attr("href");
			descr = jQuery(this).find("img").attr("alt");
		
			jQuery("#primary-image").attr("src",str);
			jQuery("#primary-caption").text(descr);
			return false;
		});
	});
});

/* Video & Media Playlist
===========================================================================*/

jQuery(document).ready(function() {
	jQuery(".playlist li").live("mouseover", function() {
		jQuery(this).addClass("active");
	});
	jQuery(".playlist li").live("mouseout", function() {
		jQuery(this).removeClass("active");
	});
});

/* Product Registration Form
===========================================================================*/

jQuery(document).ready(function() {
	var form = jQuery("#product-registration-form");
	
	if (form.size() != 0) {
		// form exists
		if (jQuery.cookie('securesite_username')) {		
			form.submit(function() {
				jQuery("#username").attr("value", jQuery.cookie('securesite_username'));
				var data = form.serialize();
				var url = form.attr("action") + "?" + data + "&jsoncallback=?";
				
				jQuery.getJSON(url, function(json) {
					// replace wrapper's inner HTML
					form.empty();
					form.append(json.html);	
				});
				return false; // async; prevent form submission
			});
			
			jQuery(".retry").live("click", function() {
				var link = jQuery(this);
				var url = "http://beta.topcondev.com/warranties/validateSN/" + link.prev("input").val()  + "/&jsoncallback=?";
				jQuery.getJSON(url, {}, (function(link) {
					return function(json) {
						if (json.html == 'false') {
							alert("The serial number is still invalid");
						} else {
							var row = link.closest("tr");
							row.removeClass("invalid");
							row.empty();
							row.append(json.html);
						}
					}
				})(link));
				return false;
			});
		
			jQuery(".select-all").live("click", function() {
				form.find("input:checkbox").attr("checked","checked");
				return false;
			});

			jQuery(".select-none").live("click", function() {
				form.find("input:checkbox").removeAttr("checked");
				return false;
			});
		} else {
			form.empty();
			form.append("Please log in before registering your products");
		}
	}
});