var slideshow_count = 0;
var slideshow_data;

function initSlideShow(data)
{
    if (!data[slideshow_count])
    {
    	slideshow_count = 0;
    }
    
    //alert(data.length);
	   	
    $('#slideshow a.src').attr('href', data[slideshow_count]['link']).css({ 'background':'url(http://jobs.billwang.net/resizer/?f=' + data[slideshow_count]['image'] + '&w=300&h=230&v=31854e0bd26515599ea723cfab846db3) no-repeat', 'opacity': 0 }).animate({ opacity: 1 }, 1000);
    
    $('#slideshow a.title').attr('href', data[slideshow_count]['link']).text(data[slideshow_count]['title']);
	
    slideshow_count++;
    
    if (document.images && data[slideshow_count])	// Cache next image
    {
    	new Image().src = 'http://jobs.billwang.net/resizer/?f=' + data[slideshow_count]['image'] + '&w=300&h=230&v=31854e0bd26515599ea723cfab846db3';
    }
}

function getBlockList(data, ul_id)
{
	var o_class = '';
	var odd = '';
	var i_display = '';
	var html = '';
	
	$.each(data, function (i, a) {
		if (i == 1)
		{
			html = '<li class="no_' + i + ' ' + o_class + '"><div class="info"><span class="title"><a href="' + a['link'] + '">' + a['functions'] + '</a></span><span class="company"><div style="float:right; color: #FFF; font:bold 16px Verdana;">01</div>' + a['company'] + '</span></div><div class="logo"><a href="' + a['profile'] + '" target="_blank"><img src="' + a['avatar'] + '" alt="" /></a></div><div class="clear_right"></div></li>';
		}
		else
		{
			if (odd == true)
			{
				o_class = 'odd';
				odd = false;
			}
			else
			{
				odd = true;
				o_class = '';
			}
						
			if (i <= 9)
			{
				i_display = '0' + i;
			}
			else
			{
				i_display = i;
			}
			
			html += '<li class="no_' + i + ' ' + o_class + '" id="' + ul_id + '-list-' + i + '"><div class="hover" style="display:none"><div class="info"><span class="title"><a href="' + a['link'] + '">' + a['functions'] + '</a></span><span class="company"><div style="float:right; color: #F5F5F5; font:bold 16px Verdana;">' + i_display + '</div>' + a['company'] + '</span></div><div class="logo"><a href="' + a['profile'] + '" target="_blank"><img src="' + a['avatar'] + '" alt="" /></a></div><div class="clear_right"></div></div><div class="list" onmouseover="showListTab(\'' + ul_id + '\', \'' + ul_id + '-list-' + i + '\');"><span style="color: #666; font:bold 12px Verdana; float:right;">' + i_display + '</span><a href="' + a['link'] + '">' + a['functions'] + '</a><div class="clear_right"></div></div></li>';
		}
	});
	
	return '<ul id="' + ul_id + '" class="block-list">' + html + '</ul>';
}

function setTab(tabid, hideel, showel)
{
	$('#' + tabid + ' li').removeClass('selected');
		
	hideel.hide();
	showel.show();
}

function resetListTab(dataid)
{
	$('#' + dataid + ' li div.hover').hide();
	$('#' + dataid + ' li div.list').show();
}

function showListTab(dataid, targetid)
{
	resetListTab(dataid);
	
	$('#' + targetid + ' div.list').hide();
	$('#' + targetid + ' div.hover').show();
}

$(document).ready(function () {
	$('#lastnews').find('div.entry:last').css('border', 0);
	
	$('#slideshow').css('margin-top', '4px');
	
	new curvyCorners(radius_5px, document.getElementById('slideshow')).applyCornersToAll();
	
	$.getJSON('httprequest.php?act=recommendgallery&jsoncallback=?', function (data) {
		slideshow_data = data;
		
		initSlideShow(slideshow_data);
		
		setInterval(function () {
			initSlideShow(slideshow_data);
		}, 5000);
	});
	
	//new curvyCorners(radius_top_5px, 'title top_corner').applyCornersToAll();
	
	//new curvyCorners(radius_top_5px, document.getElementById('middle_nav')).applyCornersToAll();
	
	//$('#middle_nav').css({'border-bottom':'2px #363636 solid','margin-top':'15px'});
	
	/*if ($.browser.msie)
	{
		$('#top_news li div.title').css('padding', '3px 0 5px 0');
		
		$('#top_news').css('margin', '7px 0 0 0');
	}
	else
	{
		$('#top_news li div.title').css('padding', '1px 0 6px 0');
		
		$('#top_news').css('margin', '10px 0 0 0');
	}*/
	
	$.getJSON('http://jobs.billwang.net/output.php?act=recommend&type=job&f=home&format=json&jsoncallback=?', function (data) {
			
		$('#jobs_recommend').html(getBlockList(data, 'jobs-recommend-data-el'));
		
		$('#jobs_recommend ul').find('li:last').css('border', 0);

		showListTab('jobs-recommend-data-el', 'jobs-recommend-data-el-list-2');
	});
	
	$.getJSON('http://jobs.billwang.net/output.php?act=new&type=job&format=json&jsoncallback=?', function (data) {		
		$('#jobs_new').html(getBlockList(data, 'jobs-new-data-el'));
		
		$('#jobs_new ul').find('li:last').css('border', 0);
	});
	
	$.getJSON('http://jobs.billwang.net/output.php?act=company&type=recommend&format=json&jsoncallback=?', function (data) {
		var html = '';
			
		$.each(data, function (i, a) {
			html += '<li><div class="info"><span class="title"><a href="' + a['link'] +  '" target="_blank">' + a['company'] + '</a></span><span class="description">' + a['description'] + '</span></div><div class="logo"><a href="' + a['link'] + '" target="_blank"><img src="' +  a['avatar'] + '" alt="" /></a></div><div class="clear"></div></li>';
		});
		
		$('#recommend_company').html('<ul class="company">' + html + '</ul>');
		
		$('ul.company').find('li:last').css('border', 0);
	});
	
	$.getJSON('http://bbs.billwang.net/json.php?do=threads&type=hot&jsoncallback=?', function (data) {
		var html = '';
		
		$.each(data, function (i, a) {
			html += '<li><a href="' + a['forumlink'] + '" target="_blank" title="' + a['forum'] + '" style="color: #999">[' + a['forum'] + ']</a> <a href="' + a['link'] + '" target="_blank" title="' + a['title'] + '">' + a['title'] + '</a> <span style="font-size: 11px;">- By <a href="' + a['profile'] + '" target="_blank" style="color: #999">' + a['poster'] + '</a> ' + a['dateline'] + '</span></li>';
		});
		
		$('#hot_threads').html('<ul class="threads">' + html + '</ul>');
	});
	
	$.getJSON('http://bbs.billwang.net/json.php?do=threads&type=new&jsoncallback=?', function (data) {
		var html = '';
		
		$.each(data, function (i, a) {
			html += '<li><a href="' + a['forumlink'] + '" target="_blank" title="' + a['forum'] + '" style="color: #999">[' + a['forum'] + ']</a> <a href="' + a['link'] + '" target="_blank" title="' + a['title'] + '">' + a['title'] + '</a> <span style="font-size: 11px;">- By <a href="' + a['profile'] + '" target="_blank" style="color: #999">' + a['poster'] + '</a> ' + a['dateline'] + '</span></li>';
		});
		
		$('#new_threads').html('<ul class="threads">' + html + '</ul>');
	});
	
	if (!$.browser.msie)
	{
		$('div.sitesearch input.submit').css('margin', '0 0 0 8px');
	}
});