$(document).ready(function(){

	/* SEARCH FORM */
	$("#site_search span.go").click(function(){
    	$("#siteSearchForm").submit();
	});

	/* MENU BUTTONS */
    $("#menu > ul > li > a, #bottom_menu ul li a").each(function(){
    	t = $(this).html();
		if(t){
			$(this).html("<img class=\"buttonImg\" src=\"/domains/thechopperdome-gent.com/generate/button.php?txt=" + t + "\" border=\"0\" />");
	
			//hover
			$("#menu img.buttonImg, #bottom_menu img.buttonImg").parent('a').hover(
				function(){
					if(!$(this).children('.buttonImg').attr('origSrc'))
						$(this).children('.buttonImg').attr('origSrc',$(this).children('.buttonImg:eq(0)').attr('src'));
	
					s = $(this).children('.buttonImg').attr('origSrc');
					$(this).children('.buttonImg').attr('src',s + '&over=1');
				},
				function(){
					s = $(this).children('.buttonImg').attr('origSrc');
					$(this).children('.buttonImg').attr('src',s);
				}
			);
		}
    });

	/* ENABLE SUBMENU */
    $("#menu ul li").hover(
		function(){
			if($(this).children('ul:eq(0)').is(':hidden'))
				$(this).children('ul:eq(0)').css('display','block');
		},
		function(){
			if($(this).children('ul:eq(0)').is(':visible'))
				$(this).children('ul:eq(0)').css('display','none');
		}
    );
	//change some html
 	$("#menu ul ul li:last-child, #menu ul ul li:last-child > a").addClass("last");
 	$("#menu ul ul li:first-child, #menu ul ul li:first-child > a").addClass("first");
	$("#menu ul ul").wrapInner("<div class=\"ulTop clear\"><div class=\"ulBack clear\"></div></div>");


	/* ANIM FLAGS */
	var lang = new Array();
	lang['lang_0'] = 'nl';
	lang['lang_1'] = 'uk';
	lang['lang_2'] = 'fr';
	lang['lang_3'] = 'de';
	$("#header_overlay .lang a").hover(
		function(){
			$(this).css('background-image',"url(/domains/thechopperdome-gent.com/images/flags/" + lang[$(this).attr('class')] + "_a.gif)");
		},
		function(){
			$(this).css('background-image',"url(/domains/thechopperdome-gent.com/images/flags/" + lang[$(this).attr('class')] + "_o.gif)");
		}
	);

	/* TITLES */
    $(".title h1.title").each(function(){
		if($(this).children('a').length){
	    	t = $(this).children('a:first').html();
			if(t)
				$(this).children('a:first').html("<img src=\"/domains/thechopperdome-gent.com/generate/title.php?txt=" + t + "\" alt=\"" + t + "\" border=\"0\" />");
		}else{
	    	t = $(this).html();
			if(t)
				$(this).html("<img src=\"/domains/thechopperdome-gent.com/generate/title.php?txt=" + t + "\" alt=\"" + t + "\" border=\"0\" />");
		}
    });

	//change TRADE shop menu
	$("div.trademenu_module ul.trade_categories li a, div.trademenu_module ul.trade_manufacturers li a").click(function(){
		//check for subs...
		if($(this).parent().children("ul:first").html()){
			$(this).parent().children("ul:first").show('slow');
		}else{
			//do click
			h = '/com_trade/?' + $(this).attr('href').substr(21).replace(/\//g,'&');
			self.location.href='/index.php?module=trade&href=' + escape(h);
		}
		return false;
	});
	//try to load the TRADE shop...
	h = $("div.trade_module div.gui a.get").attr('target');
	if(h != 'undefined' && h){
		$("#middle div.trade_module div.gui").load(h + " div#body",changeContent);
	}

	function changeContent(){
		//change links...
		$("#middle div.trade_module div.gui a[href^='http://eshop.fickser.nl/index.php']").each(function(){
			h = '/com_trade/?' + $("#middle div.trade_module div.gui a[href*='option=']:first").attr('href').split('?')[1] + $(this).attr('href').split('?')[1];
			$(this).attr('href','/index.php?module=trade&href=' + escape(h));
		});
		$("#middle div.trade_module div.gui a[href*='option=']").each(function(){
			h = '/com_trade/?' + $(this).attr('href').split('?')[1];
			$(this).attr('href','/index.php?module=trade&href=' + escape(h));
		});

		//hide stupid <td> bar
		$("td[bgcolor='#ffffff']").css('display','none');

		//change continue buttons
		$("input[src$='button_continue.gif']").css('display','none');
		$('<input type="submit" value="continue" class="submit" />').insertBefore("input[src$='button_continue.gif']");

		changeForms();
	}

	function changeForms(){
		$("div#body form").each(function(){
			h = '/com_trade/?' + $(this).attr('action').split('?')[1];

			$(this).attr("action",h).attr('onsubmit','').submit(function(){
				var options = {
			//		target:        '#middle div.trade_module div.gui',   // target element(s) to be updated with server response
					//beforeSubmit:  showRequest,  // pre-submit callback
					success:       showResponse,  // post-submit callback

					// other available options:
					//url:       url         // override for form's 'action' attribute
					//type:      type        // 'get' or 'post', override for form's 'method' attribute
					dataType:  'html'        // 'xml', 'script', or 'json' (expected server response type)
					//clearForm: true        // clear all form fields after successful submit
					//resetForm: true        // reset the form after successful submit

					// $.ajax options can be used here too, for example:
					//timeout:   3000
				};
				$(this).ajaxSubmit(options);

//				$("#middle div.trade_module div.gui").load($(this).attr("action") + " div#body",changeContent);
				return false;
			});
		});
	}

	// post-submit callback
	function showResponse(responseText, statusText)  {
		// for normal html responses, the first argument to the success callback
		// is the XMLHttpRequest object's responseText property

		// if the ajaxSubmit method was passed an Options Object with the dataType
		// property set to 'xml' then the first argument to the success callback
		// is the XMLHttpRequest object's responseXML property

		// if the ajaxSubmit method was passed an Options Object with the dataType
		// property set to 'json' then the first argument to the success callback
		// is the json data object returned by the server

	//	alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +
	//		'\n\nThe output div should have already been updated with the responseText.');

		//kill unwanted response shit
		responseText = responseText.replace("window.history.go(-1);","");

		responseText = responseText.replace(/urchinTracker\(\)\;/g,"");
//		responseText = responseText.replace("http://www.google-analytics.com/urchin.js","");
		//responseText = responseText.replace("document.getElemebtById(\"internettime\")","top.document.getElemebtById(\"internettime\")");

		//inject response
		if(!document.getElementById('internettime'))
			$("<div id=\"internettime\" style=\"display:none;\"></div>").insertBefore("#middle div.trade_module div.gui");
		o = $("<div id=\"formPageContent\"></div>").html(responseText).find("div#body");
		h = o.html();
		if(o.text().replace(/^\s*|\s*$/g,""))
			$("#middle div.trade_module div.gui").html(h);

		changeContent();
	}

	//change the stupid HR behavour of IE...
	if($.browser.msie){
		$("#content hr").wrap("<div class=\"hr\"></div>");
		$("#content hr").css('display','none');
	}
});
