/**
 * @author Developing
 */

function HintsHandler(ajaxScriptPath, hintDivID, objectsSelectorString)
{
	this.selectorString = objectsSelectorString;
	this.ajaxScriptPath = ajaxScriptPath;
	this.currentOnObject = null;
	this.hintDiv = $("#" + hintDivID);
	this.lastHintDiv = null;
	this.canHideHint = true;
	this.delay = 100;
	this.cache = new Array();
	
}

HintsHandler.prototype.assignHintHover = function()
{
	var handler = this;
	this.getHintDiv().hover(
		function()
		{
			handler.canHideHint = false;
		},
		function()
		{
			handler.canHideHint = true;
			handler.currentOnObject = null;
			handler.getHintDiv().hide();
		});
};

HintsHandler.prototype.getHintDiv = function()
{
	return this.hintDiv;
};

HintsHandler.prototype.assignOnOvers = function()
{
	var handler = this;
	$(this.selectorString).each( function() { handler.assignHint($(this)); } );
}

HintsHandler.prototype.assignHint = function(object)
{
	var handler = this;
	object.hover(
		function()
		{
			handler.handleOn($(this), object);
		}, 
		function()
		{
			handler.handleOff($(this));
		}
	);
}

HintsHandler.prototype.handleOn = function(object, eObject)
{
	this.currentOnObject = object;
	this.assignHintHover();
	var handler = this;
	object.animate({left:'0'}, handler.delay);
	object.queue(function () {
		if ( handler.currentOnObject == object )
		{
			handler.showHint( object.attr("id"), eObject );
		}
		$(this).dequeue();
	});
}

HintsHandler.prototype.handleOff = function(object)
{
	var handler = this;
	handler.currentOnObject = null;
	object.stop();
	
	object.animate({left:'0'}, 10);
	object.queue(function () {
		if (handler.canHideHint) 
		{
			handler.getHintDiv().hide();
		}
		$(this).dequeue();
	});
	
}

HintsHandler.prototype.showHint = function(id, eObject)
{
	if ( typeof(this.cache[id]) == "undefined" )
	{
		var handler = this;
		$.ajax({
			metod: "post",
			url: handler.ajaxScriptPath,
			cache: false,
			data: "id="+parseInt(id),
			success: function(xml){
				handler.fillHint(xml);
				handler.cache[id] = xml;
			}
		});
		
	}
	else
	{
		this.fillHint(this.cache[id]);
	}
	var element = eObject.get(0);
	this.getHintDiv().css("left", this.getAbsolutePosition(element).x + 2 + 'px');
	this.getHintDiv().css("top", this.getAbsolutePosition(element).y + element.height + 4 +'px');
	this.getHintDiv().show();
}

HintsHandler.prototype.getAbsolutePosition = function (element) {
    var r = { x: element.offsetLeft, y: element.offsetTop };
    if (element.offsetParent) {
      var tmp = this.getAbsolutePosition(element.offsetParent);
      r.x += tmp.x;
      r.y += tmp.y;
    }
    return r;
  };

HintsHandler.prototype.fillHint = function(xml)
{
	var model = $("model", xml);
	if ( model.size() < 1 )
	{
		return;
	}
	this.getHintDiv().find("#number").text($("number", model).text());
	this.getHintDiv().find("#scale").text($("scale", model).text());
	this.getHintDiv().find("#firm").text($("firm", model).text());
	this.getHintDiv().find("#description").text($("description", model).text());
	this.getHintDiv().find("#category").text($("category", model).text());
	var price;
	price = parseInt($("price_uah", model).text())/currencyRate;
	price = price.toFixed(2);
	this.getHintDiv().find("#price_uah").text(price+' '+currencySufix);
	switch( $("presence", model).text() )
	{
		case "yes":
		  var presenceText = "Есть";
		  break;    
		case "no":
		  var presenceText = "Нет";
		  break;
		default:
		  var presenceText = "Можно заказать";
	}
	this.getHintDiv().find("#presence").text(presenceText);
}

//=================================================================================================================

$(document).ready( function()
{
	var discountHintHandler = new HintsHandler(ajaxScriptsPath+"getModelInfo.html", "modelInfo", "#discountsContainer .scrollBoxItem img");
	discountHintHandler.assignOnOvers();
	var hintTable = $("#table", discountHintHandler.getHintDiv());
	$("#loading", discountHintHandler.getHintDiv()).ajaxStart(function(){
   		$(this).show();
		hintTable.hide();
 	});
	
	$("#loading", discountHintHandler.getHintDiv()).ajaxStop(function(){
   		$(this).hide();
		hintTable.show(500);
 	});
	
	
	var catsHintHandler = new HintsHandler("", "", ".catsItem img.midImage");
	catsHintHandler.assignOnOvers();
	catsHintHandler.getHintDiv = function()
	{
		if (this.currentOnObject != null) 
		{
			var hintDiv = $("#" + parseInt(this.currentOnObject.attr("id")) + "cat");
			this.lastHintDiv = hintDiv;
		}
		else { var hintDiv = this.lastHintDiv; }
		return hintDiv;
	};
	catsHintHandler.showHint = function(id, eObject)
	{
		$("div[id$=cat]").hide();
		var element = eObject.get(0);
		this.getHintDiv().css("left", this.getAbsolutePosition(element).x + 2 + 'px');
		this.getHintDiv().css("top", this.getAbsolutePosition(element).y + element.height + 4 +'px');
		this.getHintDiv().show(100);
	};
		
	
	
	var newOnesHandler = new HintsHandler(ajaxScriptsPath+"getModelInfo.html", "modelInfo", "#newOnesContainer .scrollBoxItem img");
	newOnesHandler.assignOnOvers();
	
	var discountsScroll = new PictureScroller( $("#discountsBox"), 
											   $(".scrollBoxItem"), 
											   $("#discountsContainer"), 
											   $("#discountsLeft"), 
											   $("#discountsRight"),
											   ajaxScriptsPath+"getDiscounts.html" );
											   
	discountsScroll.handleServerResponse = function(xml)
	{
		var discounts = $("discount", xml);
		if ( discounts.size() < 1 )
		{
			this.stopQueringForContent = true;
			return;
		}
		var scroller = this;
		discounts.each( function()
		{
			var imagePath;
			if ( (imagePath = $("mainphotopathmid", $(this)).text()) == '' )
			{
				imagePath = mediaDir + "pics/nophoto.gif";
			}
			else
			{
				imagePath = photosPath + imagePath;
			}
			var image = $('<img id="'+$("id", $(this)).text()+'n" class="midImage"></img>').attr("src", imagePath);
			var scrollBoxItem = $('<div class="scrollBoxItem">');
			
			var prices = $('<div class="prices"></div>');
			var priceText = parseInt( $("price_uah", $(this)).text() )/currencyRate;
			
			switch( $("presence", $(this)).text() )
			{
				case "yes":
				  var presenceText = "Есть";
				  break;    
				case "no":
				  var presenceText = "Нет";
				  break;
				default:
				  var presenceText = "Можно заказать";
			}
			
			var priceCaption = $('<div class="priceCaption">' + presenceText + '</div>');
			
			var newPrice = $('<span class="message"></span>').text( priceText.toFixed(2) +' '+currencySufix);
			priceText = parseInt( $("discount_price_uah", $(this)).text() )/currencyRate;
			var oldPrice = $('<span class="oldPrice"></span>').text( priceText.toFixed(2) +' '+currencySufix);
			prices.append(oldPrice, $("<br/>"), newPrice);
			scrollBoxItem.append( image, priceCaption, prices );
			scroller.ribbon.append( scrollBoxItem );
		} );
		this.refreshRibbonSize();
		discountHintHandler.assignOnOvers();
		addInfoLinksToImgs("#discountsContainer .scrollBoxItem img");
	}											   
											   
	var newOnesScroll = new PictureScroller(  $("#newOnesBox"), 
											   $(".scrollBoxItem"), 
											   $("#newOnesContainer"), 
											   $("#newOnesLeft"), 
											   $("#newOnesRight"),
											   ajaxScriptsPath+"getNewOnes.html" );											   
	
	newOnesScroll.handleServerResponse = function(xml)
	{
		var newOnes = $("newone", xml);
		if ( newOnes.size() < 1 )
		{
			this.stopQueringForContent = true;
			return;
		}
		var scroller = this;
		newOnes.each( function()
		{
			var imagePath;
			if ( (imagePath = $("mainphotopathmid", $(this)).text()) == '' )
			{
				imagePath = mediaDir + "pics/nophoto.gif";
			}
			else
			{
				imagePath = photosPath + imagePath;
			}
			var image = $('<img id="'+$("id", $(this)).text()+'n" class="midImage"></img>').attr("src", imagePath);
			var scrollBoxItem = $('<div class="scrollBoxItem">');
			var prices = $('<div class="prices" style="width:132px; text-align:center;"></div>');
			switch( $("presence", $(this)).text() )
			{
				case "yes":
				  var presenceText = "Есть";
				  break;    
				case "no":
				  var presenceText = "Нет";
				  break;
				default:
				  var presenceText = "Можно заказать";
			}
			prices.text( presenceText );
			scrollBoxItem.append( image, prices );
			scroller.ribbon.append( scrollBoxItem );
		} );
		this.refreshRibbonSize();
		newOnesHandler.assignOnOvers();
		addInfoLinksToImgs("#newOnesContainer .scrollBoxItem img");
	}
	
	function addInfoLinksToImgs(selector)
	{
		$(selector).click( function()
		{
			var id = parseInt($(this).attr('id'));
			newwindow=window.open(infoScriptPath+",id-"+id+"/", "PopUp"+id,'height=600,width=600,left=0,top=0');
			if (window.focus) {newwindow.focus()}
		} );
	}
	
	$("#toBeginingDiscounts").click( function(){ discountsScroll.backToBegining(); $("#discountsRight").show(); } );
	$("#toBeginingNewOnes").click( function(){ newOnesScroll.backToBegining(); $("#newOnesRight").show(); } );
	
	addInfoLinksToImgs(".scrollBoxItem img");
	
});

