var count = 1;
var timer;
var prev_imgs;
var default_img = 2;
var cat_current = false;
var cat_images = null;
var tabs = ["tab-1","tab-2","tab-3","tab-4"];
function recordOutboundLink(link, category, action, type) {
	_gat._getTrackerByName()._trackEvent(category, action);
	if (typeof type == 'undefined') {setTimeout('document.location = "' + link.href + '"', 100);
	} else if (type == 'window') { window.open (link.href);	}
}

function submitFlag(id){
	var r = $.ajax({type:'GET',url:'/pages/flag.php?id='+id+'&flag='+document.getElementById('flag_videos').value+'&reason='+encodeURIComponent(document.getElementById('flag_reason').value),async:false});
	$('#flag_inner').html(r);
}

function addComment(id){
	var r = $.ajax({type:'GET',url:'/pages/comment.php?id='+id+'&comment='+encodeURIComponent(document.getElementById('comment_input').value),async:false});
	$('#comments').html(r['responseText']);
}

function rotateImage(id, prev_imgs) {
        count = count + 1;
        if (count > prev_imgs) {
		count = 1;
	}

        $('#image_' + id).attr ('src', 'http://images.2.bangyoulater.com/' + id + '/' + count + '.jpg');
        timer = setTimeout ('rotateImage (\'' + id + '\',\'' + prev_imgs + '\')', 500);
}

function stopRotate(id,end) {
        clearTimeout(timer);
        if (!end) {
		end = default_img
	};

        $('#image_' + id).attr ('src', 'http://images.2.bangyoulater.com/' + id + '/' + end + '.jpg');
        count = 1;
}

function rotateCategory(category,images) {
        if(cat_current === false) {
		cat_current = 1;
	} else {
                ++cat_current;
                if(cat_current > (images.length-1)) { cat_current = 0; }
        }
        document.getElementById(category).src = '/images/category/'+images[cat_current]+'.jpg';
        timer = setTimeout('rotateCategory(\''+category+'\',['+images.toString()+'])',500);
}

function stopCategoryRotate(category,image) {
        clearTimeout(timer);
        cat_current = false;
        cat_images = null;
        document.getElementById(category).src='/images/category/'+image+'.jpg';
}

function changeTab(tab) {
	for(var i in tabs) { document.getElementById(tabs[i]).style.display='none'; }
        document.getElementById('link-tab-1').className='tab tab-info';
        document.getElementById('link-tab-2').className='tab tab-embed';
        document.getElementById('link-tab-3').className='tab tab-flag';
        document.getElementById('link-tab-4').className='tab tab-comments';
        document.getElementById(tab).style.display='block';
        document.getElementById('link-'+tab).className=document.getElementById('link-'+tab).className+' active';
}

function addToFavorites(id) {
	var r = $.ajax({type:'GET',url:'/pages/favorite.php?favorite='+id,async:false});
	document.getElementById('favorite_text').innerHTML=r['responseText'];
}

function setRating(id,x) {
	if(id == 'voted') {
		var voted_orig = $('#votes_span').text();
		$('#votes_span').html('You have already voted!');
		return;
	} else if(id == 'voted_out') { 
		$('#votes_span').text(voted_orig);
		return; 
	}
	var xml = $.ajax({type:'GET',url:'/pages/vote.php?id='+id+'&rating='+x,async:false});
//	votes_span = $(xml).find('vote_text').text();
//	vote_status = $(xml).find('rating')text();
}

