$(document).ready(function(){

	init();
	
	// HUR DOPPAR DU TRIGGER BARA PÅ SIDA 1
	if ($('.current').html() == "1") {
		$("#dopp").fancybox().trigger('click');
	}
	// END HUR DOPPAR DU

});

function init()
{
	// vote links
	$('.reportlink a').each(function(){
		$(this).click(function(e){
			e.preventDefault();
			$.get($(this).attr('href')+'&isajax=1', function($data){
				if($data=='1')
				{
					alert('Bidraget har anmälts.');
				}
			});
		})
	})
	
	// report links
	$('.voteup a').each(function(){
		$(this).click(function(e){
			e.preventDefault();
			$id = $(this).attr('id');
			$id = $id.replace('votelink-','');
			$.get($(this).attr('href')+'&isajax=1', function($data){
				if($data != '0')
				{
					$o = $('#votes-'+$id);
					// update numer of votes
					//$o.html( parseInt('0'+$o.html())+1 );
					$o.html( $data+'' );
					// notify
					alert('Tack för din röst!');
				}
			});
		})
	})
	/*
	$('.pagenavi a').each(function(){
		$(this).click(function(e){
			e.preventDefault();
			$('.pagenavi a.current').removeClass('current');
			$(this).addClass('current');
			$(this).html('<span style="position:absolute;"><img style="position:relative;left:-5px;" src="i/ajax-loader-pagination.gif" alt="Loading..." /></span>&nbsp;&nbsp;');
			$.get($(this).attr('href')+'&isajax=1', function($data){
				$('#maincontent').html($data);
				init();
			});
		})
	});
	*/
	// Add validation to all form that has the classname "validate"
	$('form.validate').each(function(index) {
		$(this).validation();
	});

	$("a.external").attr("target","_blank");
	
	$("a.newwinlink").click(function(e){
		e.preventDefault();
		window.open ($(this).attr("href"),"facebook","width=800,height=600"); 
	});
	
	$("a.tellafriendlink").each(function(){
		$(this).click(function(e){
			e.preventDefault();
			$id = $(this).attr("id");
			$id = $id.split('-')[1];
			$('#tellafriend-'+$id).fadeIn();
		});
	});
	
	$("form.tellafriendform").bind('submit',function(e){
		e.preventDefault();
		$onamn = $(this).find('input[name="namn"]');
		$oemail = $(this).find('input[name="email"]');
		$namn = $($onamn).attr("value");
		$email = $($oemail).attr("value");
		$id = $(this).attr("id");
		$id = $id.split('-')[1];
		$.get('app.php?action=tellafriend&email='+escape($email)+'&namn='+escape($namn)+'&id='+$id+'&isajax=1', function($data){
			if($data=='1')
			{
				alert('Ett e-postmeddelande har skickats till din vän.');
				$namn = $($onamn).attr("value","");
				$email = $($oemail).attr("value","");
				$('#tellafriend-'+$id).fadeOut();
			}
			else
			{
				alert('E-post kunde inte skickas. Vänligen försök igen.');
			}
		});
	});	
}

function recordStarted()
{
	// when pressing start on the recorder
}
function recordStop()
{
	// when pressing stop on the recorder
}
function recordDeleted()
{
	// when pressing delete on the recorder
}
function recordSave()
{
	// when pressing save on the recorder
}
function recordDone(username, user_video_number, video_id)
{
	// after the video has been saved and uploaded
	$.get('webcam_form.php?videoid='+video_id+'&isajax=1', function($data){
		$('#recorderform').html($data);
	});
	//self.location.href='webcam_form.php?videoid='+video_id;
}


/**
 * String prototypes
 */
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,''); }; 
String.prototype.empty = function() {
	var temp = this.trim();
	temp = this.replace(/\n/g,'');
	temp = this.replace(/\l/g,'');
	temp = this.replace(/\t/g,'');
	return temp == '';
};
/**
 * Return tagName on a jQuery object
 */
$.fn.tagName = function() {
    return this.get(0).tagName;
}