

// small submitToLink-Plugin by Medienfreunde
// grabs every input with type submit, hides it and replaces it with an a-tag which clicks the button
submitToLink = function () {
   $$('form input[type="submit"]', 'form input[type="reset"]','input[type="button"]').each( function(b){replaceSubmit(b)});
};

replaceSubmit = function(inp) {
	var a = document.createElement("a");
	a.innerHTML = inp.readAttribute('value');
	a.href = '';
	a.className = inp.readAttribute('class') || 'none';
	a.setAttribute('title',(inp.readAttribute('title') || inp.readAttribute('tip') || ""));
	inp.parentNode.insertBefore(a, inp);
	//inp.hide();
	inp.setStyle({
	   width: '0px',
	   height: '0px',
	   position: 'absolute',
	   left: '-10000px'
	});
	Event.observe(a, 'click', function(e) {
		inp.click();
		Event.stop(e);
	});
};


// hides the Tooltip if necessary

hideTip = function () {
	if (document.getElementById('tooltip'))	document.getElementById('tooltip').style.visibility = 'hidden';
};



// TabStarter

function initTabs() {
	TabControl('tab-content-holder', {
		current: (document.location.href.match(/#([-_\w]+)$/) || []).last() // try to catch the element from the url if nothing is found the first element will be displayed
		// current: (document.location.href.match(/#([-_\w]+)$/) || []).last() || 'product-list' // try to catch the element from the url or use id 'product-list' instead
	});
};


// closing the wizzard
// ### deprecated ###

function closeWizzard() {
	if (document.getElementById('wizzard')) {
		var close = document.getElementById('close-wizz').getElementsByClassName('close')[0];
		close.onclick = function() {
      $('wizzard').remove();
      hideTip();
      return false;
    };
	};
};

//Used to clear inputfileds onfocus
function clearText(thefield){
	if (thefield.defaultValue==thefield.value)
		thefield.value = ""
}

// opening popups
// Use like this:
// <a href="popup.html" onclick="return Popup.openup(this);">Popup with default size</a>
// <a href="popup.html" onclick="return Popup.openup(this,1000,400);">Popup with special size</a>
Popup = {
	openup:function(obj, w, h) {
		var url = (obj.getAttribute)? obj.getAttribute('href') : obj.href;
		if(!url) return true; // if href is not given, no popup will be opened
		var w = (w === undefined)? 710 : w;
		var h = (h === undefined)? 570 : h;
		var pop = window.open(url, '', 'width='+w+', height='+h+', resizable=yes, scrollbars=yes');
		if (pop) {
			if (pop.focus) pop.focus();
			return false; // prevents link to open in parent window
		} else {
			return true; // if popup could not be opened link opens in original window
		}
	}
}

function highlighter(elementlist, colorstart, colorend){
	var curLen = elementlist.length;
	for(i = 0; i < curLen; i++){

		var element = $(elementlist[i][0]);
		var startC = elementlist[i][1];
		var endC = elementlist[i][2];
		var cssClassen = element.className;
		cssClassen = cssClassen + ' error';
		element.className = cssClassen;
		//new Effect.Highlight(elementlist[i][0], {startcolor:startC, endcolor:endC});

	}
}

//Function for checking if size of the product was selected on ProductAddToBasket page
function checkSelectedSize(errorMessage)
{
	var size = document.forms.frmAddToBasket.size.value;
	if (size > 0) {
		document.forms.frmAddToBasket.submit();
	}else {
		alert(errorMessage);
	}
}

// submit search from page header
function submitSearch(url) {
	var query = $('query').value;
	document.location.href = url + 'query/' + query;
}

// load captcha image
function loadFormCaptcha(url, divId, session) {
	$(divId).innerHTML = '';
	new Ajax.Updater(divId, url + 'Ajax/Captcha/index/?PHPSESSID=' + session, {evalScripts:true, asynchronous:true});
	return false;
}

// refresh the mini basket in page header
function refreshMiniBasket(siteUrl) {
	new Ajax.Updater('header_span', siteUrl + 'Ajax/Basket/refreshMiniBasket/', {evalScripts:true, asynchronous:true});
	return false;
}

// simple paginator ajax wrapper
function paginator(divId, targetURL) {
	new Ajax.Updater(divId, targetURL, {evalScripts:true, asynchronous:true});
	return false;
}

// home page AB testes script to switch styles for 2 buttons at one time

function homepageContentTest(homepageAction) {

	  switch (homepageAction) {

		   case 1:
			$('open-your-own-shop-image').style.background = 'url(http://www.spreadshirt.net/Public/Common/images/ab/button1_hover_uk.jpg) no-repeat';
			$('open-your-own-shop-button').style.color = '#FFFAC8';
			break;

		   case 2:
			$('open-your-own-shop-image').style.background = 'url(http://www.spreadshirt.net/Public/Common/images/ab/button1_link_uk.jpg) no-repeat';
			$('open-your-own-shop-button').style.color = '#FFFFFF';
			break;

		   case 3:
			$('create-custom-product-image').style.background = 'url(http://www.spreadshirt.net/Public/Common/images/ab/button2_hover_uk.jpg) no-repeat';
			$('create-custom-product-button').style.color = '#FFFAC8';
			break;

		   case 4:
			$('create-custom-product-image').style.background = 'url(http://www.spreadshirt.net/Public/Common/images/ab/button2_link_uk.jpg) no-repeat';
			$('create-custom-product-button').style.color = '#FFFFFF';
			break;

		  case 5:
			$('create-custom-product-image-us').style.background = 'url(http://www.spreadshirt.com/Public/Common/images/ab/button1_hover.gif) no-repeat';
			$('create-custom-product-button-us').style.color = '#FFFAC8';
			break;

		   case 6:
			$('create-custom-product-image-us').style.background = 'url(http://www.spreadshirt.com/Public/Common/images/ab/button1_link.gif) no-repeat';
			$('create-custom-product-button-us').style.color = '#FFFFFF';
			break;

		   case 7:
			$('open-your-own-shop-image-us').style.background = 'url(http://www.spreadshirt.com/Public/Common/images/ab/button2_hover.gif) no-repeat';
			$('open-your-own-shop-button-us').style.color = '#FFFAC8';
			break;

		   case 8:
			$('open-your-own-shop-image-us').style.background = 'url(http://www.spreadshirt.com/Public/Common/images/ab/button2_link.gif) no-repeat';
			$('open-your-own-shop-button-us').style.color = '#FFFFFF';
			break;

			case 9:
			$('create-custom-product-image-us').style.background = 'url(http://www.spreadshirt.com/Public/Common/images/ab/button_hal_1_hover.gif) no-repeat';
			$('create-custom-product-button-us').style.color = '#FFFAC8';
			break;

		   case 10:
			$('create-custom-product-image-us').style.background = 'url(http://www.spreadshirt.com/Public/Common/images/ab/button1_hal_link.gif) no-repeat';
			$('create-custom-product-button-us').style.color = '#FFFFFF';
			break;

		   case 11:
			$('open-your-own-shop-image-us').style.background = 'url(http://www.spreadshirt.com/Public/Common/images/ab/button2_hal_hover.gif) no-repeat';
			$('open-your-own-shop-button-us').style.color = '#FFFAC8';
			break;

		   case 12:
			$('open-your-own-shop-image-us').style.background = 'url(http://www.spreadshirt.com/Public/Common/images/ab/button2_hal_link.gif) no-repeat';
			$('open-your-own-shop-button-us').style.color = '#FFFFFF';
			break;
		  }


}

try {
	Event.observe(window,'load',initTabs); // init tab-navi fpr switching content
	Event.observe(window,'load',initIbox); // init ibox for image-zoom and external content
	Event.observe(window,'load',submitToLink); // init the input-button-replacement
	Event.observe(window,'load',initTitles); // init tooltips
} catch(error) {}
