function memberFavorite(action, model, location, obj, type) {
	if (action != 'add' && action != 'remove') {
		return false;
	}
	// itt kell kitenni a kisviragot
    $.ajax({
		url: '/favorite/' + action + '/' + model,
		type: "GET",
		dataType: "html",
		success: function(msg) {
			// kisviragot leszedni
			if (action == 'add') {
				if (type == 1) $(obj).replaceWith('<a title="Remove ' + model + ' from Favorites" onclick="memberFavorite(\'remove\', \''+ model + '\', \'fav' + model + '\', this, 1); return false;"><img alt="" src="/themes/generic/images/i_heart.png" /></a>');
				else if (type == 2) $(obj).replaceWith('<a title="Remove ' + model + ' from Favorites" onclick="memberFavorite(\'remove\', \''+ model + '\', \'fav' + model + '\', this, 2); return false;">NEM KEDVENC</a>');
			}
			if (action == 'remove') {
				if (location == 'favlist' + model) {
					$('#' + location).fadeTo(0.1, 0.25);
					$(obj).replaceWith('');
				} else {
					if (type == 1) $(obj).replaceWith('<a title="Add ' + model + ' to Favorites" onclick="memberFavorite(\'add\', \''+ model + '\', \'fav' + model + '\', this, 1); return false;"><img alt="" src="/themes/generic/images/i_add.png" /></a>');
					else if (type == 2) $(obj).replaceWith('<a title="Add ' + model + ' to Favorites" onclick="memberFavorite(\'add\', \''+ model + '\', \'fav' + model + '\', this, 2); return false;">KEDVENCEKHEZ AD</a>');
				}
			}
    	}
    });
}

function showSearch(show) {
	if (!show) {
		$('#quicksearch').css('display', 'none');
	} else {
		$('#quicksearch').css('display', 'block');
		document.getElementById('quicksearchinput').focus();
	}
}

function showInvite(modelname) {
	var cnf = confirm(modelname+ " invited you to join a Private Sex Show!");
	if (cnf) {
		document.location.href = "/model/privatechat/"+modelname;
	}
}

$(document).ready(function() {
	$('.galleries .gallerypages[class~="photo"] a.left').click(function() {
		var current = parseInt($(this).parent().find('.pagenum .current').text());
		var total = parseInt($(this).parent().find('.pagenum .total').text());
		if ((current == 1) || (current == 0)) return;
		$('.galleries .gallerybox[class~="photo"] #photopage'+current).removeClass('active');
		current--;
		$(this).parent().find('.pagenum .current').text(current);
		$('.galleries .gallerybox[class~="photo"] #photopage'+current).addClass('active');
	});
	$('.galleries .gallerypages[class~="photo"] a.right').click(function() {
		var current = parseInt($(this).parent().find('.pagenum .current').text());
		var total = parseInt($(this).parent().find('.pagenum .total').text());
		if (current == total) return;
		$('.galleries .gallerybox[class~="photo"] #photopage'+current).removeClass('active');
		current++;
		$(this).parent().find('.pagenum .current').text(current);
		$('.galleries .gallerybox[class~="photo"] #photopage'+current).addClass('active');
	});
	$('.galleries .gallerypages[class~="video"] a.left').click(function() {
		var current = parseInt($(this).parent().find('.pagenum .current').text());
		var total = parseInt($(this).parent().find('.pagenum .total').text());
		if ((current == 1) || (current == 0)) return;
		$('.galleries .gallerybox[class~="video"] #videopage'+current).removeClass('active')
		current--;
		$(this).parent().find('.pagenum .current').text(current);
		$('.galleries .gallerybox[class~="video"] #videopage'+current).addClass('active');
	});
	$('.galleries .gallerypages[class~="video"] a.right').click(function() {
		var current = parseInt($(this).parent().find('.pagenum .current').text());
		var total = parseInt($(this).parent().find('.pagenum .total').text());
		if (current == total) return;
		$('.galleries .gallerybox[class~="video"] #videopage'+current).removeClass('active');
		current++;
		$(this).parent().find('.pagenum .current').text(current);
		$('.galleries .gallerybox[class~="video"] #videopage'+current).addClass('active');
	});
	$('.videoplayer .pages a.left').click(function () {
		var current = parseInt($(this).parent().find('.pagenum .current').text());
		var total = parseInt($(this).parent().find('.pagenum .total').text());
		if ((current == 1) || (current == 0)) return;
		$(this).parent().parent().find('.active').removeClass('active');
		current--;
		$(this).parent().find('.pagenum .current').text(current);
		$(this).parent().parent().find('#page'+current).addClass('active');
	});
	$('.videoplayer .pages a.right').click(function () {
		var current = parseInt($(this).parent().find('.pagenum .current').text());
		var total = parseInt($(this).parent().find('.pagenum .total').text());
		if (current >= total) return;
		$(this).parent().parent().find('.active').removeClass('active');
		current++;
		$(this).parent().find('.pagenum .current').text(current);
		$(this).parent().parent().find('#page'+current).addClass('active');
	});
	if ($('#reviews').length > 0) {
		setInterval (function () {
			$.ajax({url: '/join/quotes/', success: function(data) {
				$('#reviews').html(data);
			}});
		}, 15000);
	}
	$(document).delegate('.bubbled', 'mouseenter', function (e) {
		if ($(this).find('span.bubble').length) {
			var bubble = $('<div id="bubble"></div>');
			bubble.html($(this).find('span.bubble').html());
			bubble.css('left', e.pageX+20).css('top', e.pageY+20);
			$(document.body).append(bubble);
		}
	});
	$(document).delegate('.bubbled', 'mousemove', function (e) {
		var bubble = $('#bubble');
		if (bubble.length) {
			bubble.css('left', e.pageX+20).css('top', e.pageY+20);
		}
	});
	$(document).delegate('.bubbled', 'mouseleave', function () {
		$('#bubble').remove();
	});
	$('.countdown').each(function () {
		var el = this;
		window.setTimeout(function () {
			countDown(el);
		});
	});
	$('img.blackpumpkin').each(function () {
		$(this).mouseenter(function (e) {
			$('#bubble').remove();
			var bubble = $('<div id="bubble"></div>');
			bubble.html($(this).attr('alt'));
			bubble.css('display', 'block');
			bubble.css('position', 'absolute');
			bubble.css('top', (e.pageY + 10));
			bubble.css('left', (e.pageX + 10));
			$(document.body).append(bubble);
		});
		$(this).mousemove(function (e) {
			var bubble = $('#bubble');
			if (bubble.length == 1) {
				bubble.css('top', (e.pageY + 10));
				bubble.css('left', (e.pageX + 10));
			}
		});
		$(this).mouseleave(function () {
			$('#bubble').remove();
		});
	});
});

function payfixed (jqel) {
	$('#payfixed').remove();
	var payfixed = $('<div></div>');
	payfixed.attr('id', 'payfixed');
	payfixed.append(jqel.clone());
	payfixed.css('opacity', '0');
	$(document.body).append(payfixed);
	payfixed.animate({opacity: 1});
	payfixed.click(function (e) {
		if ($(e.target).is('#payfixed')) {
			$(this).animate({opacity: 0}, function () {
				$(this).remove();
			});
		}
	});
}

function toggle(id) {
	var el = $('#'+id);
	if (el.css('display') == 'none') el.css('display', 'block');
	else el.css('display', 'none');
}

function mediapic(url) {
	if ($('#media img').length == 0) {
		$('#media').append('<img src="'+url+'" style="height: 500px; width: 620px; position: absolute; top: 0; left: 0;" onclick="$(\'#media #vodv\').css(\'display\', \'block\'); $(this).remove();" />');
	}
	else {
		$('#media img').attr('src', url);
	}
	$('#media .info').css('display', 'none');
	$('#media #vodv').css('display', 'none');
}

function mediavideo(price, vodid, title, time, length) {
	flashvars.price = price;
	flashvars.vodid = vodid;
	swfobject.embedSWF(gw+'/swf/vod/vod_dev.swf', 'vodv', '620', '500', '9.0.0', 'expressInstall.swf', flashvars, params, attributes);
	$('.h2wider .videodetails').html('Cím: <a class="nopointer">'+title+'</a> | Feltöltve: '+time+' | Hossz: '+length);
	$('#media .info').remove();
	var model = $('.h2wider .left a').text();
	var site = document.title.match(/.+ - ([^\s]+)$/)[1];
	document.title = title+' - '+model+' Felvett Videója (Feltöltve: '+time+') - '+site;
}

function videoerror(html, info) {
	if (!!html) $('#media').html(html+'<div id="vodv"></div>');
	var info = info || '&nbsp;';
	$('.videoplayer .bottom .info').html(info);
}

var messages = {
	"oldpass" : {"empty" : ["Kérlek írd be a régi jelszavadat.", 1], "wrong" : ["A régi jelszó hibás.", 1]}
	, "password" : {"short" : ["Az új jelszó túl rövid.", 1], "nomatch" : ["Az új jelszavak nem egyeztnek.", 1], "success" : ["A jelszó megváltozatása sikerült.", 0]}
	, "mail": {"success": ["A változtatás megtörtént.", 0]}
};

function saveSettings(content) {
	$.ajax({
		type: "POST",
		url: "/settings/save/" + content,
		data: $('#' + content).serialize(),
		dataType: 'json',
		success: function(msg){
			$('#' + content + 'status').empty();
			for (i in msg) {
				var text = messages[i][msg[i]][0];
				if (text == 'Settings changed successfully.') text = 'Beállítások sikeresen változtatva.';
				$('#' + content + 'status').append('<div class="' + ((messages[i][msg[i]][1] == 1) ? 'error' : 'success') + '"><p>' + text + '</p></div>');
			}
		}
	});
}

var javascript_countdown = function () {
	var time_left = 10; //number of seconds for countdown
	var output_element_id = 'javascript_countdown_time';
	var keep_counting = 1;
	var no_time_left_message = 'Frissíts!';
 
	function countdown() {
		if(time_left < 2) {
			keep_counting = 0;
		}
 
		time_left = time_left - 1;
	}
 
	function add_leading_zero(n) {
		if(n.toString().length < 2) {
			return '0' + n;
		} else {
			return n;
		}
	}
 
	function format_output() {
		var hours, minutes, seconds;
		seconds = time_left % 60;
		minutes = Math.floor(time_left / 60) % 60;
		hours = Math.floor(time_left / 3600);
 
		seconds = add_leading_zero( seconds );
		minutes = add_leading_zero( minutes );
		hours = add_leading_zero( hours );
 
		return hours + ':' + minutes + ':' + seconds;
	}
 
	function show_time_left() {
		document.getElementById(output_element_id).innerHTML = format_output();//time_left;
	}
 
	function no_time_left() {
		document.getElementById(output_element_id).innerHTML = no_time_left_message;
	}
 
	return {
		count: function () {
			countdown();
			show_time_left();
		},
		timer: function () {
			javascript_countdown.count();
 
			if(keep_counting) {
				setTimeout("javascript_countdown.timer();", 1000);
			} else {
				no_time_left();
			}
		},
		init: function (t, element_id) {
			time_left = t;
			output_element_id = element_id;
			javascript_countdown.timer();
		}
	};
}();

function zeroPad (num, length) {
	if (!length) return;
	if (!num) num = 0;
	var str = ''+num;
	while (str.length < length) {
		str = '0'+str;
	}
	return str;
}
function decreaseTime (h, m, s) {
	if (s == 0) {
		if (m == 0) {
			if (h == 0) return ['00', '00', '00'];
			return [zeroPad(h-1, 2), '59', '59'];
		}
		return [zeroPad(h, 2), zeroPad(m-1, 2), '59'];
	}
	return [zeroPad(h, 2), zeroPad(m, 2), zeroPad(s-1, 2)];
}
function countDown (el) {
	var remaining = $(el).text();
	if (!remaining) return;
	var matched = remaining.match(/(\d\d):(\d\d):(\d\d)/);
	if (!matched) return;
	var decreased = decreaseTime(parseInt(matched[1], 10), parseInt(matched[2], 10), parseInt(matched[3], 10));
	if (!decreased) return;
	$(el).text(zeroPad(decreased[0], 2)+':'+zeroPad(decreased[1], 2)+':'+zeroPad(decreased[2], 2));
	if ((decreased[0]+decreased[1]+decreased[2]) == 0) {
		$(el).trigger('countDown');
	}
	else {
		window.setTimeout(function () {
			countDown(el);
		}, 1000);
	}
}
