function init() {

	function dbg(msg){
		var old = $('#debug').html();
		var cur = msg + '<br />' + old;
		$('#debug').html(cur);
	}

	$(document).keypress(function(e){
		if((e.which == '100')|(e.which == '68')|(e.which == '948')|(e.which == '916')){
			if (dbgState) {
				var msg = 'debug off';
				dbg(msg);
//				$('#debug').hide();
			} else {
				var msg = 'debug on';
//				$('#debug').show();
				dbg(msg);
			}
			
			dbgState = !dbgState;
		}else if(e.which == '13'){
			goSearch();
		}else{
			dbg('key'+e.which);
		}
	});
	
	$('#searchbutton').click(function(){
		goSearch();
	});
	
	function goSearch(){ // TODO merge with hSearch()
		var query = $('input').val();
		hSearch(query);
	}
	
	$('#facebook').click(function(){
		_gaq.push(['_trackEvent', 'Facebook', 'Visit']);
		fblink = "http://www.facebook.com/pages/PENTEL/90068595636";
		window.open(fblink);
	});
	
	$('.menuheader').click(function(){
		var $clickedContent = $('#'+$(this).parent().attr('id')+' > .menucontent');
		var notCurrent = $clickedContent.css('display') == 'none';

		if(notCurrent){
// OLD			$('.menuselection').css('background-color', '#2276bc');
/*			$('.menuselection').css('background-position', '0px 0px');
			$('.menucontent').slideUp();
			$('.menuselection').removeClass('menuselection');
*/			$clickedContent.slideDown();
			$(this).addClass('menuselection');
//			$(this).css('background-color', '#2276ee');
			$(this).css('background-position', '0px -52px');
		}else{
			$(this).css('background-position', '0px 0px');
			$(this).removeClass('menuselection');
			$clickedContent.slideUp();
		}
	});

	$('body').delegate('#navhome','click',function(){
		window.location = "#!home";
	});

	$('body').delegate('.manuf, .mainmanuf','click',function(){
		var param0 = $(this).html();
		var param = param0.replace(/ /g,'%20');
		if(selManuf != param){
			var loadStr = 'includes/getcategories.php?manuf=' + param;

			dbg('\''+param0+'\'');

			$('#rightcol').load(loadStr, function(response, status, xhr){
				if(status == "error"){
					var msg = "Sorry but there was an error: ";
					dbg(msg + xhr.status + " " + xhr.statusText);
				}else{
					var navbar = "<div id='navhome' class='navitem'></div><div class='navitem navdivider'></div><div class='navitem navtext'>" + param0 + "</div>\n";
					$('#navbar').html(navbar);
					selHome = false;
					selManuf = param0;
					selCateg = null;
				}
				enableHover();
			});
		}
	});
	
	$('body').delegate('.cats,.maincategory,.manufcategory','click',function(){
		var prmname = $(this).html();
		var param0 = this.id;
		param = param0.substring(3);
//		var param = param0.replace(/ /g,'%20');
//		var param = param.replace(/&amp;/g,'%26');
//		var loadStr = 'includes/getproductscat.php?cat=' + param;
		var loadStr = 'includes/getprodlist.php?opt=cat&val=' + param;
		var showManuf = $(this).hasClass('manufcategory');
		dbg(param0);

		$('#rightcol').load(loadStr, function(response, status, xhr){
			if(status == "error"){
				var msg = "Sorry but there was an error: ";
				dbg(msg + xhr.status + " " + xhr.statusText);
			}else{
				var navbar = "<div id='navhome' class='navitem'></div>";
				if(showManuf){
					navbar = navbar + "<div class='navitem navdivider'></div><div class='navitem navtext navhover manuf'>" + selManuf + "</div>";
				}
				navbar = navbar + "<div class='navitem navdivider'></div><div class='navitem navtext'>" + prmname + "</div>\n";
				$('#navbar').html(navbar);
				selHome = false;
				selManuf = null;
				selCateg = param0;
			}
			enableHover();
		});
	});
	
	$('body').delegate('.manufseries','click',function(){
		var prmname = $(this).html();
		var param0 = this.id;
		param = param0.substring(3);
//		var param = param0.replace(/ /g,'%20');
//		var param = param.replace(/&amp;/g,'%26');
//		var loadStr = 'includes/getproductsser.php?ser=' + param;
		var loadStr = 'includes/getprodlist.php?opt=ser&val=' + param;
		var showManuf = $(this).hasClass('manufseries');
		dbg(param0);

		$('#rightcol').load(loadStr, function(response, status, xhr){
			if(status == "error"){
				var msg = "Sorry but there was an error: ";
				dbg(msg + xhr.status + " " + xhr.statusText);
			}else{
				var navbar = "<div id='navhome' class='navitem'></div>";
				if(showManuf){
					navbar = navbar + "<div class='navitem navdivider'></div><div class='navitem navtext navhover manuf'>" + selManuf + "</div>";
				}
				navbar = navbar + "<div class='navitem navdivider'></div><div class='navitem navtext'>" + prmname + "</div>\n";
				$('#navbar').html(navbar);
				selHome = false;
				selManuf = null;
				selCateg = param0;
			}
			enableHover();
		});
	});
	
	$(document).delegate('.prodthb','click',function(){
		var id = $(this).parent().parent().attr('id');
		loadProdInfo(id);
	});

	$(document).delegate('.prodmore','click',function(){
		var id = $(this).parent().attr('id');
		loadProdInfo(id);
	});

	$(document).delegate('.dloadthb a','click',function(e){
		e.preventDefault();
	});
	
	$(document).delegate('.dloadthb','click',function(e){
		e.stopPropagation();
		var pic = $(this).find('a').attr('href');
		loadPreview1(pic);
	});

/*	$('#mnew').click(function(){
		var loadStr = 'includes/getprodlist.php?opt=new';

		$('#rightcol').load(loadStr, function(response, status, xhr){
			if(status == "error"){
				var msg = "Sorry but there was an error: ";
				dbg(msg + xhr.status + " " + xhr.statusText);
			}else{
				var navbar = "<div id='navhome' class='navitem'></div>";
//				navbar = navbar + "<div class='navitem navdivider'></div><div class='navitem navtext'>Νέες αφίξεις</div>\n";
				$('#navbar').html(navbar);
//				selHome = false;
				selHome = true;
				selManuf = null;
				selCateg = null;
			}
			enableHover();
			window.scrollTo(0,0);
		});
	});
*/
/*	$('#mproducts').click(function(){
		var loadStr = 'includes/getmainproductview.php';

		$('#rightcol').load(loadStr, function(response, status, xhr){
			if(status == "error"){
				var msg = "Sorry but there was an error: ";
				dbg(msg + xhr.status + " " + xhr.statusText);
			}else{
				var navbar = "<div id='navhome' class='navitem'></div>";
				navbar = navbar + "<div class='navitem navdivider'></div><div class='navitem navtext'>Προϊόντα</div>\n";
				$('#navbar').html(navbar);
				selHome = false;
				selManuf = null;
				selCateg = null;
			}
			enableHover();
			window.scrollTo(0,0);
		});
	});
*/	
/*	$('#mcompany').click(function(){
		var loadStr = 'includes/getcompany.php';

		$('#rightcol').load(loadStr, function(response, status, xhr){
			if(status == "error"){
				var msg = "Sorry but there was an error: ";
				dbg(msg + xhr.status + " " + xhr.statusText);
			}else{
				var navbar = "<div id='navhome' class='navitem'></div>";
				navbar = navbar + "<div class='navitem navdivider'></div><div class='navitem navtext'>Εταιρεία</div>\n";
				$('#navbar').html(navbar);
				selHome = false;
				selManuf = null;
				selCateg = null;
			}
			enableHover();
			window.scrollTo(0,0);
		});
	});
*/	
/*	$('#mdownloads').click(function(){
		var loadStr = 'includes/getdownloads.php';

		$('#rightcol').load(loadStr, function(response, status, xhr){
			if(status == "error"){
				var msg = "Sorry but there was an error: ";
				dbg(msg + xhr.status + " " + xhr.statusText);
			}else{
				var navbar = "<div id='navhome' class='navitem'></div>";
				navbar = navbar + "<div class='navitem navdivider'></div><div class='navitem navtext'>Downloads</div>\n";
				$('#navbar').html(navbar);
				selHome = false;
				selManuf = null;
				selCateg = null;
			}
			enableHover();
			window.scrollTo(0,0);
		});
	});
*/	
/*	$('#mcontact,.contacttext').click(function(){
		var loadStr = 'includes/getcontact.php';

		$('#rightcol').load(loadStr, function(response, status, xhr){
			if(status == "error"){
				var msg = "Sorry but there was an error: ";
				dbg(msg + xhr.status + " " + xhr.statusText);
			}else{
				var navbar = "<div id='navhome' class='navitem'></div>";
				navbar = navbar + "<div class='navitem navdivider'></div><div class='navitem navtext'>Επικοινωνία</div>\n";
				$('#navbar').html(navbar);
				selHome = false;
				selManuf = null;
				selCateg = null;
			}
			enableHover();
			window.scrollTo(0,0);
		});
	});
*/	
/*	$('#mlinks').click(function(){
		var loadStr = 'includes/getlinks.php';

		$('#rightcol').load(loadStr, function(response, status, xhr){
			if(status == "error"){
				var msg = "Sorry but there was an error: ";
				dbg(msg + xhr.status + " " + xhr.statusText);
			}else{
				var navbar = "<div id='navhome' class='navitem'></div>";
				navbar = navbar + "<div class='navitem navdivider'></div><div class='navitem navtext'>Links</div>\n";
				$('#navbar').html(navbar);
				selHome = false;
				selManuf = null;
				selCateg = null;
			}
			enableHover();
			window.scrollTo(0,0);
		});
	});
*/
	$('#prodcont').click(function(e){
		e.stopPropagation();
	});

	$('#prodbg').click(function(){
		$('#prodcont').hide();
		$('#prevcont').hide();
		$('#prodbg').hide();
		$('#prodcont').undelegate('#email','click');
		$('#prodcont').undelegate('#print','click');
	});

	$('#prodcont').delegate('#close','click',function(){
		$('#prodbg').click();
	});
	
	$('.menuitem').hover(
		function(){
//			$(this).css('background-image', 'url(img/arrow.gif)');
		},
		function(){
//			$(this).css('background-image', '');
		}
	);
	
	$('.menuitem').click(function(e){
		dbg('menu click');
		if(msie){
			e.preventDefault();
			var newAnchor = $(this).parent().attr('href');
			dbg(newAnchor);
			location.href = newAnchor;
			$(window).trigger('hashchange');
		}
	});

	$('.menuheader').hover(
		function(){
			if($(this).hasClass('menuselection')){
			}else{
//				$(this).css('background-color', '#2276ee');
				$(this).css('background-position', '0px -26px');
			}
		}, 
		function(){
			if($(this).hasClass('menuselection')){
			}else{
//				$(this).css('background-color', '#2276bc');
				$(this).css('background-position', '0px 0px');
			}
		}
	);

	function enableHover(){
		$('.prodsq').hover(
			function(){
//				$(this).css('background-color', '#d1e0ec');
				$(this).addClass('prodsqh');
			},
			function(){
//				$(this).css('background-color', 'transparent');
				$(this).removeClass('prodsqh');
			}
		);
		
		$('li, .prodmore').hover(
			function(){
				$(this).css('color', '#deecf0');
				$(this).css('font-weight', '900');
			}, 
			function(){
				$(this).css('color', '#03121a');
				$(this).css('font-weight', 'normal');
			}
		);

		$('.navhover').hover(
			function(){
				$(this).css('color', '#2276bc');
			}, 
			function(){
				$(this).css('color', '#666');
			}
		);

		$('#navhome').hover(
			function(){
				if(!selHome){
					$(this).css('background-position', '0px -16px');
				}
			},
			function(){
				$(this).css('background-position', '0px 0px');
			}
		);
	}
/*	
	$('#facebook, #twitter, #youtube').hover(
		function(){
			$(this).css('background-position', '0px -32px');
		},
		function(){
			$(this).css('background-position', '0px 0px');
		}
	);
*/
	function loadPreview(pic){
		dbg('preview... '+pic);
		var loadStr = '<img src=\''+pic+'\' >\n';

		$('#prodcont').html(loadStr);
		dbg('load successful!');
		showProdInfo();
	}

	function loadPreview1(pic){
		dbg('preview... '+pic);
		var loadStr = '<img src=\''+pic+'\' >\n';

		$('#prevcont').html(loadStr);
		dbg('load successful!');
		showProdInfo1();
	}

	function loadProdInfo(pid){
		dbg('loading... '+pid);
		var loadStr = 'includes/getprodinfo.php?id=' + pid; // + ' #elementid';

		$('#prodcont').load(loadStr, function(response, status, xhr) {
			if (status == "error") {
				var msg = "Sorry but there was an error: ";
				dbg(msg + xhr.status + " " + xhr.statusText);
			} else {
				dbg('load successful!');
				showProdInfo();
				$('#prodcont').delegate('#print','click',function(){
					printonce();
				});	
				$('#prodcont').delegate('#email','click',function(){
//					sendmail(name, mail, text);
				});
				$('.prodimgcenter').click(function(e){
					e.stopPropagation();
					var imgSrc = $('.prodimgcenter img').attr('src');
					imgSrc = imgSrc.match(/img\/.*/)[0].substring(4);
					var htmlImg = '<img src=\'img/maxi/' + imgSrc + '\'/>';
					$('#prodimgmax').html(htmlImg);
					$('#prodimgmax').show();
					$('#prodbgmax').show();
					$('#prodbgmax').click(function(e){
						e.stopPropagation();
						$(this).hide();
						$('#prodimgmax').hide();
					});
				});
				$('#zoom').click(function(e){
					$('.prodimgcenter').click();
				});
				$('.thbimg').click(function(e){
					var thbcur = $(this).index('.thbimg') + 1;
					var newimg = $(this).find('img').attr('src');
					newimg = newimg.replace('/mini/', '/');
					$('.prodimgcenter img').attr('src', newimg);
					$('#thbcount').html(thbcur);
				});
			}
		});
	}

	function showProdInfo(){
		$('#prodbg').show();
		$('#prodcont').show();
		
		$(document).keyup(function(e){
			if(e.which == 27){
				$('#prodbg').click();
			}
		});
	}

	function showProdInfo1(){
		$('#prodbg').show();
		$('#prevcont').show();
		
		$(document).keyup(function(e){
			if(e.which == 27){
				$('#prodbg').click();
			}
		});
	}

	//if submit button is clicked
	$('#submit').click(function () {		
		
		//Get the data from all the fields
		var name = $('input[name=name]');
		var email = $('input[name=email]');
		var website = $('input[name=website]');
		var comment = $('textarea[name=comment]');

		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		if (name.val()=='') {
			name.addClass('hightlight');
			return false;
		} else name.removeClass('hightlight');
		
		if (email.val()=='') {
			email.addClass('hightlight');
			return false;
		} else email.removeClass('hightlight');
		
		if (comment.val()=='') {
			comment.addClass('hightlight');
			return false;
		} else comment.removeClass('hightlight');
		
		//organize the data properly
		var data = 'name=' + name.val() + '&email=' + email.val() + '&website=' + 
		website.val() + '&comment='  + encodeURIComponent(comment.val());
		
		//disabled all the text fields
		$('.text').attr('disabled','true');
		
		//show the loading sign
		$('.loading').show();
		
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "process.php",	
			
			//GET method is used
			type: "GET",

			//pass the data			
			data: data,		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (html) {				
				//if process.php returned 1/true (send mail success)
				if (html==1) {					
					//hide the form
					$('.form').fadeOut('slow');					
					
					//show the success message
					$('.done').fadeIn('slow');
					
				//if process.php returned 0/false (send mail failed)
				} else alert('Sorry, unexpected error. Please try again later.');				
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});	

	function sendmail(name, mail, text){
		var valid = '';
		var isr = ' is required.';
		var subject = "PACO ΕΛΛΑΣ";
		if (name.length<1) {
//			valid += '<br />Name'+isr;
		}
		if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
//			valid += '<br />A valid Email'+isr;
		}
		if (subject.length<1) {
//			valid += '<br />Subject'+isr;
		}
		if (text.length<1) {
//			valid += '<br />Text'+isr;
		}
		if (valid!='') {
//			$("#response").fadeIn("slow");
//			$("#response").html("Error:"+valid);
		}
		else {
			var datastr ='name=' + name + '&mail=' + mail + '&subject=' + subject + '&text=' + text;
//			$("#response").css("display", "block");
//			$("#response").html("Sending message .... ");
//			$("#response").fadeIn("slow");
			setTimeout("send('"+datastr+"')",2000);
		}
		return false;
	};

	function send(datastr){
		$.ajax({	
			type: "POST",
			url: "includes/mail.php",
			data: datastr,
			cache: false,
			success: function(html){
//			$("#response").fadeIn("slow");
//			$("#response").html(html);
//			setTimeout('$("#response").fadeOut("slow")',2000);
			dbg("mail sent!");
		}
		});
	}
	
	function printonce(){
		$('#prodcont').print();
	}
	
	function enableHash(){
		$(window).bind('hashchange', function(e){
			dbg('hashchange');
			var newhash = location.hash;
			var hashbang = newhash.substring(0,2);
			var hashmain = newhash.substring(2);
			
			if(hashbang == '#!'){
				var fragment = hashmain.split(/\//);
				switch(fragment[0]){
					case 'home':
						hHome();
						break;
					case 'contact':
						hContact();
						break;
					case 'links':
						hLinks();
						break;
					case 'downloads':
						hDownloads();
						break;
					case 'products':
						hProducts();
						break;
					case 'company':
						hCompany();
						break;
					case 'search':
						hSearch(fragment[1]);
						break;
					default:
						window.location = "#!home";
						break;
				}
			}
		})
	}

	function hHome(){
		var hashLocation = 'includes/getprodlist.php?opt=new';
		selHome = true;
		loadHash(hashLocation, '');
	}
	
	function hContact(){
		var hashLocation = 'includes/getcontact.php';
		selHome = false;
		loadHash(hashLocation, '');
	}
	
	function hLinks(){
		var hashLocation = 'includes/getlinks.php';
		selHome = false;
		loadHash(hashLocation, '');
	}
	
	function hDownloads(){
		var hashLocation = 'includes/getdownloads.php';
		selHome = false;
		loadHash(hashLocation, '');
	}
	
	function hProducts(){
		var hashLocation = 'includes/getmainproductview.php';
		selHome = false;
		loadHash(hashLocation, '');
	}
	
	function hCompany(){
		var hashLocation = 'includes/getcompany.php';
		selHome = false;
		loadHash(hashLocation, '');
	}
	
	function hSearch(query){
		var param0 = query;
		selHome = false;
		if(param0 != ''){
			var param = param0.replace(/ /g,'%20');
			var param = param.replace(/&amp;/g,'%26');
			var hashLocation = 'includes/getprodlist.php?opt=src&val=';
			loadHash(hashLocation, param);
			window.location = "#!search/"+param;
		}
	}
	
	function loadHash(hashLocation, param){
			var loadStr = hashLocation+param;
			dbg('loadHash: '+loadStr);

			$('#topmessage').hide();
			$('#topmessage').prependTo('body');
			
			$('#rightcol').load(loadStr, function(response, status, xhr){
				if(status == "error"){
					var msg = "Sorry but there was an error: ";
					dbg(msg + xhr.status + " " + xhr.statusText);
				}else{
					if(selHome){
						$('#topmessage').prependTo('#rightcol');
						$('#topmessage').show();
					}
					loadHome();
				}
				enableHover();
			});
	}

	function loadHome(){
		var navbar = "<div id='navhome' class='navitem'></div>";
		$('#navbar').html(navbar);
		selHome = true;
		selManuf = null;
		selCateg = null;
	}

	var msie = false;
	var dbgState = false;
//	$('#debug').show();
	dbg('init');
	if($.browser.msie){
		msie = true;
		dbg('damn! on IE :-(');
	}
	$('input').val('');
	
	var selManuf = null;
	var selCateg = null;
	var selHome = true;
	$('#menumanuf > .menuheader').click();
	$('#menucategory > .menuheader').click();
	enableHover();
	enableHash();
	hHome();
	$.get('includes/resizeimages.php', function(data){
		dbg("resize images" + data);
	});
}

