//var DIR_WS_IMAGES = '/lasystems2/images/';
var DIR_WS_IMAGES = '/images/';
/* RELOCATE */
function relocate(url) {
	window.location = url;
}
/* END RELOCATE */

/* EFFECT */
function appearDiv(div) {
	Effect.Appear(div);
}

function fadeDiv(div) {
	Effect.Fade(div);
}

function showDiv(div) {
	$(div).style.display = 'block';
}

function hideDiv(div) {
	$(div).style.display = 'none';
}
/* END EFFECT */

function check_products_online( product_id ) {
	new Ajax.Request ('ajax_check_products_online.php', 
	{
		method : 'get',
		parameters : { product_id: product_id },
		onLoading: function () {
			$('page_content').style.display = "block";
		},
		onSuccess : function (transport) {
			var response = transport.responseText;
			if ( response != "" ) {
				$('page_content').innerHTML = response;
			}
		},
		onFailure : function () { alert("Error retreving the file."); }
	});
}

function searchProductKeyword(type) {
	var searchProductValue = $('searchByKeyword').value;
	
	if ( type == 1 && searchProductValue == '[keyword]' ) {
		$('searchByKeyword').value = '';
	}
	
	if ( type == 0 && searchProductValue == '' ) {
		$('searchByKeyword').value = '[keyword]';
	}
}

function searchProductCategory(value) {
	$('categorySearch').update(value);
	
	hideDiv('categorySelect');
}

function searchProductBrand(value) {
	$('brandSearch').update(value);
	
	hideDiv('brandSelect');
}

function chooseCategory() {
	if ( $('categorySelect').style.display == 'none' ) {
		showDiv('categorySelect');
	}
	else {
		hideDiv('categorySelect');
	}
}

function chooseBrand() {
	if ( $('brandSelect').style.display == 'none' ) {
		showDiv('brandSelect');
	}
	else {
		hideDiv('brandSelect');
	}
}

/* SCROLL ANIMATION */
var SPEED = 2;
function ScrollDown(ELEM) {
	if(typeof ELEM == 'string') {
		ELEM = document.getElementById(ELEM);
	}
	
	if(ELEM) {
		ELEM.scrolling = setInterval(function() { ELEM.scrollTop += SPEED; }, 20);
	}
}

function ScrollUp(ELEM) {
	if(typeof ELEM == 'string') {
		ELEM = document.getElementById(ELEM);
	}
	
	if(ELEM) {
		ELEM.scrolling = setInterval(function(){ ELEM.scrollTop -= SPEED; }, 20);
	}
}

function StopScroll(ELEM) {
	if(typeof ELEM == 'string') {
		ELEM = document.getElementById(ELEM);
	}
	
	if (ELEM) {
		clearInterval(ELEM.scrolling);
	}
}
var Timer;
function ScrollLeft() {
	Timer = setInterval("document.getElementById('productImagesContainer').scrollLeft -= 2", 15);
}
function ScrollRight() {
	Timer = setInterval("document.getElementById('productImagesContainer').scrollLeft += 2", 15);
}

/* END SCROLL ANIMATION */

function showSubMenu(id, type) {
	var totalNumber = $('totalNumber').value;
	var _id = '';
	
	for ( i = 1; i <= totalNumber; i++ ) {
		_id = 'submenu' + i;
		if ( $(_id) && _id != id ) {
			hideDiv(_id);
		}
	}
		
	if ( type == 1 ) {
		if ( $(id).style.display == 'none' ) {
			showDiv(id);
		}
		else {
			hideDiv(id);
		}
	}
}

function mainPageAnimation() {
	appearDiv('messageBig');
	
	setTimeout("appearDiv('animateCategory')", 1500);
	
	setTimeout("appearDiv('productBox1')", 3000);
	setTimeout("appearDiv('productBox2')", 4000);
	setTimeout("appearDiv('productBox3')", 5000);
	
	var manufacturer = $('current_manufacturer').value;
	
	setTimeout("loadCategory(" + manufacturer + ")", 8000);
}

function changeAttributes(categoryId) {
	if ( categoryId == 0 ) {
		$('priceProduct1').update(firstCategory[0][2]);
		$('priceProduct2').update(firstCategory[1][2]);
		$('priceProduct3').update(firstCategory[2][2]);
		
		$('productImage1').src = firstCategory[0][1];
		$('productImage2').src = firstCategory[1][1];
		$('productImage3').src = firstCategory[2][1];
		
		$('productLink1').onclick = function() { relocate(firstCategory[0][0]); }
		$('productLink2').onclick = function() { relocate(firstCategory[1][0]); }
		$('productLink3').onclick = function() { relocate(firstCategory[2][0]); }
		
		$('category1').className = 'animationCategoryLinkSelected';
		$('category2').className = 'animationCategoryLink';
		$('category3').className = 'animationCategoryLink';
		
		$('current_manufacturer').value = 1;
		setTimeout("loadCategory(" + $('current_manufacturer').value + ")", 8000);
	}
	
	if ( categoryId == 1 ) {
		$('priceProduct1').update(secondCategory[0][2]);
		$('priceProduct2').update(secondCategory[1][2]);
		$('priceProduct3').update(secondCategory[2][2]);
		
		$('productImage1').src = secondCategory[0][1];
		$('productImage2').src = secondCategory[1][1];
		$('productImage3').src = secondCategory[2][1];
		
		$('productLink1').onclick = function() { relocate(secondCategory[0][0]); }
		$('productLink2').onclick = function() { relocate(secondCategory[1][0]); }
		$('productLink3').onclick = function() { relocate(secondCategory[2][0]); }
		
		$('category1').className = 'animationCategoryLink';
		$('category2').className = 'animationCategoryLinkSelected';
		$('category3').className = 'animationCategoryLink';
		
		$('current_manufacturer').value = 2;
		setTimeout("loadCategory(" + $('current_manufacturer').value + ")", 8000);
	}
	
	if ( categoryId == 2 ) {
		$('priceProduct1').update(thirdCategory[0][2]);
		$('priceProduct2').update(thirdCategory[1][2]);
		$('priceProduct3').update(thirdCategory[2][2]);
		
		$('productImage1').src = thirdCategory[0][1];
		$('productImage2').src = thirdCategory[1][1];
		$('productImage3').src = thirdCategory[2][1];
		
		$('productLink1').onclick = function() { relocate(thirdCategory[0][0]); }
		$('productLink2').onclick = function() { relocate(thirdCategory[1][0]); }
		$('productLink3').onclick = function() { relocate(thirdCategory[2][0]); }
		
		$('category1').className = 'animationCategoryLink';
		$('category2').className = 'animationCategoryLink';
		$('category3').className = 'animationCategoryLinkSelected';
		
		$('current_manufacturer').value = 0;
		setTimeout("loadCategory(" + $('current_manufacturer').value + ")", 8000);
	}
}

function loadCategory(categoryId) {
	setTimeout("fadeDiv('product1')", 500);
	setTimeout("fadeDiv('product2')", 1000);
	setTimeout("fadeDiv('product3')", 1500);
	
	setTimeout("changeAttributes(" + categoryId + ")", 2100);
	
	setTimeout("appearDiv('product1')", 2000);
	setTimeout("appearDiv('product2')", 2500);
	setTimeout("appearDiv('product3')", 3000);
}

function changeCheckbox(number) {
	var currentValue = $('checkbox' + number + 'value').value;
	
	if ( currentValue == 1 ) {
		$('checkbox' + number).src = DIR_WS_IMAGES + 'checkbox_small.jpg';
		$('checkbox' + number + 'value').value = 0;
	}
	else {
		$('checkbox' + number).src = DIR_WS_IMAGES + 'checkbox_small_selected.gif';
		$('checkbox' + number + 'value').value = 1;
	}
}

function readDescriptionCart(productId, type) {
	if ( type == 0 ) {
		hideDiv('description_long' + productId);
		showDiv('description_short' + productId);
	}
	
	if ( type == 1 ) {
		hideDiv('description_short' + productId);
		showDiv('description_long' + productId);
	}
}

function deleteProductCart(product_id, osCsid, file) {
	var r = confirm('Are you sure you want to remove this product from your cart?');
	
	if ( r == true ) {
		relocate(file + '?osCsid=' + osCsid + '&product_id=' + product_id + '&action=delete_prod');
	}
}

function show_points_catalogue() {
	var thisHeight = screen.height;
	var thisWidth = screen.width;
	
	var newTop = (thisHeight) / 20;
	var newLeft = (thisWidth) / 2 - 300;
	
	$('popup_order_system').style.top = newTop + 'px';
	$('popup_order_system').style.left = newLeft + 'px';
	
	showDiv('popup_order_system');
} 

function close_point_system() {
	hideDiv('popup_order_system');
}

function select_point_product(product_id, product_name, product_description, product_image, product_price) {
	hideDiv('all_products_pages');
	hideDiv('all_products');
	
	$('active_product').value = product_id;
	$('product_detail_title').update(product_name);
	$('product_detail_image').src = DIR_WS_IMAGES + 'points_system/' + product_image;
	$('product_detail_description').update(product_description);
	$('product_detail_price').update(product_price + ' points');
	
	showDiv('product_points_details');
}

function deselect_point_product(osCsid) {
	hideDiv('product_points_details');
	
	$('active_product').value = 0;
	$('active_product').value = 0;
	$('product_detail_title').update('');
	$('product_detail_image').src = '';
	$('product_detail_description').update('');
	$('product_detail_price').update('');
	
	$('transaction_id').update('<a href="javascript: add_point_order(\'' + osCsid + '\');"><img border="0" src="' + DIR_WS_IMAGES + 'order_now.jpg"></a>');
	
	showDiv('all_products_pages');
	showDiv('all_products');
}

function add_point_order(osCsid) {
	showDiv('show_loader');
	
	var active_product = $('active_product').value;
	var message = '';
	var status = -1;
	
	new Ajax.Request ('ajax_make_point_products.php', {
		method : 'post',
		parameters : { active_product: active_product, osCsid: osCsid },
		onSuccess : function (transport) {
			var response = transport.responseText;
			
			if ( response == 0 ) {
				status = 1;
				message = 'Order succesfully added';
				
				var current_points = parseInt($('my_points').innerHTML);
				var product_points = $('product_detail_price').innerHTML;
				
				product_points = parseInt(product_points.replace(' points',  ''));
				
				var remaining_points = current_points - product_points;
				
				$('my_points').update(remaining_points);
			}
			
			if ( response == 1 ) {
				status = 0;
				message = 'Please login to order';
			}
			
			if ( response == 2 ) {
				status = 0;
				message = 'You don\'t have enough points';
			}
			
			var response = '';
	
			if ( status == 0 ) {
				response = '<table><tr>';
				response += '<td><img src="' + DIR_WS_IMAGES + 'attention.jpg"></td>';
				response += '<td class="small_attention_text">' + message + '</td>';
				response += '<td></td>';
				response += '</tr></table>';
			}
			
			if ( status == 1 ) {
				response = '<table><tr>';
				response += '<td><img src="' + DIR_WS_IMAGES + 'accepted.jpg"></td>';
				response += '<td class="small_accepted_text">' + message + '</td>';
				response += '<td></td>';
				response += '</tr></table>';
			}
			
			$('transaction_id').update(response);
	
			hideDiv('show_loader');
		},
		onFailure : function () { alert("Error retreving the file."); }
	});
}

function change_point_page(page) {
	showDiv('show_loader');
	new Ajax.Request ('ajax_get_point_products.php', {
		method : 'post',
		parameters : { page: page },
		onSuccess : function (transport) {
			var response = transport.responseText;
			
			$('all_products').update(response);
			hideDiv('show_loader');
			
			var active_link = $('active_link').value;
			$('page_link_' + active_link).className = 'page_points_normal';
			$('page_link_' + page).className = 'page_points_selected';
			$('active_link').value = page;
		},
		onFailure : function () { alert("Error retreving the file."); }
	});
}

function select_feature_categ(type) {
	if ( type == 0 ) {
		hideDiv('cheap_items');
		hideDiv('best_items');
		hideDiv('cheap_items_link');
		hideDiv('best_items_link');
		
		showDiv('new_items');
		showDiv('new_items_link');
		
		$('llink1').className = "headerFeaturedLinkSelected";
		$('llink2').className = "headerFeaturedLink";
		$('llink3').className = "headerFeaturedLink";
	}
	
	if ( type == 1 ) {
		hideDiv('new_items');
		hideDiv('best_items');
		hideDiv('new_items_link');
		hideDiv('best_items_link');
		
		showDiv('cheap_items');
		showDiv('cheap_items_link');
		
		$('llink1').className = "headerFeaturedLink";
		$('llink2').className = "headerFeaturedLinkSelected";
		$('llink3').className = "headerFeaturedLink";
	}
	
	if ( type == 2 ) {
		hideDiv('new_items');
		hideDiv('best_items');
		hideDiv('cheap_items_link');
		hideDiv('new_items_link');
		
		showDiv('cheap_items');
		showDiv('best_items_link');
		
		$('llink1').className = "headerFeaturedLink";
		$('llink2').className = "headerFeaturedLink";
		$('llink3').className = "headerFeaturedLinkSelected";
	}
}

function changeProductPicture(ob, text) {
	$('productPicture').update('<a href="' + ob.src.replace('thumbnail', 'medium') + '" target="_blank" class="enlarge_class"><img src="' + ob.src + '" border="0"><br>'+text+'</a>');
}

function changeProductImage(type, products_id, pi_id) {
	new Ajax.Request('ajax_change_product_image.php', {
		method: 'post',
		parameters: {
			type: type,
			products_id: products_id,
			pi_id: pi_id
		},
		onSuccess: function (transport) {
			var response = transport.responseText;
			$('productImage').update(response);
		}
	});
}

function show_fees(id) {
	$j('#' + id).fadeIn(400);
}

function hide_fees(id) {
	$j('#' + id).fadeOut(400);
}

//product info
function change_thumb(href, pos, zoomed_href) {
	//$j('#zoomed').val(href);
	$j('#zoomed').val(zoomed_href);
	$j('#current_zoomed').val(pos);
	var urls = [ href ];
	$j.preload( urls, {
		base: DIR_WS_IMAGES,
		ext:'.jpg',
		onComplete: function(data) {
			var img = new Image();
			img.src = data.image;
			var newWidth = img.width;
			var newHeight = img.height;
			var maxWidth = 176;
			var maxHeight = 176;
			if (newWidth > maxWidth) {
				newHeight = maxWidth/newWidth*newHeight;
				newWidth = maxWidth;
			}
			if (newHeight > maxHeight) {
				newWidth = maxHeight/newHeight*newWidth;
				newHeight = maxHeight;
			}
			$j('#image_small_preview').attr('width', newWidth);
			$j('#image_small_preview').attr('height', newHeight);
			$j('#image_small_preview').attr('src', img.src);
		}
	});
	if ( pos == 0) {
		$j('#product_save_percent').show();
	} else {
		$j('#product_save_percent').hide();
	}
}
function show_zoom(href) {
	$j('#image_small_preview').fadeOut(200);
	$j('#zoom_tool').hide(200);
	$j('#zoom_animation').slideDown(500);
	var urls = [ href ];
	$j.preload( urls, {
		base: DIR_WS_IMAGES,
		ext:'.jpg',
		onComplete: function(data) {
			var img = new Image();
			img.src = data.image;
			var newWidth = img.width;
			var newHeight = img.height;
			var maxWidth = 510;
			var maxHeight = 420;
			if (newWidth > maxWidth) {
				newHeight = maxWidth/newWidth*newHeight;
				newWidth = maxWidth;
			}
			if (newHeight > maxHeight) {
				newWidth = maxHeight/newHeight*newWidth;
				newHeight = maxHeight;
			}
			$j('#image_big_preview').attr('width', newWidth);
			$j('#image_big_preview').attr('height', newHeight);
			$j('#image_big_preview').attr('src', img.src);
		}
	});
	if ($j('#current_zoomed').val() == 0) {
		$j('#arrow_left_slide').attr('src', DIR_WS_IMAGES + 'arrow_left_off.gif');
	} else {
		$j('#arrow_left_slide').attr('src', DIR_WS_IMAGES + 'arrow_left.gif');
	}
	if ($j('#current_zoomed').val() == $j('#total_pic').val() - 1) {
		$j('#arrow_right_slide').attr('src', DIR_WS_IMAGES + 'arrow_right_off.gif');
	} else {
		$j('#arrow_right_slide').attr('src', DIR_WS_IMAGES + 'arrow_right.gif');
	}
	var current = $j('#current_pic').val();
	$j('#current_page_it').html(current);
}
function hide_zoom() {
	$j('#zoom_animation').slideUp(500);
	$j('#image_small_preview').fadeIn(200);
	$j('#zoom_tool').show(200);
}
function slide_image(type) {
	var current = $j('#current_pic').val();
	var total = $j('#total_pic').val();
	var product = '';
	if (type == 0) {//left
		current--;
		if (current >= 1) {
			//product = $j('#product_picture' + current).attr('src');
			product = $j('#product_picture_' + current).val();
			product = product.replace('.jpg', '');
			product = product.replace('/thumb/', '/large/');
			if (current == 1) {
				product = product.split('images/');
			} else {
				product = product.split('../images/');
			}
			product = product[product.length-1];
			show_zoom(product);
			$j('#current_pic').val(current);
			$j('#current_page_it').html(current);
			if (current > 1) {
				$j('#arrow_left_slide').attr('src', DIR_WS_IMAGES + 'arrow_left.gif');
			} else {
				$j('#arrow_left_slide').attr('src', DIR_WS_IMAGES + 'arrow_left_off.gif');
			}
			if (current < total) {
				$j('#arrow_right_slide').attr('src', DIR_WS_IMAGES + 'arrow_right.gif');
			} else {
				$j('#arrow_right_slide').attr('src', DIR_WS_IMAGES + 'arrow_right_off.gif');
			}
		}
	} else {//right
		current++;
		if (current <= total) {
			//product = $j('#product_picture' + current).attr('src');
			product = $j('#product_picture_' + current).val();
			product = product.replace('.jpg', '');
			product = product.replace('/thumb/', '/large/');
			product = product.split('/images/');
			product = product[product.length-1];
			show_zoom(product);
			$j('#current_pic').val(current);
			$j('#current_page_it').html(current);
			if (current > 1) {
				$j('#arrow_left_slide').attr('src', DIR_WS_IMAGES + 'arrow_left.gif');
			} else {
				$j('#arrow_left_slide').attr('src', DIR_WS_IMAGES + 'arrow_left_off.gif');
			}
			if (current < total) {
				$j('#arrow_right_slide').attr('src', DIR_WS_IMAGES + 'arrow_right.gif');
			} else {
				$j('#arrow_right_slide').attr('src', DIR_WS_IMAGES + 'arrow_right_off.gif');
			}
		}
	}
}
//end product info
/*
function showHideElement(elementId) {
	if ($(elementId).style.display == 'none') {
		$(elementId).style.display = 'block';
	} else {
		$(elementId).style.display = 'none';
	}
}
*/

var ns=(document.layers);
var ie=(document.all);
var w3=(document.getElementById && !ie);

function browser_center_left(iWidth) {
	var documentWidth = 0;
	var documentLeft = 0;
	if (self.innerWidth) {
	  documentWidth = self.innerWidth/2 + window.pageXOffset - 20;
	  if (window.top.cumain) if (window.top.cumain.culeft) documentWidth = documentWidth - window.top.cumain.culeft.innerWidth/2 + window.pageXOffset;
	} else if (window.innerWidth) {
	  documentWidth = window.innerWidth/2 + window.pageXOffset - 20;
	  if (window.top.cumain) if (window.top.cumain.culeft) documentWidth = documentWidth - window.top.cumain.culeft.innerWidth/2 + window.pageXOffset;
	} else if (ie) {
	  documentWidth = browser_body().offsetWidth/2 + browser_body().scrollLeft - 20;
	  if (window.top.cumain) if (window.top.cumain.culeft) documentWidth = documentWidth - window.top.cumain.culeft.browser_body().offsetWidth/2 + window.top.cumain.culeft.browser_body().scrollLeft;
	}
	if (documentWidth>0) documentLeft = documentWidth - iWidth/2;
	if (documentLeft<20) documentLeft = 20;
	return documentLeft;
}

function browser_center_top(iHeight) {
	var documentHeight = 0;
	var documentTop = 0;
	if (self.innerHeight) {
	  documentHeight = self.innerHeight/2 + window.pageYOffset - 20;
	  //if (window.top.cumain) if (window.top.cumain.culeft) documentHeight = documentHeight - window.top.cumain.culeft.innerHeight/2 + window.pageYOffset;
	} else if (window.innerHeight) {
	  documentHeight = window.innerHeight/2 + window.pageYOffset - 20;
	  //if (window.top.cumain) if (window.top.cumain.culeft) documentHeight = documentHeight - window.top.cumain.culeft.innerHeight/2 + window.pageYOffset;
	} else if (ie) {
	  documentHeight = browser_body().offsetHeight/2 + browser_body().scrollTop - 20;
	  //if (window.top.cumain) if (window.top.cumain.culeft) documentHeight = documentHeight - window.top.cumain.culeft.browser_body().offsetHeight/2 + window.top.cumain.culeft.browser_body().scrollTop;
	}
	if (documentHeight>0) documentTop = documentHeight - iHeight/2;
	if (documentTop<20) documentTop = 20;	
	return documentTop;
}

function browser_body() {
	return (document.compatMode && document.compatMode!="BackCompat") ? document.documentElement : document.body;
}

var online_check_enable = true;
var ctime = 0;

function add_products_to_cart(products_id, cart_quantity, img, ajax_page) {
	tb_show(null, ajax_page + '&products_id=' + products_id + '&cart_quantity=' + cart_quantity, false);
	/*
	if (online_check_enable) {
		online_check_enable = false;
		clearTimeout(ctime);
		var width = 400;
		var height = 200;
		var left=browser_center_left(parseInt(width)).toFixed(0).toString()+'px';
		var top=browser_center_top(parseInt(height)).toFixed(0).toString()+'px';
		$('addtocartdiv').setStyle({
	    position: 'absolute',
	    left: left,
	    top: top,
	    width: width,
	    height: height,
	    zIndex: 100,  
	    display: 'block',
	    backgroundColor: 'white',
	    borderWidth: '1px',
	    borderStyle: 'solid',
	    borderColor: 'black',
	    padding: '8px'
	  });
		$('addtocartdiv').innerHTML = img;
		new Ajax.Request (ajax_page,
			{
				method: 'get',
				parameters: {
					products_id: products_id,
					cart_quantity: cart_quantity
				},
				onSuccess: function (transport) {
					var response = transport.responseText;
					$('addtocartdiv').innerHTML = response;
					refresh_header_cart_info();
					ctime = setTimeout('remove_add_products_to_cart();', 60*1000);
					online_check_enable = true;
				},
				onFailure: function () { alert("Error retreving the file."); }
			}
		);
	}
	*/
}

function remove_add_products_to_cart() {
	tb_remove();
	//$('addtocartdiv').hide();
	//parent.location.reload(1);
}
