var myAlert = function (message) {
	alert(message);
};

var myConfirm = function (message) {
	return confirm(message);
};

var fnDisplayComments = function (divCommentsList, data) {
	divCommentsList.fadeOut('slow', function() {
		divCommentsList.empty();
		var i;
		for (i=0;i<data.length;i++) {
			divCommentsList.append(data[i]);
		}
		
		divCommentsList.fadeIn('slow');
	});
};

var comments = function() {
	var commentTA = $('div.comments form textarea');
	var exampleValue = 'Adaugă un comentariu ...';
	
	if (commentTA.val() == '') {
		commentTA.val(exampleValue);
		commentTA.addClass('example');
	}
	
	commentTA.click(function() {
		if ($(this).val() == exampleValue) {
			$(this).val('');
		}
		$(this).removeClass('example');
		$(this).siblings('button').show();
		$(this).addClass('comments_high');
	});
	
	commentTA.blur(function() {
		if ($(this).val() == '') {
			$(this).val(exampleValue);
			$(this).addClass('example');
			$(this).siblings('button').hide();
			$(this).removeClass('comments_high');
		}
	});

	$('div.comments form button').click(function (e) {
		var ta = $(this).siblings('textarea.comments');
		if (ta.val() == '') {
			ta.blur();
			return;
		}
		var action = $(this).parent().attr('action') + '.json';
		var dataString = $(this).parent().serialize();
		var divCommentsList = $(this).parents('div.comments').find('div.comments_list');
		$.ajax({
			cache : false,
			type : 'POST',
			url : action,
			data : dataString,
			dataType : 'json',
			success : function (data) {
				if (data === false) {
					myAlert('ooops! nu am putut adauga comentariul');
					return;
				}
				
				fnDisplayComments(divCommentsList, data);
			},
			error : function (o, txt, e) {
				myAlert('ooops! nu am putut adauga commentul');
			}
		});
		ta.val('');
		ta.blur();
	});
	
	var commentAct = $('div.comments a.action');
	
	commentAct.live('click', function() {
		var action = $(this).attr('href');
		var cid = $(this).attr('rel');
		var isDelete = false;
		var mesaj = 'Nu am putut valida comentariul';
		
		if ($(this).hasClass('delete')) {
			isDelete = true;
			mesaj = 'Nu am putut sterge comentariul';
			if (! myConfirm('Esti sigur ca vrei sa stergi comentariul?')) {
				return false;
			}
		}
		
		var divCommentsList = $(this).parents('div.comments').find('div.comments_list');
		var me = $(this);
		var me2 = me.parent('li');
		if (me2.length) {
			me = me2;
		}
		
		$.ajax({
			cache: false,
			url: action,
			success : function (data) {
				if (data === false) {
					myAlert(mesaj);
					return;
				}
				if (isDelete) {
					fnDisplayComments(divCommentsList, data);
				}
				else {
					me.remove();
				}
			},
			error : function (o, txt, e) {
				myAlert(mesaj);
			}
		});
		
		return false;
	});
}

/** search stuff **/
var searchfn = function () {
	var q = $('#q');
	var exampleValue = 'Caută pe ConsumRedus ...';
	
	q.click(function() {
		if ($(this).val() == exampleValue) {
			$(this).val('');
		}
	});
	
	q.blur(function() {
		if ($(this).val() == '') {
			$(this).val(exampleValue);
		}
	});
	
	q.keypress(function(e) {
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			$(this).parent().submit();
			return false;
		}
	});
	
	$('#qsubmit').click(function() {
		$(this).parent().submit();
		return false;
	});
}

var notificari = function(urlnotif) {
	var req = {
		url: urlnotif+'.json',
		cache: false,
		timeout: 30*1000,
		error: function() {},
		success: function(data) {
			var notifications = $('#notifications');
			if (data.length == 1) {
				notifications.attr('title', '1 notificare');
			}
			else if (data.length > 1){
				notifications.attr('title', data.length+' notificari');
			}
			
			if (data.length > 0) {
				notifications.text(data.length);
				notifications.addClass('active');
				notifications.click(function() {
					window.location.href = urlnotif;
				});
			}
		}
	};
	$.ajax(req);
	
	setTimeout(function() { notificari(urlnotif); }, 60*1000);
};

var xxxxx = function(urlx) {
	var req = {
		url: urlx+'.json',
		cache: false,
		timeout: 10 * 1000,
		error: function() {},
		success : function (data) {
			if (data) {
					window.location.reload();
			}
		}
	};
	setTimeout(function() {
		$.ajax(req);
	}, 60 * 1000);
}

var dropdownmenu = function() {
	$('#pageh li.menu').hover(function() {
		$(this).children('ul.submenu').slideDown().show();
		$(this).addClass('hover');
	}, function () {
		$(this).children('ul.submenu').slideUp().hide();
		$(this).removeClass('hover');
	});
}

var divscroll = function() {
	/** config **/
	var picHeight = 40;
	var paddingTop = 5;
	var paddingBottom = 5;
	var xPosition = 252;
		
	$("div.scrollclip div.scroll").hover(function() {
		$(this).addClass('scrollx');
	}, function() {
		$(this).removeClass('scrollx');
	});
	
	$("div.scrollclip div.scroll").scroll(function() {
        var oh=$(this)[0].scrollHeight;
        var h=$(this).height();
        var st=$(this).scrollTop();

        var yPosition = Math.ceil(st*(h-picHeight)/(oh-paddingTop-paddingBottom-h))+paddingTop;

        $(this).css('background-position', xPosition + 'px ' + yPosition + 'px');
	});
}

var flashmessage = function() {
	setTimeout(function() {
		$('#flashMessage').slideUp('slow');
	}, 7 * 1000);
}

var scrollable1 = function () {
	$('div.scrollable').scrollable({circular: true}).autoscroll(5000).navigator();
}

var imgtooltip1 = function () {
	$('img.hastooltip_left').not('.tooltip_initiated').tooltip({position: 'center left', effect: 'slide', tipClass: 'tooltip_thumbs_left'});
	$('img.hastooltip_left').not('.tooltip_initiated').addClass('tooltip_initiated');
}

var imgtooltip2 = function () {
	$('img.hastooltip_top').not('.tooltip_initiated').tooltip({position: 'top center', effect: 'slide', tipClass: 'tooltip_thumbs_top'});
	$('img.hastooltip_top').not('.tooltip_initiated').addClass('tooltip_initiated');
}

var showactions1 = function () {
/*	$('.showactions').hover(
		function() {$(this).find('div.actions').slideDown();},
		function() {$(this).find('div.actions').slideUp();}
	); */
}

var loginRequired = function (message) {
	$('#loginrequired h1').html('Sign-in pentru a continua.');
	
	$('#myaccount a').click();
}

var domainExtract = function (s) {
	var x = s.split(/:\/\//);
	var d = x[0];
	if (x.length == 2) {
		d = x[1];
	}
	d = d.split(/\//);
	return d[0];
}

var commaSplit = function ( val ) {
	return val.split( /,\s*/ );
}

var extractLast = function ( term ) {
	return commaSplit( term ).pop();
}

var autocompleteTags = function (urlx) {
	$( ".autocompletetags" )
	// don't navigate away from the field on tab when selecting an item
	.bind( "keydown", function( event ) {
		if ( event.keyCode === $.ui.keyCode.TAB &&
				$( this ).data( "autocomplete" ).menu.active ) {
			event.preventDefault();
		}
	})
	.autocomplete({
		source: function( request, response ) {
			$.getJSON( urlx + ".json", {
				term: extractLast( request.term )
			}, response );
		},
		search: function() {
			// custom minLength
			var term = extractLast( this.value );
			if ( term.length < 2 ) {
				return false;
			}
		},
		focus: function() {
			// prevent value inserted on focus
			return false;
		},
		select: function( event, ui ) {
			var terms = commaSplit( this.value );
			// remove the current input
			terms.pop();
			// add the selected item
			terms.push( ui.item.value );
			// add placeholder to get the comma-and-space at the end
			terms.push( "" );
			this.value = terms.join( ", " );
			return false;
		}
	});
}
