$(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.');
					}
				}
			);
		}
	);

	$('a[rel=lightbox]').lightBox();
	
	$('div#newsImages').cycle({ 
		fx: 'fade', 
		speed: 1000,
		timeout: 10000
	});
	
	$("select#update_productline").change(function() {
		var productline = document.getElementById('update_productline').value;
		if(productline != '') {
			$.get('ajax.php', {act: 'getCategories', productline: productline},
				function(returned_data) {
					$("select#update_category").html(returned_data);
				}
			);
		}
	});

	$("select#edit_cat_productline").change(function() {
		var productline = document.getElementById('edit_cat_productline').value;
		if(productline != '') {
			$.get('ajax.php', {act: 'getCategories', productline: productline},
				function(returned_data) {
					$("select#edit_category").html('<option value="">Valitse...</option>'+returned_data);
				}
			);
		}
	});
	
	$("select#delete_productline").change(function() {
		var productline = document.getElementById('delete_productline').value;
		if(productline != '') {
			$.get('ajax.php', {act: 'getCategories', productline: productline},
				function(returned_data) {
					$("select#delete_category").html(returned_data);
				}
			);
		}
	});
	
	/* <Product search> */
	$("select#search_productline").click(function() {
		$("div#category").hide();
		$("div#brand").hide();
		$("div#model").hide();
		$("div#searchButton").hide();

		var productline = document.getElementById('search_productline').value;
		if(productline != '') {
			$.get('ajax.php', {act: 'getCategories', productline: productline},
				function(returned_data) {
					// if data is returned
					if(returned_data != '') {
						// enable category -field because there's data to be input
						document.getElementById('search_category').disabled = false;
						$("select#search_category").html(returned_data);
						// get the product view style from db
						$.get('ajax.php', { act: 'getProductlineView', productline: productline},
							function(returned_data) {
								// if productview
								if(returned_data == '0') {
									// add value to view -field
									$("input#view").val('0');
								}
								else {
									// add value to view-field
									$("input#view").val('1');									
								}
								// show category select
								$("div#category").show();
							}
						);
					}
				}
			);
		}
	});

	$("select#search_category").click(function() {
		$("div#brand").hide();
		$("div#model").hide();
		$("div#searchButton").hide();

		var category = document.getElementById('search_category').value;

		if($("input#view").val() == '1') {
		
			// 27.01.2010, Tomi. Tarkistetaan löytyykö joltain tuotteelta merkki-kentästä tietoa. Jos löytyy, niin näytetään merkki -kenttä asiaankuuluvalla sisällöllä.
			$.get('ajax.php', {act: 'checkBrands', category: category},
				function(returned_data) {
					if(returned_data == 'ok') {
						$.get('ajax.php', {'act': 'getBrands', 'category': category, 'all': 'ok'},
							function(returned_data) {
								if(returned_data != '') {
									$("select#search_brand").html(returned_data);
									$("div#brand").show();
								}
							}
						);
					}
					else {
						$("select#search_brand").html('');
						$("div#searchButton").show();
					}
					
				}
			);
			//$("div#searchButton").show();

		}
		else {
			//var category = document.getElementById('search_category').value;
			if(category != '') {
				$.get('ajax.php', {'act': 'getBrands', 'category': category},
					function(returned_data) {
						if(returned_data != '') {
							$("select#search_brand").html(returned_data);
							$("div#brand").show();
						}
					}
				);
			}
		}
	});

	$("select#search_brand").click(function() {
		
		if($("input#view").val() == '1') {
			$("div#searchButton").show();
		}
		else {
			$("div#searchButton").hide();
		
			var brand = document.getElementById('search_brand').value;
			if(brand != '') {
				$.get('ajax.php', {act: 'getModels', brand: brand, category: document.getElementById('search_category').value},
					function(returned_data) {
						//if(returned_data != '') {
							$("div#model").show();
							document.getElementById('search_model').disabled = false;
							$("select#search_model").html(returned_data);
						//}
						/*
						else {
							$("div#model").hide();
							document.getElementById('search_model').disabled = true;
							$("select#search_category").html('<option value="">Valitse ensin merkki.</option>');
						}
						*/
					}
				);
			}
		}
	});

	$("select#search_model").click(function() {
		$("div#searchButton").show();
	});
	/* </Product search> */


	/* <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: 'getCategories', productline: productline},
				function(returned_data) {
					// if data is returned
					if(returned_data) {
						// enable category -field because there's data to be input
						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('Tuote lisätty!').css('color','#00b100').css('display', 'block');

		return false;
	});
	
	/* </Basket> */
	
	/*<Images without product - delete> */
	
	$("a.deleteImage").click(function() {
		var answer = confirm('Oletko varma, että haluat poistaa kuvan "'+$(this).attr('id')+'"?');
		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;
			}
		}
	);
});
//})(jQuery);


<!--
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();
}
//-->
