$(document).ready(function() {
	
	checkHeight();
	function checkHeight() {
		var height = parseInt($(window).height()) -70;
		if($("div#content").height() > height) {
			$("div#content").css('overflow-y', 'scroll');
			$("div#content").css('height', height);	// 50 = yläkuva, 20 = scrollbarin nuolinappi
		}
		$("div#leftcontent").css('height', height+10);
	}

	// 26.03.2010, Tomi
	$("input.toggleConversionNumber").click(function() {
			var user_id = $(this).attr('id');
			if($(this).val() == '1') {
				var value = 0;
			}
			else {
				var value = 1;
			}
			
			$.get('ajax.php', {'act': 'toggleConversionNumber', 'user_id': user_id, 'value': value},
				function(returned_data) {
					if(returned_data) {
						//$("input#"+user_id).parent('td').css('background-color','#0c0');
						$("input#"+user_id).val(value);
					}
					else {
						alert('Vaihtonumeronäkyvyyden asetus ei onnistunut.');
					}
				}
			);
		}
	);

	// 25.05.2010, Tomi
	$("input.toggleConversionNumberAdmin").click(function() {
			var user_id_raw = $(this).attr('id');
			var user_id = $(this).attr('id').split('_')[0];
			if($(this).val() == '1') {
				var value = 0;
			}
			else {
				var value = 1;
			}
			
			$.get('ajax.php', {'act': 'toggleConversionNumberAdmin', 'user_id': user_id, 'value': value},
				function(returned_data) {
					if(returned_data) {
						$("input#"+user_id_raw).val(value);
					}
					else {
						alert('Vaihtonumerohallinnan asetus ei onnistunut.');
					}
				}
			);
		}
	);
	$('div#newsImages').cycle({ 
		fx: 'fade', 
		speed: 1000,
		timeout: 5000
	});
	
	$("div#logos").cycle({
		fx: 'fade',
		speed: 1000,
		timeout: 8000
	});

	$('a[rel=lightbox]').lightBox();
	
	
	/* <Product search> */
	$("select#search_productline").click(function() {
		// 17.01.2011, Tomi -- Loader näkyviin
		$("div#category").append('<div id="cat_loader"><img src="/images/ajax-loader.gif" style="width:16px;height:16px;" /></div>');
		// ---
		//$("div#category").hide();
		$("select#search_category").hide();
		$("div#brand").hide();
		$("div#model").hide();
		$("div#searchButton").hide();

		var productline = document.getElementById('search_productline').value;
		if(productline != '') {
			$.get('ajax.php', {act: 'getMenuLevel', parent: productline},
				function(returned_data) {
					// if data is returned
					if(returned_data.length > 1) {
						// 17.01.2011, Tomi -- Remove loader
						$("div#cat_loader").remove();
						// ---
						$("select#search_category").show();
						
						// enable category -field because there's data to be input
						document.getElementById('search_category').disabled = false;
						// 10.01.2011, Tomi
						$("select#search_category").html(returned_data);
						$("div#category").show();
					}
				}
			);
		}
	});

	$("select#search_category").click(function() {
		var category = document.getElementById('search_category').value;
		// 17.01.2011, Tomi -- Loader näkyviin
		if(category != '') {
			$("div#brand").append('<div id="brand_loader"><img src="/images/ajax-loader.gif" style="width:16px;height:16px;" /></div>');
			$("div#brand").show();
		}
		else {
			$("div#brand").hide();
		}
		$("select#search_brand").hide();
		$("select#search_brand").html('');
		$("select#search_model").html('');
		$("div#model").hide();
		$("div#searchButton").hide();

		// Tarkistetaan onko alakategorioita
		$.get('ajax.php', {'act': 'getSubCategories', 'menu_id': category},
			function(returned_data) {
				if(returned_data.length > 1) {
					$("div#brand_loader").remove();
					$("select#search_brand").html(returned_data);
					$("select#search_brand").show();
					$("div#model").show();
					$("input#brand_db").val('1');
				}
				else {
					// Ei löydy alakategorioita -> Katsotaan löytyykö merkkejä
					$.get('ajax.php', {'act': 'getBrands', 'menu_id': category},
						function(returned_data) {
							if(returned_data.length > 1) {
								//alert('brands');
								// Merkkejä löytyy, tulostetaan ne
								$("div#brand_loader").remove();
								$("select#search_brand").html(returned_data);
								$("select#search_brand").show();
								$("div#brand").show();
							}
							else {
								// Merkkejä ei löytynyt -> Näytetään hakunappi ja piilotetaan merkki
								$("div#brand").hide();
								$("select#search_brand").hide();
								$("div#searchButton").show();
							}
						}
					);
					$("input#brand_db").val('0');
				}
			}
		);
	});

	$("select#search_brand").click(function() {
		$("select#search_model").html('');
		var brand = document.getElementById('search_brand').value;
		var menu_id = $("#search_category").val();
		
		
		// Tarkistetaan onko alakategorioita
		$.get('ajax.php', {'act': 'getSubCategories', 'menu_id': brand},	// 17.08.2011, Tomi -- menu_id oli ennen menu_id, mutta tässä haetaan merkin alakategorioita
			function(returned_data) {
				if(returned_data.length > 1) {
					// Löytyy, haetaan ne
					$.get('ajax.php', {act: 'getMenuLevel', 'parent': brand},
						function(returned_data) {
							if(returned_data != '') {
								$("div#model_loader").remove();
								$("select#search_model").html(returned_data);
								$("select#search_model").show();
								$("div#model").show();
							}
						}
					);
					$("input#model_db").val('1');
				}
				else {
					// Ei löydy alakategorioita -> Katsotaan löytyykö malleja
					if(parseInt($("input#brand_db").val()) == 1) {
						$("div#model").hide();
						$("select#search_model").html('');
						$("div#searchButton").show();
						
						/*
						$.get('ajax.php', {'act': 'getSubCategoryModels', 'brand': brand},
							function(returned_data) {
								alert('mallit: '+returned_data);
								if(returned_data != '') {
									// Malleja löytyy, tulostetaan ne
									$("div#model_loader").remove();
									$("select#search_model").html(returned_data);
									$("select#search_model").show();
									$("div#model").show();
								}
								else {
									// Malleja ei löytynyt -> Näytetään hakunappi
									$("select#search_model").html('');
									$("div#searchButton").show();
								}
							}
						);
						*/
					}
					else {
						$.get('ajax.php', {'act': 'getModels', 'brand': brand, 'menu_id': menu_id},
							function(returned_data) {
								if(returned_data.length > 1) {
									// Malleja löytyy, tulostetaan ne
									$("div#model_loader").remove();
									$("select#search_model").html(returned_data);
									$("select#search_model").show();
									$("div#model").show();
								}
								else {
									// Malleja ei löytynyt -> Näytetään hakunappi
									$("select#search_model").html('');
									$("div#searchButton").show();
								}
							}
						);
					}
					$("input#model_db").val('0');
				}
			}
		);
		
	});

	$("select#search_model").click(function() {
		$("div#searchButton").show();
	});
	/* </Product search> */

	
	/* <Modelsearch> */
	$("select#msearch_brand").change(function() {
		$("div#modelsearch_model").show();
		$("select#msearch_model").hide();
		$("div#modelsearch_category").hide();
		$("div#modelsearch_category").html('');
		$("div#modelsearch_submit").hide();
		var brand = $(this).val();
		if(brand != '' && brand != 'brand') {
			$("div#modelsearch_model").append('<div id="model_loader"><img src="/images/ajax-loader.gif" style="width:16px;height:16px;" /></div>');
			$.get('ajax.php', {act: 'getModelsByBrand', brand: brand},
				function(returned_data) {
					$("select#msearch_model").html(returned_data);
					$("div#model_loader").remove();
					$("select#msearch_model").show();
				}
			);
		}
		else {
			$("div#modelsearch_model").hide();
		}
	});

	$("select#msearch_model").change(function() {
		$("div#modelsearch_category").show();
		$("div#modelsearch_category").html('');
		$("div#modelsearch_submit").hide();
		var brand = $("select#msearch_brand").val();
		var model = $(this).val();
		var id = $("input#id").val();
		var lang = $("input#lang").val();
		if(brand != '' && model != '' && model != 'model') {
			$("div#modelsearch_category").append('<div id="model_loader"><img src="/images/ajax-loader.gif" style="width:16px;height:16px;" /></div>');
			$.get('ajax.php', {act: 'getCategoriesByModelAndBrand', brand: brand, model: model, id: id, lang:lang},
				function(returned_data) {
					//$("select#msearch_category").html(returned_data);
					$("div#modelsearch_category").html(returned_data);
					$("div#model_loader").remove();
					$("select#msearch_category").show();
				}
			);
		}
		else {
			$("div#modelsearch_category").hide();
		}
	});
	/* </Modelsearch> */

	/* <PDF-generator> */
	$("select#search_productline_gen").click(function() {
		$("div#category_gen").hide();
		$("div#searchButton_gen").hide();

		var productline = document.getElementById('search_productline_gen').value;
		if(productline != '') {
			$.get('ajax.php', {act: 'getMenuLevel', parent: productline, 'view': 'icon'},
				function(returned_data) {
					// if data is returned
					if(returned_data != '') {
						document.getElementById('search_category_gen').disabled = false;
						$("select#search_category_gen").html(returned_data);
						// show category select
						$("div#category_gen").show();
						$("div#searchButton_gen").show();
					}
				}
			);
		}
	});
	/* </PDF-generator> */

	
	$("select#edit_productline").change(function() {
		var dropdown = document.getElementById('edit_productline');
		var index = dropdown.selectedIndex;
		var value = dropdown.options[index].value;
		var text = dropdown.options[index].text;
		$("input#edit_productline_name").val(text);
		$.get('ajax.php', { act: 'getProductlineView', productline: value},
			function(returned_data) {
				if(returned_data == '1') {
					document.getElementById('edit_productline_view').checked = true;
				}
				else {
					document.getElementById('edit_productline_view').checked = false;
				}
			}
		);
	});
	
	$("select#edit_category").change(function() {
		var dropdown = document.getElementById('edit_category');
		var index = dropdown.selectedIndex;
		var value = dropdown.options[index].value;
		var text = dropdown.options[index].text;
		$("input#edit_category_name").val(text);
	});
	
	
	/* <Basket> */
	
	$("input.addToBasket").click(function() {
		var productNumber = $(this).attr('name').split('_')[0];
		var id = $(this).attr('name').split('_')[2];
		//var amount = $("input[title='"+productNumber+"']").val();
		var amount = $(this).prev().val();
		if(amount == '') {
			//$("span.addToBasketStatus[id='"+productNumber+"']").html('Syötä kappalemäärä').css('color','#f00');
			$("+span", this).html('Syötä kappalemäärä').css('color','#f00').css('display', 'block');
			return false;
		}
		var RegExp = /^[0-9]{1,4}$/;
		if(!RegExp.test(amount) || amount == '0') {
			//$("span.addToBasketStatus[id='"+productNumber+"']").html('Kappalemäärä virheellinen').css('color','#f00');
			$("+span", this).html('Kappalemäärä virheellinen').css('color','#f00').css('display', 'block');
			return false;
		}
		
		$.get('ajax.php', {act: 'addToBasket', productNumber: productNumber, amount: amount, id: id});
		$("+span",this).html('OK!').css('color','#00b100').css('display', 'block');

		return false;
	});
	
	/* </Basket> */
	
	/*<Images without product - delete> */
	$("a.deleteImage").click(function() {
		var answer = confirm('Poistetaan kuva "'+$(this).attr('id')+'", oletko varma?');
		if(answer) {
			var IMG = $(this).attr('id');
			$.get('ajax.php', {act: 'deleteImage', image: IMG},
				function(returned_data) {
					if(returned_data) {
						$("a."+IMG).replaceWith('Poistettu');
						$("a#"+IMG).remove();
					}
					else {
						alert('Poisto epäonnistui.');
					}
				}
			);
		}
	});
	/*</Images without product - delete> */

	
	/* <News image select> */
	$("span.select a").click(function() {
		$("input#news_image").val($(this).attr('id'));
		return false;
	});
	/* </News image select> */
	

	/* <Productinfo popup> */
	$("a[id^='prodinfo_']").click(function() {
		var parts = $(this).attr('id').split('_');
		var url = "productinfo.php?";
		if(parts[1] != '') {
			url += "pcode="+escape(parts[1]);
		}
		if(parts[2] != '') {
			url = url+"&pmodel="+escape(parts[2]);
		}
		if(parts[3] != '') {
			url = url+"&pmotor="+escape(parts[3]);
		}
		if(parts[4] != '') {
			url = url+"&pyear="+escape(parts[4]);
		}
		// 28.01.2010, Tomi
		if($(this).attr('rel') != '') {
			url = url+"&id="+$(this).attr('rel');
		}
		
		var randomnumber = Math.floor(Math.random()*1000)
		var newWindw = window.open(url, randomnumber, "width=590, height=370, scrollbars=0");
		newWindow.focus();
		return false;
		//window.open('productinfo.php?pcode='+ID, 'Tuote: '+ID, 'width=650,height=370,scrollbars=no');
	});
	/* </Productinfo popup> */	
	
	
	/* Ennakko */
	$("input#addPrebuy, input#modifyPrebuy").click(function() {
			var error = false;
			
			if($("input#name").val() == '') {
				error = true;
				$("input#name").addClass('error');
			}
			else {
				$("input#name").removeClass('error');
			}
			
			if($("input#csv").val() == '' && $(this).attr('id') == 'addPrebuy') {
				error = true;
				$("input#csv").addClass('error');
			}
			else {
				$("input#csv").removeClass('error');
			}

			if($("input#startdate").val() == '') {
				error = true;
				$("input#startdate").addClass('error');
			}
			else {
				$("input#startdate").removeClass('error');
			}

			if($("input#enddate").val() == '') {
				error = true;
				$("input#enddate").addClass('error');
			}
			else {
				$("input#enddate").removeClass('error');
			}
			
			if(!error) {
				return true;
			}
			else {
				return false;
			}
		}
	);
	
	
	/* Admin */
	
	
	// 27.05.2011
	$("a#infofiletrigger").click(
		function() {
			$("div#infofileupload").show();
		}
	);
	
	$("input.novelty_type_chooser").click(
		function() {
			if($(this).val() == 'productnumber') {
				$("input#novelty_file").val('');
				$("input#novelty_header").val('');
				$("tr.file").hide();
				$("tr.productnumber").show();
			}
			else {
				$("tr.productnumber").hide();
				$("input#productnumber").val('');
				$("tr.file").show();
			}
		}
	);

	// 15.11.2011
	$("input.offer_type_chooser").click(
		function() {
			if($(this).val() == 'productnumber') {
				$("input#offer_file").val('');
				$("input#offer_header").val('');
				$("tr.file").hide();
				$("tr.productnumber").show();
			}
			else {
				$("tr.productnumber").hide();
				$("input#productnumber").val('');
				$("input#price").val('');
				$("tr.file").show();
			}
		}
	);
	
	// 08.01.2012
	$("input[name='level']").click(
		function() {
			if($(this).val() == '0') {
				$("input[name='price_visible'][value='1']").removeAttr('checked');
				$("input[name='price_visible'][value='0']").attr('checked', 'checked');
			}
			else {
				$("input[name='price_visible'][value='0']").removeAttr('checked');
				$("input[name='price_visible'][value='1']").attr('checked', 'checked');
			}
		}
	);
	
});



<!--
function popUp(product) {
	var theURL = 'productinfo.php?pcode='+product;
	var winName = product;
    var window_width = 590;
    var window_height = 370;
    var newfeatures = 'scrollbars=0';
    var window_top = (screen.height-window_height)/2;
    var window_left = (screen.width-window_width)/2;
    newWindow=window.open(theURL,winName);
    newWindow.focus();
}

function confirmDelete(the_url) {
	if (confirm("Oletko varma?")) {
		window.location.href=the_url;
	}
}

//-->

