
$(document).ready(function() {
	
	popup();
	
	optionValue = $("#voucherValues").val();		//alert(optionValue);
	$("#voucherAddCartBtn").attr("href",optionValue);
	
//	optionValue = $("#sRegion").val();		//alert(optionValue);
//	$("#countryBtn").attr("href",optionValue);

	$('#quantity').bind('keyup', function() {
		stockLevel = $('#stockLevel').text();
		if($(this).val() > stockLevel) {
			$('#quantityError').show();
			$('#addCartBtn').hide();
			
		} else {
			$('#quantityError').hide();
			$('#addCartBtn').show();
		}
	});
	
//	jQuery('#productCarousel').jcarousel({
//		scroll: 1,
//		initCallback: productcarousel_initCallback,
//		// This tells jCarousel NOT to autobuild prev/next buttons
//		buttonNextHTML: null,
//		buttonPrevHTML: null 
//	});
//	
//	jQuery('#artistCarousel').jcarousel({
//		scroll: 1,
//		initCallback: artistcarousel_initCallback,
//		// This tells jCarousel NOT to autobuild prev/next buttons
//		buttonNextHTML: null,
//		buttonPrevHTML: null 
//	});
//	
//	jQuery('#artistCarousel1').jcarousel({
//		scroll: 1,
//		initCallback: artistcarousel1_initCallback,
//		// This tells jCarousel NOT to autobuild prev/next buttons
//		buttonNextHTML: null,
//		buttonPrevHTML: null 
//	});
//	
//	jQuery('#artistCarousel2').jcarousel({
//		scroll: 1,
//		initCallback: artistcarousel2_initCallback,
//		// This tells jCarousel NOT to autobuild prev/next buttons
//		buttonNextHTML: null,
//		buttonPrevHTML: null 
//	});
//	
//	jQuery('#artistCarousel3').jcarousel({
//		scroll: 1,
//		initCallback: artistcarousel3_initCallback,
//		// This tells jCarousel NOT to autobuild prev/next buttons
//		buttonNextHTML: null,
//		buttonPrevHTML: null 
//	});
//	
//	jQuery('#artistCarousel4').jcarousel({
//		scroll: 1,
//		initCallback: artistcarousel4_initCallback,
//		// This tells jCarousel NOT to autobuild prev/next buttons
//		buttonNextHTML: null,
//		buttonPrevHTML: null 
//	});
		
    bVersion = getInternetExplorerVersion();
    
    $('.productCategoryNav li a.selected').siblings('ul').show();
    
    $('.productCategory').click(function () {
    	$('.productTypeNav').hide();
    	$('.productCategory').removeClass("selected");
    	$(this).addClass("selected");
    	$(this).siblings('ul').show();
    	return false;
    });
    
    $('.bannerImages').innerfade({
    	animationtype: 'fade',
    	speed: 2000,
		timeout: 5000,
		type: 'sequence',
		containerheight: '631px'
    });
    
});

//document.getElementById('sProduct').onchange = submitform;
//
//var submitform = function() {
//	refinesearchForm.submit(); 
//}

//function productcarousel_initCallback(carousel) {
//	jQuery('#productCarousel-next').bind('click', function() {
//		carousel.next();
//		return false;
//	});
//	
//	jQuery('#productCarousel-prev').bind('click', function() {
//		carousel.prev();
//		return false;
//	});
//}; 
//
//function artistcarousel_initCallback(carousel) {
//	jQuery('#artistCarousel-next').bind('click', function() {
//		carousel.next();
//		return false;
//	});
//	
//	jQuery('#artistCarousel-prev').bind('click', function() {
//		carousel.prev();
//		return false;
//	});
//};
//
//function artistcarousel1_initCallback(carousel) {
//	jQuery('#artistCarousel-next1').bind('click', function() {
//		carousel.next();
//		return false;
//	});
//	
//	jQuery('#artistCarousel-prev1').bind('click', function() {
//		carousel.prev();
//		return false;
//	});
//}; 
//
//function artistcarousel2_initCallback(carousel) {
//	jQuery('#artistCarousel-next2').bind('click', function() {
//		carousel.next();
//		return false;
//	});
//	
//	jQuery('#artistCarousel-prev2').bind('click', function() {
//		carousel.prev();
//		return false;
//	});
//}; 
//
//function artistcarousel3_initCallback(carousel) {
//	jQuery('#artistCarousel-next3').bind('click', function() {
//		carousel.next();
//		return false;
//	});
//	
//	jQuery('#artistCarousel-prev3').bind('click', function() {
//		carousel.prev();
//		return false;
//	});
//}; 
//
//function artistcarousel4_initCallback(carousel) {
//	jQuery('#artistCarousel-next4').bind('click', function() {
//		carousel.next();
//		return false;
//	});
//	
//	jQuery('#artistCarousel-prev4').bind('click', function() {
//		carousel.prev();
//		return false;
//	});
//};  

function refineChangeHandler() {
	//$("#refinesearchForm").submit();
	$('#submitSearch').click();
}

function voucherChangeHandler() {
	optionValue = $("#voucherValues").val();		//alert(optionValue);
	$("#voucherAddCartBtn").attr("href",optionValue);
	$("#voucherAddCartBtn").removeAttr("onclick");
}

function myChangeHandler() {
	optionValue = $("#sRegion").val();		//alert(optionValue);
	$("#countryBtn").attr("href",optionValue);
	$("#countryBtn").removeAttr("onclick");
}

function categoryChangeHandler() {
	optionValue = $("#categoryDropdown").val();
	//alert(optionValue);
	
	if(optionValue == 'country') {
		$.ajax({    
			type: "GET",
			url: "/admin-resources/repository/assets/country_item/asc_country-item-title/list.xml",
		    dataType: "xml",
		    success: parseCountryXml
		});		
	} else if (optionValue == 'category') {
		$.ajax({    
			type: "GET",
			url: "/admin-resources/repository/assets/product_category/asc_product-category-title/list.xml",
		    dataType: "xml",
		    success: parseCategoryXml
		});
	} else if (optionValue == 'artist') {
		$.ajax({
			type: "GET",
			url: "/admin-resources/repository/assets/artist/asc_artist-family-name/list.xml",
		    dataType: "xml",
		    success: parseArtistXml
		});
	} else if (optionValue == 'price') {
		$.ajax({    
			type: "GET",
			url: "/admin-resources/repository/assets/price_range/list.xml",
		    dataType: "xml",
		    success: parsePriceRangeXml
		});
	}
}

function parseCategoryXml(xml) {
  //find every Category and populate the sub dropdown
    $('#subDropdown').attr('name', 'productCategoryId');
    $('#subDropdown').find('option').remove().end().append('<option>Choose Sub-Category</option>');
    $(xml).find("product-category").each(function() {
    	$('#subDropdown').append("<option value="+$(this).attr('product-category-id')+">"+$(this).find("product-category-title").text()+"</option>");
    	//$("#output").append($(this).attr("author") + "<br />");
    });
}

function parseCountryXml(xml) {
  //find every Category and populate the sub dropdown
    $('#subDropdown').attr('name', 'productCountryId');
    $('#subDropdown').find('option').remove().end().append('<option>Choose Sub-Category</option>');
    $(xml).find("country-item").each(function() {
    	$('#subDropdown').append("<option value="+$(this).attr('country-item-id')+">"+$(this).find("country-item-title").text()+"</option>");
    	//$("#output").append($(this).attr("author") + "<br />");
    });
}

function parseArtistXml(xml) {
  //find every Category and populate the sub dropdown
  $('#subDropdown').attr('name', 'productArtistId');
	$('#subDropdown').find('option').remove().end().append('<option>Choose Sub-Category</option>');  
    $(xml).find("artist").each(function() {
    	$('#subDropdown').append("<option value="+$(this).attr('artist-id')+">"+$(this).find("artist-given-name").text()+" "+$(this).find("artist-family-name").text()+"</option>");
    	//$("#output").append($(this).attr("author") + "<br />");
    });
}

function parsePriceRangeXml(xml) {
  //find every Category and populate the sub dropdown
  $('#subDropdown').attr('name', 'priceRange');
	$('#subDropdown').find('option').remove().end().append('<option>Choose Sub-Category</option>');
    $(xml).find("price-range").each(function() {
    	start = $(this).find('price-start').text();
    	//alert(start);
    	if(start == '') {
			start = 0;    		
    	}
    	//$('#subDropdown').append("<option value='test'>test</option>");
    	$('#subDropdown').append("<option value="+start+"-"+$(this).find('price-end').text()+">"+start+"-"+$(this).find('price-end').text()+"</option>");
    	//$("#output").append($(this).attr("author") + "<br />");
    });
}

function getInternetExplorerVersion() {
	var rv = -1;
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
		rv = parseFloat( RegExp.$1 );
	}
	return rv;
}

function popupWindow(url, name, attributes){
	var newWindow = window.open(url, name, attributes);
	newWindow.focus();
} 


function popup() {
	$('a.popupLink').bind('click', function(e) { popupWindow($(this).attr('href'), $(this).attr('title').replace(/[^A-z]/ig, ''),'width=736, height=600, scrollbars'); e.preventDefault(); });
};
 