$(document).ready(function(){
	$('a[rel=external]').each(function() {
		this.target = "_blank";
	});
	
	$("div.tabs").tabs();
});

function scrollUp(obj, pixels) {
	if(parseInt($(obj).height()) + parseInt($(obj).css('top')) > 325) {
		$(obj).animate({'top': '-=' + 3*pixels + 'px'}, 'fast');
	}
}

function scrollDown(obj, pixels) {
	if(parseInt($(obj).css('top')) < 0){
		$(obj).animate({'top': '+=' + 3*pixels + 'px'}, 'fast');
	}
}

function showVideo(obj, autostart) {
	$(obj).parent().parent().children('li').children('a').children('img').each(function() {
		$(this).attr('src', baseURL + '/images/box-big-small.png');
		$(this).removeClass('activ');
	});
	
	file = $(obj).parent().children('a').attr('href');
	img = $(obj).parent().children('img').attr('src').match(/(.*)\.(gif|jpg|png)/i);
	text = $(obj).parent().children('a').metadata({type:'attr', name:'data'});
	
	$(obj).parent().children('a').children('img').addClass('activ');
	$(obj).parent().children('a').children('img').attr('src', baseURL + '/images/box-big-small-ov.png');
	
	$('div.videoplayer').children('a:last').html('<strong>' + text.artist + '</strong> ' + text.title);
	$('div.videoplayer').children('a:last').attr('href', text.url);
	
	$('#videoplayer').attr('href', baseURL + '/player.php?url=' + file);
	$('#videoplayer').media({ width: 520, height: 335, params: {  allowFullScreen: true, wmode: 'transparent' }, flashvars: { bufferlength: 25, image: img[0] + '?width=520', backcolor: '0xbfd681', frontcolor: '0x4b0084', autostart: autostart }});
	return false;
}

function refreshCaptcha(captcha) {
	test = false;
	$.ajax({
		url: baseURL + "/tools/refresh_captcha",
		type: "POST",
		data: "",
		success: function(data){
			data = eval("(" + data + ")");
	    	$("#captchaImage").attr('src', data['src']);
	    	$("#comment_captcha").val('');
		}
	});
	return test;
}

function checkCaptcha(captcha) {
	test = false;
	$.ajax({
		type: "POST",
		url: baseURL + "/tools/check_captcha",
		async: false,
		cache: false,
		data: "code=" + captcha,
		success: function(data) {
			data = eval("(" + data + ")");
			if(data['response'] == true) {
				test = true;
			} else {
				test = false;
			}
		},
		error: function(data) {
			test = false;
			}
	});
	return test;
}


function voteItem(groupId, templateId){
	$.ajax({
		type: 'POST',
		url: baseURL+'/tools/vote_item/',
		data: 'groupId='+groupId+'&templateId='+templateId,
		dataType: 'json',
		success: function(data){
			if(data.response == 'success'){ 
				$('#vote_' + groupId + ' span').html(data.item[0].votes + ' voturi');
				$('#vote_' + groupId + ' a').hide();
			}
		}
	});
}

function login() {
	$('#loginError').html('');
	$('#errMsg').css("display","none");
	$.ajax({
		type: "POST",
		url: baseURL + "/users/login",
		data: "login_username=" + $('#login_username').val() + "&login_password=" + $('#login_password').val(),
		success: function(data){
			data = eval("(" + data + ")"); 
			if(data['message'] == "success") {
				$('#username').html('<b>' + data['user'][0]['username'] + '</b>');
				$('#login').hide();
				$('#logout').show();
			} else {
				this.error(data);
			}
		},
		error: function(data) {
			if(data.message == "inactive"){
				$('#loginError').html("Contul dumneavoastra nu a fost activat inca. Va rugam sa urmati instructiunile din mailul trimis catre dvs.");
			} else if(data.message == "failure"){
				$('#errMsg').css("display","block");
			}
		}
	});

	return false;
}

function logout() {
	$.ajax({
		type: "POST",
		url: baseURL + "/users/logout",
		data: "",
		success: function(data){
			data = eval("(" + data + ")"); 
			if(data['message'] == "success") {
				$('#login').show();
				$('#logout').hide();
			}
		},
		error: function(data) {
		}
	});
	
	return false;
}

function clearTextBox(obj, string, autoselect) {
	if($(":input[id=" + obj + "]").val() == string) {
		$(":input[id=" + obj + "]").val("");
	} else if($(":input[id=" + obj + "]").val() == "") {
		$(":input[id=" + obj + "]").val(string);
	} else {
		if(autoselect == true) {
			$(":input[id=" + obj + "]").select();
		}
	}
}

function getUrl(url) {
	window.location.href = url;
}