$(document).ready(function() {

// Buy Forms
$("form.buyForm select.mainSelect").each(function(i) {
	var parentItem = $(this).parent().parent();
	var parentId = parentItem.attr("id");
	var countryId = parentItem.attr("rel");
	$(this).change(function(e) {
		var buyType = "";
		var productType = "";

		$("form#"+parentId).children("p").children("select.delivery").each(function(i) {
			$(this)[0].selectedIndex = 0;
			$(this).children("option").each(function(i) {
				$(this).attr("SELECTED","");
			});
		});

		if ($(this).children("option:selected").val().replace("pro-","").replace("std-","") == "upgrade") {
			productType = $(this).children("option:selected").val().replace("-upgrade","");
	
			buyType = "upgrade";
			$("form#"+parentId).children("p.upgrade-option").show();
	
			var upgradeItem = $("form#"+parentId).children("p.upgrade-option").children("select.upgrade-option").children("option:first").val();

			$("form#"+parentId).children("p").children("select.delivery").hide();
			$("form#"+parentId).children("p").children("select#"+upgradeItem).show();
		} else if ($(this).children("option:selected").val().replace("pro-","").replace("std-","") == "full") {
			productType = $(this).children("option:selected").val().replace("-full","");
			buyType = "full";
	
			$("form#"+parentId).children("p.upgrade-option").hide();
			$("form#"+parentId).children("p").children("select.delivery").hide();
			$("form#"+parentId).children("p").children("select#"+productType+"-"+buyType).show();
			$("form#"+parentId).children("p").children("select#"+productType+"-"+buyType).children("option:first").attr("SELECTED","SELECTED");
		}
	});
	$(this).parent().parent().children("p.add-to-cart").children("a").click(function(e) {
		e.preventDefault();
		var selectedProduct = "";
		
		var selectedItem = $("form#"+parentId).children("p").children("select.mainSelect").children("option:selected").val();
		
		if (selectedItem.replace("pro-","").replace("std-","") == "full") {
			selectedProduct = $("form#"+parentId).children("p").children("select#"+selectedItem).children("option:selected").val();
		} else {
			var selectedUpgrade = $("form#"+parentId).children("p").children("select.upgrade-option").children("option:selected").val();
			selectedProduct = $("form#"+parentId).children("p").children("select#"+selectedUpgrade).children("option:selected").val();
		}

		window.open('http://store.digitalriver.com/store/datawatc/en_'+countryId+'/buy/productID.'+ selectedProduct +'/quantity.1','_newtab');
	});
});
$("select.upgrade-option").each(function(i) {
	if ($(this).children("option").length > 1) {
		var parentItem = $(this).parent().parent();
		var parentId = parentItem.attr("id");
		$(this).change(function(e) {
			var upgradeItem = $("form#"+parentId).children("p.upgrade-option").children("select.upgrade-option").children("option:selected").val();

			$("form#"+parentId).children("p").children("select.delivery").hide();
			$("form#"+parentId).children("p").children("select#"+upgradeItem).show();
		});
	}
});

$('#announcementBot').after('<div id="featureNav">').cycle({
    fx: 'fade',
    pager: '#featureNav',
    pause: 1,
    speed: 500,
    timeout: 15000
});

$('a.inlineEvent').cluetip({ width: '600px', showTitle: false, local: true, positionBy: 'mouse' });

$('a.regionPicker').cluetip({ 
	width: '250px', 
	showTitle: false, 
	local: true, 
	positionBy: 'mouse', 
	sticky: true, 
	activation: 'click',
	closePosition: 'bottom' 
});

$('a.upgradeNotice').cluetip({ 
	width: '425px', 
	showTitle: false, 
	local: true, 
	topOffset: 0,
	leftOffset: -435,
	arrows: true,
	positionBy: 'auto'
});

$(".upgrade-option").change(function() {
    $(".upgrade-option").val() == ".pro-upgrade" | $(".upgrade-option").val() == ".std-upgrade" ? $("stdstd-upgrade").show() : $("stdstd-upgrade").hide();
});

// perform JavaScript after the document is scriptable.
$(function() {
	// setup ul.tabs to work as tabs for each div directly under div.panes
	$("ul.tabs").tabs(".panecontent");
});

$("#Payment").change(function() {
    $("#Payment").val() == "Visa" | $("#Payment").val() == "MasterCard" | $("#Payment").val() == "American Express" ? $(".altHide").show() : $(".altHide").hide();
});

var myFile = document.location.toString();

if (myFile.match('course')) { // the URL contains an anchor
    // click the navigation item corresponding to the anchor
    var myAnchor = myFile.split('#')[1];

    var toggleLink = $('a[href="#' + myAnchor + '"]').attr("rel");
    $('div #' + toggleLink).toggle();
    if (myAnchor) {
        var top = $('#' + myAnchor).offset().top;
        self.scrollTo(0, top);
    }

}

	$('.CourseLink').click(function() {
	    $("#Course1").hide();
	    $("#Course2").hide();
	    $("#Course3").hide();
	    $("#Course4").hide();
	    $("#Course5").hide();
	    $("#Course6").hide();
	    $("#Course7").hide();
	    $("#Course8").hide();
	
	    $('div #' + $(this).attr("rel")).toggle();
	});

});

// centered popUp window
function NewWindow(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable';
win = window.open(mypage, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}
