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.topconpa.com';
					break;
				case 'de':
					url = 'http://www.topconpa.de';
					break;
				default:
					url = 'http://www.topconpa.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("form#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);
		}
	});
	
	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() {
	var hash = location.hash;
	if (jQuery("#tertiary-nav").size() != 0) {
		jQuery(".tab-content").hide();
		if (hash) {
			// Load hashed tab
			jQuery(hash).show();
			hash = hash.substring(1);
			hash = hash.replace("and","&amp;");
			var newHash = ''
			hash = hash.split("-");
			for (var i = 0; i < hash.length; i++) {
				newHash += hash[i].substring(0,1).toUpperCase() + hash[i].substring(1,hash[i].length) + " ";
			}
			hash = newHash.substring(0,newHash.length-1);

			jQuery("#tertiary-nav li a").each(function() {
				if (jQuery(this).html() == hash) {
					log("match");
					jQuery(this).parent().addClass("active");
				}
			});
		} else {
			// Load first tab
			jQuery("#tertiary-nav li:first").addClass("active");

			var tabStr = "#" + jQuery("#tertiary-nav li:first").find("a").html().replace(/ /g,"-").replace("&amp;","and").toLowerCase();
			jQuery(tabStr).show();
		}
	}
	
	jQuery("#tertiary-nav li").click(function() {
		// Tab names aren't valid CSS IDs
		var tabStr = "#" + jQuery(this).find("a").html().replace(/ /g,"-").replace("&amp;","and").toLowerCase();
	
		jQuery(".tab-content").hide();
		jQuery("#tertiary-nav li").removeClass("active");
	
		jQuery(tabStr).show();
		jQuery(this).addClass("active");
		return false;
	});
});

/* 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");
		}
	}
});