jQuery.jHistory = {
	options: {},
	id: '',
	setEvents: function(){
		opj = jQuery.jHistory;
		cssid = opj.options.cssid;
		if(opj.options.minimize){
			jQuery('#MIN').click(function(){
				if (jQuery('#' + cssid + 'Content').css('display') != 'none') {
					jQuery('#' + cssid + 'Content').css('display','none'); 
				} else {
					jQuery('#' + cssid + 'Content').css('display','block'); 
				}
			});
		}
		if (opj.options.draggable) {
			jQuery('#'+cssid+'Box').makeDragable({
				handler: jQuery('#'+cssid+'Title')
			});
		}
		if (opj.options.addbutton) {
			jQuery('#ADD').click(function(){
				jQuery.jHistory.addHistory();
			});
		}
		if (opj.options.closable) {
			jQuery('#CLOSE').click(function(){
				jQuery('#'+ cssid + 'Box').css('display', 'none');
			});
		}
		if (opj.options.position) {
			jQuery('#POSITION').click(function(){
				jQuery.jHistory.removeposition();
			});
		}
		jQuery('#REM').click(function() { 
			jQuery.jHistory.removeHistory(); 
		});		
	},
	show: function(){
		opj = jQuery.jHistory;
		cssid = opj.options.cssid;
		if(jQuery('#'+cssid+'Box').css('display') == 'none'){
			jQuery('#'+cssid+'Box').css('display', 'block');
		}
	},
	hide: function(){
		opj = jQuery.jHistory;
		cssid = opj.options.cssid;
		if(jQuery('#'+cssid+'Box').css('display') != 'none'){
			jQuery('#'+cssid+'Box').css('display', 'none');
		}
	},
	showhide: function(){
		opj = jQuery.jHistory;
		cssid = opj.options.cssid;
		if(jQuery('#'+cssid+'Box').css('display') != 'none') {
			jQuery('#'+cssid+'Box').css('display', 'none');
		} else {
			jQuery('#'+cssid+'Box').css('display', 'block');
		}
	},
	removeposition: function(){
		opj = jQuery.jHistory;
		cssid = opj.options.cssid;
		jQuery('#'+cssid+'Box').css({
			top: opj.options.left,
			left: opj.options.top
		});
	},
	addHistory: function(link){
		cookie_old = jQuery.cookie('historyall');
		site = new Object();
		site['url'] = window.location.href;
		site['title'] = window.document.title;
		if (jQuery.jHistory.options.saveTitleLink) {
			var id = link.title;
			if(cookie_old != 0) {
				if(cookie_old.length >= 1){
					arr = cookie_old.split(";");
					var find = false;
					jQuery.each(arr, function(k, item){
						if (id == item) {
							find = true;
						}
					});
					if(!find){
						id = cookie_old + ";"+id;
					} else {
						id = cookie_old;
					}
				} else {
					id = cookie_old + ";"+id;
				}
			}
			jQuery.cookie('historyall', id, {
				expires: 356
			});
			jQuery().jAlert(lang.history_save);
		}
		else {
			if (cookie_old == 0) {
				list = '1';
				jQuery.cookie('historyall', list, {
					expires: 356
				});
				jQuery.cookie('history_1', jQuery.toJSON(site), {
					expires: 356
				});
				jQuery().jAlert(lang.history_save);
			}
			else {
				counter = 0;
				if (cookie_old != 1) {
					arr = cookie_old.split(";");
					big = 0;
					
					jQuery.each(arr, function(k, item){
						if (parseInt(item) > big) {
							big = item;
						}
						counter++;
					});
					big = parseInt(big) + 1;
				}
				else {
					big = parseInt(cookie_old) + 1;
				}
				list = cookie_old + ";" + big;
				
				if (counter < 40) {
					jQuery.cookie('historyall', list, {
						expires: 356
					});
					jQuery.cookie('history_' + big, jQuery.toJSON(site), {
						expires: 356
					});
					jQuery().jAlert(lang.history_save);
				}
				else {
					jQuery().jAlert(lang.history_too_much_cookies);
				}
			}
			if (jQuery.jHistory.id != ''  && !jQuery.jHistory.options.saveTitleLink ) {
				jQuery.jHistory.getHistory();
			}
		}
	},
	removeHistory: function(link){
		cookie_old = jQuery.cookie('historyall');
		if (cookie_old != 0) {
			if (jQuery.jHistory.options.saveTitleLink) {
				list = "";
				if(cookie_old.length > 1){
					arr = cookie_old.split(";");
					jQuery.each(arr, function(k, item){
						if (link.title != item) {
							list += item + ";";
						}
					});
					jQuery.cookie('historyall',list.substr(0, list.length - 1), {
						expires: 356
					});
				} else {
					jQuery.cookie('historyall', 0, {
						expires: 356
					});
				}
				window.location.reload();
			}
			else {
				if (cookie_old != 1) {
					arr = cookie_old.split(";");
					list = "";
					jQuery.each(arr, function(k, item){
						if (jQuery('#history_' + item + ':checked').length > 0) {
							jQuery.cookie('history_' + item, null);
							if (jQuery.jHistory.id != '') {
								jQuery('#checkBox_history_' + item).remove();
								jQuery('#historyBox_history_' + item).remove();
							}
						}
						else {
							list += item + ";";
						}
					});
					jQuery.cookie('historyall', list.substr(0, list.length - 1), {
						expires: 356
					});
				}
				else {
					if (jQuery('#history_' + item + ':checked').length > 0) {
						jQuery.cookie('history_' + cookie_old, null);
						jQuery.cookie('historyall', 0, {
							expires: 356
						});
						if (jQuery.jHistory.id != '') {
							jQuery('#checkBox_history_' + cookie_old).remove();
							jQuery('#historyBox_history_' + cookie_old).remove();
						}
					}
				}
			}
		}
	},
	getHistory: function(){
		cookie_old = jQuery.cookie('historyall');
		if(cookie_old != 0 && !jQuery.jHistory.options.saveTitleLink){
			function setHistory(item){
				opj = jQuery.jHistory;
				cssid = opj.options.cssid;
				
				checkBox = jQuery('<div></div>');
				checkBox.attr({
					id: 'checkBox_history_'+item
				}).addClass('Check').appendTo('#'+cssid+'Content');
								
				check = jQuery('<input type="checkbox"/>');
				check.attr({
					name: 'history_'+item,
					id: 'history_'+item,
					value: item
				}).appendTo('#checkBox_history_'+item);
																					
				histBox = jQuery('<div></div>');
				histBox.addClass('Title')
					.attr('id','historyBox_history_'+item)
					.css('width',opj.options.width-30)
					.appendTo('#'+cssid+'Content');
				
				historyLink = jQuery('<a></a>');
				historyLink.attr({
					href: jsonCookie.url,
					target: '_self'
				}).html(jsonCookie.title).appendTo('#historyBox_history_'+item);
				
				clear = jQuery('<div></div>');
				clear.css('clear','both').appendTo('#'+cssid+'Content');
				//jQuery('#'+jQuery.jHistory.options.cssid+'Content').append(checkBox).append(histBox).append(clear);
			}
			
			jQuery('#'+jQuery.jHistory.options.cssid+'Content').empty();
			if (cookie_old != 1) {
				arr = cookie_old.split(";");
				jQuery.each(arr, function(k, item){
					jsonCookie = jQuery.parseJSON(jQuery.cookie('history_'+item)); 
					setHistory(item);						
				});	
			}
			else {
				jsonCookie = jQuery.cookie('history_'+cookie_old);
				jsonCookie = jQuery.parseJSON(jsonCookie); 
				setHistory(cookie_old);					
			}
		}
	}
};
jQuery.fn.extend({
	jHistory: function(id, options){
		options = jQuery.extend({
			width:			300,
			top:			0,
			left:			0,
			title:			'Merkzettel',
			cssid:			'history',
			showTitle:		true,
			hiding:			false,
			addbutton:		true,
			closable:		false,
			minimize:		false,
			position:		false,
			draggable: 		false,
			saveTitleLink:  false
		}, options || {});	
		cssid = options.cssid;
		titlewidth = 0;
		jQuery.jHistory.options = options;
		jQuery.jHistory.id = id;
		
		if(jQuery(id)  && !jQuery.jHistory.options.saveTitleLink){
			jel = jQuery('<div></div>');
			jel.attr('id',cssid + 'Box').css({
				width: options.width+'px',
				top: options.top,
				left: options.left,
				display: 'block'
			}).appendTo(jQuery(id));
			
			jhead = jQuery('<div></div>');
			jhead.attr('id',cssid+'Head').css({
				width: options.width
			}).appendTo(jel);
			
			jtitle = jQuery('<div></div>');
			jtitle.attr('id',cssid+'Title');
			if (options.showTitle) {
				jtitle.html('<h4>'+options.title+'</h4>');
			}
			if (!options.draggable) {
				jtitle.css('cursor','default');
			}
			jmin = jQuery('<div></div>');
			jmin.attr('id','MIN').addClass(cssid+'Action').html('_');
			
			jclose = jQuery('<div></div>');
			jclose.attr('id','CLOSE').addClass(cssid+'Action').html('x');			
			
			jposition = jQuery('<div></div>');
			jposition.attr('id','POSITION').addClass(cssid+'Action').html('^');			
			
			jcontent = jQuery('<div></div>');
			jcontent.attr('id',cssid+'Content').css('width',parseInt(options.width) - 10);				
			
			jfooter = jQuery('<div></div>');
			jfooter.attr('id',cssid+'Footer').css('width',options.width);		
			
			jadd = jQuery('<div></div>');
			jadd.attr('id','ADD').addClass(cssid+'Action').css('color','#007e09').html('+');			
			
			jrem = jQuery('<div></div>');
			jrem.attr('id','REM').addClass(cssid+'Action').css('color','#ac0000').html('-');						
			
			jtitle.appendTo(jhead);
			
			if(options.closable){
				jclose.appendTo(jhead);
				titlewidth += 20;
			}
			
			if(options.minimize){
				jmin.appendTo(jhead);
				titlewidth += 20;
			}
			
			if(options.position && options.draggable){
				jposition.appendTo(jhead);
				titlewidth += 20;
			}
			
			if(options.addbutton){
				jadd.appendTo(jfooter);
			}
		
			if(options.showTitle || options.draggable || (options.position && options.draggable) || options.minimize || options.closable){
				jtitle.css(parseInt(options.width)- (titlewidth + 5));
				jhead.appendTo(jel);
			}
		
			jrem.appendTo(jfooter);
			
			jcontent.appendTo(jel);
			jfooter.appendTo(jel);
					
			jQuery.jHistory.setEvents.apply(jel);
		}
		cookie_old = jQuery.cookie('historyall');
		if(!cookie_old){
			jQuery.cookie('historyall',0,{expires: 356});
		}
		else{
			if (jQuery(id) && !jQuery.jHistory.options.saveTitleLink) {
				jQuery.jHistory.getHistory();
			}
		}
	}
});
jQuery.jAlert = {
	fading: function(){
		if(jQuery(this).css('display') == 'none'){
			jQuery(this).css('display','block').animate({
				opacity: 1
			},2000);
		} else {
			jQuery(this).animate({
				opacity: 0
			}, 2000, function(){
				jQuery(this).css('display','none');
			});
		}		
	},
	setEvents: function(){
		jQuery('div.window_tr',this).click(function(){
			jQuery.window.close.apply(jQuery(this).parent('div'));
		});
		jQuery(this).makeDragable({
			handler: jQuery('.window_tm',this).css('cursor','move'),
			opacity: 0.8
		});
		var that = this;
		window.setTimeout(function(){jQuery.jAlert.close.apply(that);},3000);
	},
	close: function(){
		jQuery(this).animate({
			opacity:0
		},2000,function(){
			jQuery(this).remove();
		});
	}
};
jQuery.fn.extend({
	jAlert: function(text){
		ele = jQuery('<div></div>');
		elemente = ['tl', 'tm', 'tr', 'content', 'bl', 'bm', 'br'];
		width = 306;
		height = 135;
		title = window.document.title;
		var left = ((jQuery(window).width() / 2) +jQuery().scrollLeft()) - (parseInt(width) / 2);
		var top = ((jQuery(window).height() / 2) +jQuery().scrollTop()) - (parseInt(height) / 2);
		
		var rand = Math.round((Math.random() * 1000));
		ele.css({
			position: 'absolute',
			left: left,
			top: top,
			zIndex: 900,
			opacity: '0',
			display: 'none',
			width: width,
			height: height 
		}).attr('id', 'alert_'+rand);
		jQuery.each(elemente, function(v, k){
			switch (k) {
				case 'tl':
				case 'tr':
				case 'bl':
				case 'br':
					e = jQuery('<div></div>');
					e.attr('class', 'window_' + k).appendTo(ele);
					break;
				case 'tm':
					e = jQuery('<div></div>');
					e.attr('class', 'window_' + k).css({
						width: (parseInt(width) - 40) + 'px'
					}).appendTo(ele).html(title);
					break;
				case 'bm':
					e = jQuery('<div></div>');
					e.attr('class', 'window_' + k).css({
						width: (parseInt(width) - 40) + 'px'
					}).appendTo(ele);
					break;
				case 'content':
					img = '<img src="design/img/ok.gif" />';
					e = jQuery('<div></div>');
					e.css('clear', 'both').appendTo(ele);
					e = jQuery('<div></div>');
					e.attr('class', 'window_' + k).css({
						width: (parseInt(width) - 2) + 'px',
						height: (parseInt(height) - 43) + 'px',
						textAlign: 'center',
						fontWeight: 'bold',
						overflow: 'hidden',
						minHeight: '0px'
					}).html("<br>"+img+"<br><br>"+text).appendTo(ele);
					break;
			}
		});
			
		ele.appendTo('body');
		jQuery.jAlert.fading.apply(ele);
		jQuery.jAlert.setEvents.apply(ele);
	}
});

