/********************************************************************************/
/************         Web shop Locale analog        *************************/
var Locale = {};

Locale.get = function(key){
	var warnStr = '';
	if (TRANSLATION_WARNING == '1') {
		warnStr = ' <span style="color:red">(TRANSLATE !)</span>';
	}
	return WEBSHOP_LANG[key] ? WEBSHOP_LANG[key] : key + warnStr;
}


function show_subcatalog(id)
{
    var cat = document.getElementById("catalog_"+id);
    var img = document.getElementById("img_"+id);
    if (cat.className.indexOf("catalog") != -1)
    { 
        cat.className = "hidden";
        img.src = "/common/img/right.gif";
    }
    else
    {
        cat.className = "catalog"; 
        img.src = "/common/img/down.gif";
    }
    
}

/********************************************************************************/
/********************************************************************************/
/********************************************************************************/

function getRequestInstance()
{
	if (!window.opera && document.all)
	{
	    return new ActiveXObject("Msxml2.XMLHTTP");
	}
	else
	{
		return new XMLHttpRequest();
	}
}

function remoteCall(url, method, params, function_name)
{
	if(req==null) var req = getRequestInstance();
	req.onreadystatechange = function()
	{
		try
		{
			if (req.readyState == 4)
			{
				if (req.status != 200)
				{
					throw alert('Error '+req.status);
				}
    			function_name(req);
            }
		}
		catch (error)
		{
		//	alert(error.description);
		}
	}
	req.open(method, url);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send(params);
}

function empty(){}

/********************************************************************************/
/********************************************************************************/
/********************************************************************************/

function addProduct(id, onComplete) {
	var url = typeof LANG != 'undefined' ? '/' + LANG + '/shoppingcard/add/pid/' : '/shoppingcard/add/pid/';
	if (id) {
		if (typeof onComplete == 'function') {
			remoteCall(url + id, 'GET', null, onComplete);
		}		
	} else {
		//alert('main.js fucntion addProduct. error: id is NULL');
	}
}

var counts = 0;


function updateShortCart (count, price) {
	document.getElementById('shopping_card_count').innerHTML = count;
	document.getElementById('shopping_card_total_price').innerHTML = price;
	window.setTimeout('updateBlink()',100);
}

function updateBlink() {
	var header = document.getElementById("shopping_card_header");
	var list   = document.getElementById("shopping_card_list");  
	if (counts%2==0) {
		header.className += " headerWebshop_blink";
		list.className   += " list_blink";
	} else {
		header.className = header.className.replace('headerWebshop_blink','');
		list.className   = list.className.replace('list_blink','');	
	}
	
	counts++;
	if (counts < 6) {
		window.setTimeout('updateBlink()', 100);
	} else {
		if (document.getElementById('shoppingCardFullList') != null) {
			window.location.href = '/'+LANG+'/shoppingcard';
		}
	}
}

/********************************************************************************/
/********************************************************************************/
/********************************************************************************/

function formSubmit(url,method,formId)
{
	var form = document.getElementById(formId);
	form.action = url;
	form.method = method;
	form.submit();
	
}

/********************************************************************************/
/********************************************************************************/
/********************************************************************************/

var top;
var fx;
var flag = false;


function recalculateShoppingCardPosition (firstShow) {
	var target = $('shopping_card');
	if (target == null) { return; } // bug (on shoppingcard/end page) fix

	var coord = target.getCoordinates();
	//offsetWidth, offsetHeight, offsetTop
	var width  = coord.width
	var height = coord.height;
	top        = coord.top;
	left       = coord.left;

// empty div 	
	if (firstShow || $('hidden_basket_div') == null) {
		var div = document.createElement('div');
		div.className = 'list';
		div.style.padding = '0px';
		div.style.border = 'none';
		div.style.visibility = 'hidden';
		div.style.width = width + 'px';
		div.style.height = height + 'px';
		div.id = "hidden_basket_div";
		target.parentNode.insertBefore(div,target);
	} else {
		var div = $('hidden_basket_div');
		var div_coord = div.getCoordinates();
		target.style.left = div_coord.left + 'px';
	}
// basket	
	target.style.position = 'absolute';
	target.style.width = width + 'px';
	target.style.height = height + 'px';
	target.style.top = top + 'px';
	if (firstShow) { target.style.left = left + 'px'; }
	fx = new Fx.Styles(target, {duration: 10, wait: false});
}

window.addEvent('resize', function () { 
	recalculateShoppingCardPosition();
});


// !! Assign on Drag functionality to products
window.addEvent('domready', function() {

	recalculateShoppingCardPosition(true);
	
	var drop = $('shopping_card_list');
	$$('.dragged_area').each(function(item){
		item.addEvent('mousedown', function(e) {
			e = new Event(e).stop(); // move basket
			if(flag){
				fx.start({
					'top': [e.page.y - 25]
				});
			}
	
			var clone = this.clone()
				.setStyles(this.getCoordinates()) 
				.setStyles({'opacity': 0.7, 'position': 'absolute'})
				.addClass('item_clone')
				.addEvent('emptydrop', function() {
					this.remove();
					drop.removeEvents();
				}).inject(document.body);
	
			drop.addEvents({
				'drop': function() {
					drop.removeEvents();
					clone.remove();
				// call WebshopProductClass.params.getRemoteData function (loadAttributesData for metacategory, or loadProductData)
					var id     = item.getProperty('common_id');
					var hash   = item.getProperty('rand_hash');
					var button = $('a_' + id + '_' + hash);
					button.fireEvent('click', e); // e - event. required here
					setShoppingStyle('');
				},
				'over': function() {
					setShoppingStyle('_blink');
				},
				'leave': function() {
					setShoppingStyle('');
				}
			});
	
			var left = null;
			var right = null;
			var drag = clone.makeDraggable({
				droppables: [drop],
				onStart: function(){left = clone.style.left; right = clone.style.left;},
				onComplete: function(){
					if (left == clone.style.left && right == clone.style.left){
						var id = item.getProperty('common_id');
						if (item.getProperty('is_metacategory') == 1) {
							document.location.href = '/'+LANG+'/products/info/metacategory_id/' + id;
						} else {
							document.location.href = '/'+LANG+'/products/info/id/' + id;
						}
					}
				}
			}); 
	
			drag.start(e); 
		});
		
	});		


});



/*****      Change Shopping Card   style     ***************/

function setShoppingStyle(style) {
	if(style == '_blink'){
		document.getElementById("shopping_card_header").className += " headerWebshop"+style;
		document.getElementById("shopping_card_list").className += " list"+style;
	}
	else{
		document.getElementById("shopping_card_header").className = document.getElementById("shopping_card_header").className.replace('headerWebshop_blink','');
		document.getElementById("shopping_card_list").className = document.getElementById("shopping_card_list").className.replace('list_blink','');
	}
	
}


function  moveBasket (e) {
	try {
		e = new Event(e).stop();
		if(flag){
			fx.start({
				'top': [e.page.y - 25]
			});
		}
	} catch (err) {	}
}


window.onscroll = function(event){
	if(document.documentElement.scrollTop <= top){
		fx.start({
			'top': [top]
		});
		flag = false;
	}
	else flag = true;
}
