function bookmarksite(title,url) {
	if (window.sidebar) window.sidebar.addPanel(title, url, "");
	else if(document.all) window.external.AddFavorite(url, title);
	else if(window.opera && window.print){ window.external.AddFavorite(url, title);
	/*
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
		*/
	}
}

function tabShow(_tab) {
	$('tabContent').style.display = 'none';
	$('tabComment').style.display = 'none';
	
	$('menuContent').className = '';
	$('menuComment').className = '';
	
	//$('tabContent').style.display = 'none';
	$('tab' + _tab).style.display = '';
	$('menu' + _tab).className = 'on';
}

function loadCurrentList(offset) {
	$('currentPosition').style.display = '';

	if ($('current_list_loading'))
		$('current_list_loading').style.display = '';
	
	var qs = '';
	qs += $("product_view_form").serialize();
	qs += '&data[offset]='+offset;
	
	var url = 'product.php?action=ajax_current_list&'+qs;
	new Ajax.Request(url, {
		asynchronous:true,
		onComplete:function(request){
			$("currentPosition").innerHTML = request.responseText;

			if ($('current_list_loading'))
				$('current_list_loading').style.display = 'none';
		}
	});
}

function product_view(product_id, product_action) {
    var f = document.forms.product_view_form;

    f.attributes['action'].value = product_action;
    f.elements['product_id'].value = product_id;
    f.submit();
    return false;
}

function loadRelatedList(_product_id) {	
	var url = 'product.php?action=ajax_related_list&product_id=' + _product_id;
	new Ajax.Request(url, {
		asynchronous:true,
		onComplete:function(request){
			$("relatedHolder").innerHTML = request.responseText;
		}
	});
}

function moveCurrentListLeft(diff) {
	var i;
	
	for (i=0;i<5;i++) {
		if ($('currentProduct'+(i+diff))) {
			$('currentProduct'+i).className = $('currentProduct'+(i+diff)).className;
			$('currentProduct'+i).innerHTML = $('currentProduct'+(i+diff)).innerHTML;
		} else {
			$('currentProduct'+i).innerHTML = '';
		}
	}
}

function moveCurrentListRight(diff) {
	var i;
	
	for (i=4;i>=0;i--) {
		if ($('currentProduct'+(i-diff))) {
			$('currentProduct'+i).className = $('currentProduct'+(i-diff)).className;
			$('currentProduct'+i).innerHTML = $('currentProduct'+(i-diff)).innerHTML;
		} else {
			$('currentProduct'+i).innerHTML = '';
		}
	}
}

function setImage(lrg,img)
{
	document.getElementById("mainImage").src=lrg;
	document.getElementById("magnifier").href=img;
	largePhoto = img;
}

function openPhoto() {
	path =  document.getElementById("magnifier").href;
/*
	myLightWindow.activateWindow({
		href: path,
		title: ''
	});
*/
//	window.open(path,'large_photo','width=300,height=300');
	var img = new Image();
	Event.observe(img, 'load', function() {
		var width = img.width;
		var height = img.height;
		var dim = document.viewport.getDimensions();
		var scr = document.viewport.getScrollOffsets();
		$('popupBox').style.display = 'none';
		$('popupBoxContent').innerHTML = '';
		$('popupBox').style.left = ((dim['width']-width-20)/2+scr['left']) + 'px';
		$('popupBox').style.top = ((dim['height']-height-20)/2+scr['top']) + 'px';
		$('popupBox').style.width = $('popupBox').style.width = '0px';
//		$('popupBox').style.overflow = '';
//		$('popupBox').style.display = '';

		var imgOff = $('mainImage').cumulativeOffset();
		var imgDim = $('mainImage').getDimensions();

/*
		new Effect.Scale($('popupBox'), 100, {
			duration: 0.4,
			scaleContent: true,
			scaleFrom: 10,
			scaleFromCenter: true,
			scaleMode: {originalHeight: height+20, originalWidth: width+20},
			afterFinish: function(ef) {
				$('popupBoxContent').innerHTML = '<img src="'+path+'" />';

				Event.observe($('popupBox'), 'click', function() {
					$('popupBox').style.display = 'none';
					$('popupBoxContent').innerHTML = '';
				});

			}
		});
*/

		$('popupBox').style.width = $('mainImage').width + 'px';
		$('popupBox').style.height = $('mainImage').height + 'px';
		$('popupBox').style.top = imgOff.top + 'px';
		$('popupBox').style.left = imgOff.left + 'px';
		$('popupBox').style.display = '';
		new Effect.Parallel([
//				new Effect.Appear($('popupBox'), { duration: 0.5, from: 0.7, to: 1,sync: true }),
				new Effect.Scale($('popupBox'), 100, {
					duration: 0.5,
					scaleContent: true,
					scaleFrom: 100 * $('mainImage').width / (width+20),
					scaleMode: {originalHeight: height+35, originalWidth: width+20},
					sync: true
				}),
				new Effect.Move($('popupBox'), {
					duration: 0.5,
					x: ((dim['width']-width-20)/2+scr['left']),
					y: ((dim['height']-height-20)/2+scr['top']),
					mode: 'absolute',
					sync: true
				})
			],
			{
			duration: 0.5,
			afterFinish: function(ef) {
				$('popupBoxContent').innerHTML = '<img src="'+path+'" />';

				Event.observe($('popupBox'), 'click', function() {
					$('popupBox').style.display = 'none';
					$('popupBoxContent').innerHTML = '';
				});

			}
		});

	});
	img.src = path;

}
function openLargePhoto(src) {
	var img = document.getElementById('largeImage');

	img.src = 'images/spacer.gif';
  if (src)
		img.src = src;
	else
		img.src = largePhoto;

	var imgDiv = document.getElementById('largeImageDiv');
	imgDiv.style.display = '';
}
function closeLargePhoto() {
	var imgDiv = document.getElementById('largeImageDiv');
	imgDiv.style.display = 'none';
}
function rateProduct(pid,rate){
	$('rating').style.display = 'none';
	//$('send').style.display = 'none';
	//$('ratingLoad').style.display = '';

	pid= parseInt(pid);
	rate=parseInt(rate);
	//alert('ajax_functions.php?action=product_rating&product_id='+pid+'&rate='+rate);
	new Ajax.Request('ajax_functions.php?action=product_rating&product_id='+pid+'&rate='+rate, {
		asynchronous:true,
		onComplete:function(request){
			var json;
			//alert(request.responseText);
			eval('json = ' + request.responseText);
			//$('ratingLoad').style.display = 'none';
			$('ratingMessage').innerHTML= json[0].message;
			//$('currentRating').style.width = json[0].precent;
			//$('currentRatingMain').style.width = json[0].precent;
		}
	});

}

function loadCompareList(cid) {
	$('compareProducts').style.display = '';
	$('compare_list_loading').style.display = '';
	$('compareProducts').scrollTo();

	var qs = 'product_id={DMY}{PRODUCT_ID}';
	qs += '&' + $("product_view_form").serialize();
  new Ajax.Request('proizvod.asp?action=ajax_compare_list&'+qs, {
                    asynchronous:true,
                    onComplete:function(request){
											$("compareProductsContent").innerHTML = request.responseText;
											$('compare_list_loading').style.display = 'none';
                     }
                    });

}

function viewFormSubmit(prodId, pageLink) {

		//var f = document.forms.view_form;
		var f = $("product_view_form")

		f.attributes['action'].value = pageLink;
		if (f.elements['product_id'])
			f.elements['product_id'].value = prodId;
		f.submit();
		return false;
}

function changeProductImage(image_id) {

	clearTimeout(autoTimeout);
	if (image_id != '' && productImages['byImageId'][image_id]) {
		setImage(productImages['byImageId'][image_id]['src'],productImages['byImageId'][image_id]['largeSrc']);

		var imgId;
		var el;
		try {
		if (current_image_id > 0) {
			imgId = "thumb"+current_image_id;
			el = $(imgId);
			if (el) {
				el.style.border = '1px solid #FFFFFF;';
			}
		}
		} catch(err) {}

		try {
		current_image_id = image_id;
		if (current_image_id > 0) {
			imgId = "thumb"+current_image_id;
			el = $(imgId);
			if (el) {
				el.style.border = '1px solid #888888;';
			}
		}
		} catch(err) {}
	}

}

function startAutoChangeProductImage() {
	clearTimeout(autoTimeout);
	if (productImages['byImageNum'].length > 1)
		autoTimeout = setTimeout("autoChangeProductImage()",5000);
}

function autoChangeProductImage() {
	clearTimeout(autoTimeout);
	var nextNum = (1+productImages['byImageId'][current_image_id]['num'])%productImages['byImageNum'].length;
	var nextImageId = productImages['byImageNum'][nextNum];
	changeProductImage(nextImageId);
	autoTimeout = setTimeout("autoChangeProductImage()",5000);
}

function productRate(value) {
	var updateOptions = {
		product_id: startup_options.product_id,
		rating: value
	}
	new Ajax.Request('product.php?action=ajax_product_rating', {
		parameters : Object.extend(updateOptions, {}),
		onComplete: function(request) {
			var json;
			eval('json = ' + request.responseText);
			prod_rate.setValue(json.percent, true, true);
			$('rating_value').innerHTML = json.message;
		}
	});
}

var current_image_id = '';
var productImages = new Object();
productImages['byImageId'] = new Object();
productImages['byImageNum'] = new Array();
var autoTimeout;
