/**
 * Javascript/mootools for "Ukens Jobb"
 *
 * Author: Martin Aasen <martin.aasen@eddamedia.no>
 *
 */

(function(){

	var i = 0;
	var jsonitems = '';
	var taxonomy = '';
	var timer = 5;
	var periodical, dummy;
	var tindeurl = '';
	var siteid = '';
	
	var ukensjobbC2 = new Request({
		url: '/jsp/ukensjobb.jsp',
		method: 'get',
		
		onSuccess: function(responseText)
		{
			jsonitems = JSON.decode(responseText);

			tindeurl = jsonitems.tinde.url;
			taxonomy = encodeURIComponent(jsonitems.tinde.taxonomy);
			siteid = jsonitems.siteid;
			
			if (jsonitems.salesdep.url == "http://minside.innsida.no/" || jsonitems.salesdep.url == "http://minside.karmoylokal.no/") {
				jsonitems.salesdep.url = "http://minside.h-avis.no/"
			}
			
			$$('.heading').set('html', '<a href="' + tindeurl + '/stilling/lokalt"><img src="/img/ukensjobb/'+siteid +'_ledige_jobber.gif" alt=""></a>');
			
			if(jsonitems.ads.length > 0)
			{
				if (jsonitems.ads[i].image != '') {
					$('joblogo').set('html', '<a href="' + jsonitems.ads[i].url + '"><img src="http://files.tinde.no/image?src=' + jsonitems.ads[i].image + '&resize=150x90" alt=""></a>');
				} else {
					$('joblogo').set('html', '<a href="' + jsonitems.ads[i].url + '"><img src="http://files.tinde.no/image?src=http://www.tinde.no/images/design/jobb_noimage.png&resize=150x90" alt=""></a>');
				}
				$('jobtitle').set('html', '<a href="' + jsonitems.ads[i].url + '">' + jsonitems.ads[i].title + '</a>');
			}
			else
			{
				$('joblogo').set('html', '<a id="noads" href="' + jsonitems.salesdep.url + '">Vil du ha din annonse her? Kontakt salgsavdelingen</a>');
			}
			
			$('jobsearch').set('html', '<a href="' + tindeurl + '/stilling/lokalt">Søk i ledige jobber</a>');
			$('jobad').set('html', '<a href="' + jsonitems.salesdep.url + '">Sett inn annonse</a>');
			
			$('property').set('html', '<a href="' + tindeurl + '/eiendom/lokalt">Eiendom</a>');
			$('job').set('html', '<a href="' + tindeurl + '/stilling/lokalt">Jobb</a>');
			$('motor').set('html', '<a href="' + tindeurl + '/motors/lokalt">Motor</a>');
			$('classified').set('html', '<a href="' + tindeurl + '/marked/lokalt">Bruktmarked</a>');
			
			// If there are none or only one ad, set "Previous"/"Next" inactive.
			if(jsonitems.ads == undefined || jsonitems.ads.length == 1)
			{
				$('arrow_prev').removeClass('active');
				$('arrow_next').removeClass('active');
			}
			
			// Refresh logo and title using the timer interval.
			if(jsonitems.ads != undefined && jsonitems.ads.length >= 2)
			{
				var refresh = (function()
				{
					dummy = $time() + $random(0, 100);
					i++; if(i > jsonitems.ads.length-1) { i = 0; }
					
					if (jsonitems.ads[i].image != '') {
						$('joblogo').set('html', '<a href="' + jsonitems.ads[i].url + '"><img src="http://files.tinde.no/image?src=' + jsonitems.ads[i].image + '&resize=150x90" alt=""></a>');
					} else {
						$('joblogo').set('html', '<a href="' + jsonitems.ads[i].url + '"><img src="http://files.tinde.no/image?src=http://www.tinde.no/images/design/jobb_noimage.png&resize=150x90" alt=""></a>');
					}
					$('jobtitle').set('html', '<a href="' + jsonitems.ads[i].url + '">' + jsonitems.ads[i].title + '</a>');
				});
				periodical = refresh.periodical(timer * 1000, this);
			}
				
			// Previous
			$('prev').addEvent('click', function() {
				if(jsonitems.ads != undefined && jsonitems.ads.length >= 2)
				{
					if(i == 0) { i = jsonitems.ads.length-1; } else { i--; }
					
					if (jsonitems.ads[i].image != '') {
						$('joblogo').set('html', '<a href="' + jsonitems.ads[i].url + '"><img src="http://files.tinde.no/image?src=' + jsonitems.ads[i].image + '&resize=150x90" alt=""></a>');
					} else {
						$('joblogo').set('html', '<a href="' + jsonitems.ads[i].url + '"><img src="http://files.tinde.no/image?src=http://www.tinde.no/images/design/jobb_noimage.png&resize=150x90" alt=""></a>');
					}
					$('jobtitle').set('html', '<a href="' + jsonitems.ads[i].url + '">' + jsonitems.ads[i].title + '</a>');
				}
				return false;
			});
				
			// Next
			$('next').addEvent('click', function() {
				if(jsonitems.ads != undefined && jsonitems.ads.length >= 2)
				{
					if(i == jsonitems.ads.length-1) { i = 0; } else { i++; }
					
					if (jsonitems.ads[i].image != '') {
						$('joblogo').set('html', '<a href="' + jsonitems.ads[i].url + '"><img src="http://files.tinde.no/image?src=' + jsonitems.ads[i].image + '&resize=150x90" alt=""></a>');
					} else {
						$('joblogo').set('html', '<a href="' + jsonitems.ads[i].url + '"><img src="http://files.tinde.no/image?src=http://www.tinde.no/images/design/jobb_noimage.png&resize=150x90" alt=""></a>');
					}
					$('jobtitle').set('html', '<a href="' + jsonitems.ads[i].url + '">' + jsonitems.ads[i].title + '</a>');
				}
				return false;
			});
				
			// Tinde search (click)
			$$('.button').addEvent('click', function() {
				var querystring = $('querystring').value;
				window.location = tindeurl + '/sok/build?querystring=' + encodeURIComponent(querystring) + '&vertical=tinde-job';
			});
			
			// Tinde search (submit/enter)
			$('searchform').addEvent('submit', function(event) {
				new Event(event).stop();
				var querystring = $('querystring').value;
				window.location = tindeurl + '/sok/build?querystring=' + encodeURIComponent(querystring) + '&vertical=tinde-job';
			});
			
		}
		
	}); ukensjobbC2.send();
	
})();