$(function(){
	$('#slides').slides({
		preload: true,
		preloadImage: 'public/img/loading.gif',
		play: 5000,
		pause: 2500,
		hoverPause: true
	});
	
	// load special offers
	pagination(0, 4, 'index.php?controller=equipments&action=firstpage', 'os_items');
	
	// load second offers
	pagination(0, 10, 'index.php?controller=equipments&action=all', 'second_items');
	
	// lightbox 
	_lightbox();
	
	// tabs
	_tabs();
	
});

_tabs = function ()
{
	// Default Action
	$("div[id^='tabs']").each(function(index, el){
		$(el).find(".tab_content").hide();
		$(el).find("ul.tabs li:first").addClass("active").show(); //Activate first tab
		$(el).find(".tab_content:first").show(); // Show first tab content
		
		// On Click Event
		$(el).find("ul.tabs li").click(function() {
			$(el).find("ul.tabs li").removeClass("active"); //Remove any "active" class
			$(this).addClass("active"); //Add "active" class to selected tab
			$(el).find(".tab_content").hide(); //Hide all tab content
			activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
			$(activeTab).fadeIn(); //Fade in the active content
			return false;
		});
	});
}

_lightbox = function()
{
	$('a.lightbox').lightBox({
		imageLoading: 'public/img/lightbox/lightbox-ico-loading.gif',
		imageBtnClose: 'public/img/lightbox/lightbox-btn-close.gif',
		imageBtnPrev: 'public/img/lightbox/lightbox-btn-prev.gif',
		imageBtnNext: 'public/img/lightbox/lightbox-btn-next.gif',
		imageBlank: 'public/img/lightbox/lightbox-blank.gif'
	});
}

/**
 * @param int start - pagination start index
 * @param int nr_items - number of items
 * @param string url - the URL to load to
 * @param string el - the HTML element to load in
 * @usage pagination(0, 4, 'index.php?controller=news&action=firstpage', 'element_to_display');
 */
pagination = function(start, nr_items, url, el)
{
	var _url_ = url + '&start=' + start + '&nr_items=' + nr_items;
	$('#' + el).html('');
	
	$.get(_url_, function(response, status, xhr){
		if (status == "error") 
		{
			var msg = "Sorry but there was an error: ";
			$("#error").html(msg + xhr.status + " " + xhr.statusText);
			$("#error").show();
		}
		else
		{
			var ret = jQuery.parseJSON(response);
			
			if (el === 'os_items')
			{
				// format the response
				fso(ret.items);
			}
			else if (el === 'second_items')
			{
				// format the response
				fsi(ret.items);
				_tabs();
				_lightbox();
			}
			
			// add pagination link
			var nr_pages = Math.ceil(ret.all/nr_items);
			var current_page = (start + nr_items) / nr_items;
			
			var t_html = '<div class="clear-both"></div>';
			t_html += '<div id="os_nav">';
			
			if (current_page > 1)
			{
				t_html += '<span class="symbol" onclick="javascript:pagination(' 
					+ '0' + ', ' + nr_items + ', \'' + url + '\', \'' 
					+ el + '\');">&#171;&nbsp;</span>';
				
				t_html += '<span onclick="javascript:pagination(' 
					+ (start - nr_items) + ', ' + nr_items + ', \'' + url + '\', \'' 
					+ el + '\');">&nbsp;prev&nbsp;</span>';
			}
			
			var start_idx = 0;
			for (var i=1; i<=nr_pages; i++)
			{
				start_idx = (i-1) * nr_items;
				if (current_page == i)
				{
					t_html += '<span class="nav-selected"> '	+ i + ' </span>';
				}
				else
				{
					t_html += '<span onclick="javascript:pagination(' 
						+ start_idx + ', ' + nr_items + ', \'' + url + '\', \'' + el + '\');"> ' 
						+ i + ' </span>';
				}
			}
			
			if (nr_pages != current_page)
			{
				t_html += '<span onclick="javascript:pagination(' 
					+ (start + nr_items) + ', ' + nr_items + ', \'' + url + '\', \'' 
					+ el + '\');">&nbsp;next&nbsp;</span>';
				
				t_html += '<span class="symbol" onclick="javascript:pagination(' 
					+ ((nr_pages - 1) * nr_items) + ', ' + nr_items + ', \'' + url + '\', \'' 
					+ el + '\');">&nbsp;&#187;</span>';
			}
			
			t_html += '</div>';
						
			$('#' + el).append(t_html);
		}
	});
	
	$('#' + el + '-loading').ajaxStart(function() {
		$(this).show();
		$(this).css('z-index', '999').css('background-color', '#F5F5F5');
	}).ajaxStop(function() {
		$(this).hide();
		$('#' + el).fadeIn('slow');
	});
}

// display formated special offers on the first page
fso = function (o)
{
	var t_html = '';
	t_html += '<div class="clear-both"></div>';
	for (var i in o)
	{
		t_html += '<div class="os_item">';
		
		if (o[i].promote == 1)
		{
			t_html += '<img class="promote" src="public/img/os_promote.png" alt="image promote special offer normandia" />';
		}
		
		t_html += '<img src="public/img/equipments/second/' + i + '/thumb_img.jpg" alt="image special offer normandia" />';		
		t_html += '<h3>' +  o[i].categ  + " " + o[i].title + '</h3>';
		t_html += o[i].desc;
		t_html += '<div class="more-details">';
		t_html += '<a class="bold" href="index.php?action=view_equipment_detail&controller=index&id=' + i + '">';
		t_html += 'Mai multe detalii ...'; 
		t_html += '</a>';
		t_html += '</div>';
		t_html += '<div class="os_sep"></div>'
		t_html += '</div>';
	}
	$('#os_items').html(t_html);
}

//display formated seconds offers
fsi = function (o)
{
	var t_html = '';
	var cls = ''; 
	
	for (var i in o)
	{
		if (i & 1 == 1)
		{
			cls = 'left-side-item';
		}
		else
		{
			cls = 'right-side-item';
		}
		
		var img = o[i].image.replace('thumb_', '');
		t_html += '<div class="' + cls + '">';
		t_html += '<h2>' +  o[i].title + '</h2>';
		t_html += '<div style="margin-top: 10px;">';
		
		t_html += '<div id="tabs' + i + '">';
		t_html += '<ul class="tabs">'; 
		t_html += '<li><a href="#tab1' + i + '">Descriere</a></li>';
		t_html += '<li><a href="#tab2' + i + '">Specificații</a></li> ';
		t_html += '</ul>'; 
		t_html += '<div class="tab_container">'; 
		t_html += '<div id="tab1' + i + '" class="tab_content">';
		t_html += '<a class="lightbox" href="public/img/equipments/second/' + i + '/img.jpg">';
		t_html += '<img src="public/img/equipments/second/' 
				+ i + '/thumb_img.jpg" width="150" alt="Normandia echipament ' + o[i].title + '" />'
		t_html += '</a>';
		t_html += '<h3>' + o[i].categ + '</h3>';
		t_html += '<p>' + o[i].desc + '</p>';		
		t_html += '</div>';			
		t_html += '<div id="tab2' + i + '" class="tab_content">';
		t_html += '<p>' + o[i].specs + '</p>';
		t_html += '</div>';	
		t_html += '</div>';	
		t_html += '</div>';	
		t_html += '</div>';	
		t_html += '</div><!-- end tabs -->';
	}
	
	$('#second_items').html(t_html);
	t_html = '';
}
