/**
 * @author Arturas Paleicikas arturas dot paleicikas [eta] metasite dot net
 */
var MobileSingle = {
	inCompare: {},

	init: function(dataType){
		this.dataType = dataType;
		this.getCompare();

        this.initExpertForm($('.__callExpertForm'), $('#deviceExpertForm'));
	},

    initExpertForm: function($call, $obj){
        var self = this;
        
        $call
            .click(function(){
                window.Call._popup(400, $obj.html());                
                return false; 
            });
    },

	addForCompare: function(deviceId, deviceImg, translations){
		MSIM = this;
		MSIM.deviceId = deviceId;
		MSIM.deviceImg = deviceImg;
		MSIM.translations = translations;
		MSIM.validateDeviceCompare();
	},

	validateDeviceCompare: function(){
		var MSIM = this;
		var element = $('li.toolBoxSimpleCompare');
		if(MSIM.deviceId && MSIM.deviceId > 0 && !window.MobileSingle.inCompare[MSIM.deviceId]){
			element.show();
			element.find('a').click(function(){
				MSIM.dragIn(MSIM.deviceId, MSIM.deviceImg);
				return false;
			});
		} else {
			element.hide();
		}
	},
		 
	getComparePath: function(path){
		MSIM = this;
		var c = 0;
		var arr = [];
		$.each(MSIM.inCompare, function(i, n){
			c++;
			arr[arr.length] = 'd'+c+'='+i;
		});

		if(arr.length == 0)
		{
			alert(MSIM.translations['empty_compare']);
			return false;
		} else {
			window.location.href=path+'?'+arr.join('&');
			return;
		}
		
	},

	saveCompare: function(){
		MSIM = this;
		var arr = [];
		$.each(window.MobileSingle.inCompare, function(i,n){
			arr[arr.length] = n.id+'|'+n.img+'|'+MSIM.dataType;
		});

		$.cookie('mobileInCompare', arr.join(']['), {path: '/'});
	},

	getCompare: function(){
		var MSIM = this;

		var data = $.cookie('mobileInCompare');
		//console.log(data);

		if(data){
			var gsplit = data.split('][');
			$.each(gsplit, function(i,n){
				var split = n.split('|');
				if(split[0] && split[1] && split[2]){

					var id = split[0];
					var imgSrc = split[1];
					var dataType = split[2];
					
					//console.log(dataType+' '+MSIM.dataType);
				
					if(dataType == MSIM.dataType){
						window.MobileSingle.inCompare[id] = {'id':id, 'img':imgSrc, 'type':dataType};
						$('#compBoxLister')
							.append('<div class="compareBox" id="compareBoxElement'+id+'"><img src="'+imgSrc+'" alt="" width="65" height="75" title="" /><a href=""><img src="images/common/mobile/ico.delete.gif" alt="" class="delete" /></a></div>')
								.find('div.compareBox:last')
								.fadeIn(800)
								.find('a')
									.bind('click', function(){
										var parentNode = this.parentNode;
										//$(parentNode).SlideOutLeft(400, function(){
											//$(this).remove();
											$(parentNode).remove();
											if(window.MobileSingle.inCompare[id]){
												delete window.MobileSingle.inCompare[id];
												MSIM.saveCompare();
												MSIM.validateDeviceCompare();
											}
										//});

										return false;
									});
					}						
				}		
			});
		}
	},

	dragIn: function(deviceId, deviceImg){
		MSIM = this;
		var isInComp = $('#compareBoxElement'+deviceId);

		if(isInComp.size() == 0){
			var $list = $('#compBoxLister div.compareBox');
			if($list.length < 3){
				
				window.MobileSingle.inCompare[deviceId] = {'id':deviceId, 'img':deviceImg, 'type':MSIM.dataType};
				window.MobileSingle.saveCompare();

				$('li.toolBoxSimpleCompare').hide();
				$('#compBoxLister')
					.append('<div class="compareBox" id="compareBoxElement'+deviceId+'"><img src="'+deviceImg+'" alt="" width="65" height="75" title="" /><a href=""><img src="images/common/mobile/ico.delete.gif" alt="" class="delete" /></a></div>')
						.find('div.compareBox:last')
						.fadeIn(800)
						.find('a')
							.bind('click', function(){
								var parentNode = this.parentNode;
								//$(parentNode).SlideOutLeft(400, function(){
										//$(this).remove();
										$(parentNode).remove();
										if(window.MobileSingle.inCompare[deviceId]){
											delete window.MobileSingle.inCompare[deviceId];
											window.MobileSingle.saveCompare();
											$('li.toolBoxSimpleCompare').show();
										}
								//});

								return false;
							});
				//setTimeout(function(){$('li.toolBoxSimpleCompare a').DraggableDestroy();}, 0);
			} else { alert(MSIM.translations['can_compare_only_3_devices']); }
		} else { alert(MSIM.translations['device_already_exists_in_compare']); }
	}
}
