var product_info = "";

window.addEvent('domready', function() {
	parseUrl();
	
	
	if($("detailsku")){
		var url = "/Better PHP/_regular.php";
		var jsonRequest = new Request.JSON({url: url, onComplete: function(returnData){
			product_info = returnData;
			//All items now have appraisals - this function is not necessary
			//processDetailFunctions();
		}}).get({'sku': $("detailsku").value});
	}
	
	
	function processDetailFunctions(){
		if($("detAppraisal")){
			if(product_info.Appraisal == 'Y'){
				$("detAppraisal").innerHTML = "<li>Appraisal</li>";
			}
		}
	}
	
	if($("matty_replace")){
		returnListProducts();
	}

	if($("returnByoDiamonds")){
		createCookie('currentStep', 'diamond');
		buildSteps();
		getDiamondBoundaryValues();
	}

	if($("productMatrix")){
		if($('detail_product_no')){
			getProductDetails($('detail_product_no').value, $('detail_product_category').value);
		}
	}
	if($('totalSteps')){
		if(mattyParams[7] == 'Yes'){
			createCookie('currentStep', 'ring');
			buildSteps();
		}
	}

	if($('relatedAjax')){
		createCookie('sku', $$('.dropDownSku')[0].value);
		getRelatedAjax();
	}
	
	if($('compareDiamondsReturnDiv')){
		getCompareDiamonds();
	}
	
	if($$('.customDetailAddToCart').length > 0){
		var customDetailAddToCart = $$('.customDetailAddToCart');		
		var sizeDropDown = $$('.sizeDropDown');		
		var colorDropDown = $$('.colorDropDown');		
		var dropDownSku = $$('.dropDownSku');
		var dropDownType = $$('.dropDownType');
		

		customDetailAddToCart.each(function(actual_element, index_element){
			actual_element.addEvent('click', function(e){
				var stopForm = false;
				
				if(colorDropDown.length > 0){
					if(colorDropDown[index_element].value.indexOf('size') > -1){
						alert('Please select a size before building this ring');
						stopForm = true;
					}
					if(colorDropDown[index_element].value.indexOf('prong') > -1){
						alert('Please select a prong type before building this ring');
						stopForm = true;
					}
				}
				if(sizeDropDown.length > 0){
					if(sizeDropDown[index_element].value.indexOf('size') > -1){
						alert('Please select a size before building this ring');
						stopForm = true;
					}
					if(sizeDropDown[index_element].value.indexOf('prong') > -1){
						alert('Please select a prong type before building this ring');
						stopForm = true;
					}
				}
				if(stopForm == false){
					createCookie(dropDownType[index_element].value, dropDownSku[index_element].value);
					
					if(dropDownType[index_element].value == 'byo_ring'){
						if(sizeDropDown.length > 0){
							createCookie('byo_ring_size', sizeDropDown[index_element].value);
						}
						if(colorDropDown.length > 0){
							createCookie('byo_ring_color', colorDropDown[index_element].value);
						}
						
						if(readCookie('byo_diamond')){ /* We're on a setting page adding to custom ring when a diamond has already been set */
							window.location.href = "http://www.diamondwave.com/-strse-template/buildyourown/Page.bok";
						}
						else{ /* We're on a setting page but need to go get diamond */
							clearDiamondCookies();
							pars = "sku"+ dropDownSku[0].value + "&type=ring";
							var ajax_url = "http://www.diamondwave.com/php/set_byo_cookies.php?" + pars;
							new Ajax(ajax_url, {
								onComplete: window.location.href = "http://www.diamondwave.com/-strse-template/diamondsearch-dsh-advanced/Page.bok",
								evalScripts: true
							}).request();
						}
					}
					if(dropDownType[index_element].value == 'byo_diamond'){
						if(readCookie('byo_ring')){ /* We're on a diamond page adding to custom ring when a setting has already been set */
							window.location.href = "http://www.diamondwave.com/-strse-template/buildyourown/Page.bok";
						}
						else{ /* We're on a diamond page but need to go get setting */
							pars = "sku"+ dropDownSku[0].value + "&type=diamond";
							var ajax_url = "http://www.diamondwave.com/php/set_byo_cookies.php?" + pars;
							new Ajax(ajax_url, {
								onComplete: window.location.href = "http://www.diamondwave.com/-strse-All/12/1/0/Yes//Categories.bok",
								evalScripts: true
							}).request();
						}
					}
				}
			});
		});
	}	

	if($$('.regularAddToCart').length > 0){
		var regularDetailAddToCart = $$('.regularAddToCart');		
		var sizeDropDown = $$('.sizeDropDown');		
		var colorDropDown = $$('.colorDropDown');		

		regularDetailAddToCart.each(function(actual_element, index_element){
			actual_element.addEvent('click', function(e){
				var stopForm = false;
				
				if(colorDropDown.length > 0){
					if(colorDropDown[index_element].value.indexOf('size') > -1){
						alert('Please select a size before adding to shopping basket');
						stopForm = true;
					}
					if(colorDropDown[index_element].value.indexOf('prong') > -1){
						alert('Please select a prong type before adding to shopping basket');
						stopForm = true;
					}
				}
				if(sizeDropDown.length > 0){
					if(sizeDropDown[index_element].value.indexOf('size') > -1){
						alert('Please select a size before adding to shopping basket');
						stopForm = true;
					}
					if(sizeDropDown[index_element].value.indexOf('prong') > -1){
						alert('Please select a prong type before adding to shopping basket');
						stopForm = true;
					}
				}
				if(stopForm == false){
					document.cartadd.submit();
				}
			});
		});
	}	

});

function post_set_cookie(){
	if(readCookie('byo_diamond')){
		if(readCookie('byo_ring')){
			window.location.href = "http://www.diamondwave.com/-strse-template/buildyourown/Page.bok";
		}
		else{
			window.location.href = "http://www.diamondwave.com/-strse-All/12/1/0/Yes//Categories.bok";
		}
	}
	else{
		window.location.href = "http://www.diamondwave.com/-strse-template/diamondsearch/Page.bok";
	}
}

var mattyParams = new Array;				
var tmp_url_array = new Array;
var queryString = "";
var main_url = "http://www.diamondwave.com/";


function Querystring(qs) { // optionally pass a querystring to parse
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}


function parseUrl(){

var actual_url = document.URL;
if(actual_url.indexOf('?') > -1){
	queryString = actual_url.substring(actual_url.indexOf('?'), actual_url.length);
	actual_url = actual_url.substring(0, actual_url.indexOf('?'));
}
else {
	actual_url = actual_url.replace(/cart_id\/[0-9]{0,}\//, '');
	// remove the stupid pro$hit cart ID appendage to URL
}
var tmp_url_array = actual_url.split("/");

	
	mattyParams[0] = tmp_url_array[0];
	mattyParams[1] = tmp_url_array[1];
	mattyParams[2] = tmp_url_array[2];
	mattyParams[3] = tmp_url_array[3];
	mattyParams[4] = tmp_url_array[4];

	if(tmp_url_array[5]){
		mattyParams[4] = tmp_url_array[4];
		mattyParams[5] = tmp_url_array[5];
		mattyParams[6] = tmp_url_array[6];
		mattyParams[7] = tmp_url_array[7];
		mattyParams[8] = tmp_url_array[8];
		mattyParams[9] = tmp_url_array[9];
		mattyParams[10] = tmp_url_array[10];
	}
	else{
		if(tmp_url_array[3] == "Categories.bok"){
			mattyParams[3] = "-strse-All";
		}
		mattyParams[4] = 12;
		mattyParams[5] = 1;
		mattyParams[6] = 0;
		mattyParams[7] = "";
		mattyParams[8] = "";
		mattyParams[9] = "";
		mattyParams[10] = "Categories.bok";
	}

}


var Site = new Class({
	initialize: function(){
		this.panel_functions();
	},
	panel_functions: function(){
	
		if($('matty_replace')){
			
			if($$('.mattyPagesSelect')[0]){
				/*** Pages ***/
				var matty_pages_select = $$('.mattyPagesSelect')[0];		
				matty_pages_select.addEvent('change', function(e){
					mattyParams[4] = $$('.mattyPagesSelect')[0].value;
					processRun();
				});
			}
			if($$('.mattySortSelect')[0]){
				/*** Sort ***/
				var matty_sort_select = $$('.mattySortSelect')[0];
				matty_sort_select.addEvent('change', function(e){
					mattyParams[6] = $$('.mattySortSelect')[0].value;
					processRun();
				});
		
			}
			if($$('.mattyMetalsSelect')[0]){
				/*** Metal ***/
				var matty_metal_select = $$('.mattyMetalsSelect')[0];
				matty_metal_select.addEvent('change', function(e){
					mattyParams[8] = $$('.mattyMetalsSelect')[0].value;
					processRun();
				});
			}
			if($$('.mattyClaritySelect')[0]){
				/*** Metal ***/
				var matty_clarity_select = $$('.mattyClaritySelect')[0];
				matty_clarity_select.addEvent('change', function(e){
					mattyParams[9] = $$('.mattyClaritySelect')[0].value;
					processRun();
				});
			}

			if($$('.mattyPagesSelect')[1]){
				/*** Pages ***/
				var matty_pages_select = $$('.mattyPagesSelect')[1];		
				matty_pages_select.addEvent('change', function(e){
					mattyParams[4] = $$('.mattyPagesSelect')[1].value;
					processRun();
				});
			}
			if($$('.mattySortSelect')[1]){
				/*** Sort ***/
				var matty_sort_select = $$('.mattySortSelect')[1];
				matty_sort_select.addEvent('change', function(e){
					mattyParams[6] = $$('.mattySortSelect')[1].value;
					processRun();
				});
		
			}
			if($$('.mattyMetalsSelect')[1]){
				/*** Metal ***/
				var matty_metal_select = $$('.mattyMetalsSelect')[1];
				matty_metal_select.addEvent('change', function(e){
					mattyParams[8] = $$('.mattyMetalsSelect')[1].value;
					processRun();
				});
			}
			if($$('.mattyClaritySelect')[1]){
				/*** Metal ***/
				var matty_clarity_select = $$('.mattyClaritySelect')[1];
				matty_clarity_select.addEvent('change', function(e){
					mattyParams[9] = $$('.mattyClaritySelect')[1].value;
					processRun();
				});
			}

			if($$('#mattyStylesImageOptionsWrapper img')){
				/*** Subs ***/
				var total_subs = $$('#mattyStylesImageOptionsWrapper img');
				
				total_subs.each(function(actual_element, index_element){
					actual_element.addEvent('click', function(e){
						total_subs.each(function(actual_element2, index_element2){
							total_subs[index_element2].src = total_subs[index_element2].title;
						});
						actual_element.src = actual_element.alt;
						mattyParams[3] = actual_element.name;
						processRun();
					});
				});
			}
		}
	}
});

function processRun(){
	mattyParams[5] = 1;
	var url_with_changed_parameters = mattyParams.join('/');
	if(queryString != ''){
		url_with_changed_parameters += queryString;
	}
	window.location.href = url_with_changed_parameters;
}


function afterAjaxFunctions(){
	var new_Class = new Site();
}


function toggleDrops(toggleVictim){
	if(toggleVictim.className=='show'){
		toggleVictim.className = 'hide';
	}
	else{
		toggleVictim.className = 'show';
	}
}

function valChange(sliderObj, val, newPos, knobNumber) { 
	if(sliderObj.totalKnobs == 2){
		if(knobNumber == 1){
			$('matty' + sliderObj.name + 'SlideractualMin').innerHTML = val;
			mattyParams[sliderObj.minHtmlVal] = val;
		}
		if(knobNumber == 2){
			$('matty' + sliderObj.name + 'SlideractualMax').innerHTML = val;
			mattyParams[sliderObj.maxHtmlVal] = val;
		}
	}
	else{
		$('matty' + sliderObj.name + 'SlideractualMin').innerHTML = val;
		mattyParams[sliderObj.minHtmlVal] = val;
	}
}
function valEnd(sliderObj, val, newPos) { 
	processRun();
}

/*  For the love of all that is good, the doctype must be transitional */
function createSlider(val, width, height, sliderMin, sliderMax, actualMin, actualMax){
	val.width         = width;
	val.height        = height;
	val.imgDir   = '/images/NeoSet2/';
	val.direction = 1;
	val.attachOnChange(valChange);
	val.attachOnSlideEnd(valEnd);		
	val.fieldName     = 'val_start';
	val.minVal        = sliderMin;
	val.maxVal        = sliderMax;
	val.valueInterval = 1;
	val.valueDefault  = actualMin;
	val.useInputField = 0;
	val.styleValueFieldClass = 'sliderInput';		
	if(actualMax != -1){
		val.totalKnobs = 2;
		val.useSecondKnob        = true;
		val.preventValueCrossing = true;
		val.wheelAmount        = 0; //disable mouse wheeling cause we have 2 knobs.		
		val.fieldName2     = 'val_end';
		val.minVal2        = sliderMin;
		val.maxVal2        = sliderMax;
		val.valueInterval2 = 1;
		val.valueDefault2  = actualMax;
		val.setSliderIcon2('up.gif', 20, 15);//file,width,height
		val.useInputField2 = 0;
		val.styleValueFieldClass2 = 'sliderInput';	
		val.setSliderIcon('down.gif', 20, 15);
	}
	else{
		val.totalKnobs = 1;
		val.setSliderIcon('slider_large.gif', 80, 21);
	} 
	val.drawInto('matty' + val.name + 'Slider');
	
}
