
App = {
	emptyFunction : function() {},
	pageName : '',

	initialise : function() 
	{
		if (typeof this.initors[this.pageName] == 'function') 
		{
			App.initors[this.pageName]();
		} 
		this.initSearch();
		this.initSideCarousel();
		$('.tabs').nextTabs();
		
		$('#sidebarVideos').nextMediaplayer({
			width : 278,
			height: 230
		});		
		
		var lastEditionUrl = $('#last_edition_url');
		if (lastEditionUrl.length > 0)
		{
			$('#articole_editie').attr('href', lastEditionUrl.val());
		}
	},
	
	initSideCarousel : function()
	{
		var slideshowIndex = $('#slideshowIndex');
		var sideCarousel = $('#sideCarousel');
		sideCarousel.jCarouselLite({
			btnNext: "#btnNext",
			btnPrev: "#btnPrev",
			circular: false,
			visible: 1,
			
			afterEnd : function(o)
			{
				var idx = $('img',o).eq(0).attr('alt');
				idx++;
				slideshowIndex.html(idx);				
			}
		});			
	},
		
	initSearch : function()
	{
		var searchSiteRadio = $('#search-okidoki-check-1');
		var searchOkidokiRadio = $('#search-okidoki-check-2');
		var searchInput = $('#searchQuery');
		searchInput.click(function()
		{
			if (this.value == 'introduceti termenii cautarii')
			{
				this.value='';
			}
		});
		$('#okidoki').submit(function()
		{
			searchQuery = searchInput.val();
			if (searchQuery != '')
			{
				if (searchSiteRadio.attr('checked') == true)
				{
					window.location = '/cauta?q=' + searchQuery;
					return false;
				}			
				else
				{
					window.location = 'http://okidoki.ro/search?query=' + searchQuery;
				}
			}
			else
			{
				alert('Introduceti termenii cautarii!');
			}
			return false;
		});	
	},
	initSlideshow : function()
	{
		var slideshowIndex = $('#mainSlideshowIndex');
		var mainCarousel = $('#mainCarousel');
		var slideTitles = $('#mainSlideshowTitles .title');
		var slides = $('#slidesList img');
		slides.click(function()
		{
			var idx = $(this).attr('alt');			
			//mainCarousel.get(0).go(idx);
			window.location.href = window.location.pathname + '#slide-'+(parseInt(idx)+1);
			window.location.reload();
		});
		
		mainCarousel.jCarouselLite({
			btnNext: "#btnNextMain",
			btnPrev: "#btnPrevMain",
			circular: false,
			visible: 1,
			
			afterEnd : function(o)
			{
				var idx = $('img',o).eq(0).attr('alt');
				idx++;
				slideshowIndex.html(idx);	
				slideTitles.hide();
				var currentSlide = slideTitles.eq(idx-1);
				var txt = currentSlide.text();
				if (txt != 'no title')
				{
					currentSlide.show();
				}
			}
		});	

		for (i = 1; i < 200; i++)
		{
			var hash = '#slide-' + i;
			if (window.location.hash == hash)
			{
				mainCarousel.get(0).go(i - 1);
			}
		}		
	},
	
	rand : function( min, max ) {
		return Math.floor(Math.random() * (max - min + 1)) + min;
	}	
}

App.initors = {
	'galerie_foto' : function()
	{
		App.initSlideshow();		
		$('#comments').nextComments();	
	},
	
	'slideshow' : function() 
	{
		App.initSlideshow();		
		$('#comments').nextComments();
		
	},
	
	'anuar' : function()
	{
		var form = $("#form_anuar");
		var url = $('#anuarEpaymentUrl').val();

		$('#tip_plata').change(function(){
			var orderType = $("#tip_plata").val();

			if(orderType == "op")
			{			
				form.show();
			}
			if(orderType == "online")
			{				
				if (null != url)
				{
					window.location = url;
				}
			}
			
			if(orderType == "-1")
			{			
				form.hide();
			}
		});
	},
	'articol' : function()
	 {
		$('#comments').nextComments();
	 },
	 
	 'articol_video' : function()
	 {
		$('#articleVideo').nextMediaplayer({
			width : 490,
			height: 405,
            autostart : true 
		});	
		$('#comments').nextComments();		
	 },
	 
	 'galerie_video' : function()
	 {
		$('#articleVideo').nextMediaplayer({
			width : 666,
			height: 455,
			autostart : true
		});		 
		$('#comments').nextComments();
	 }	 
};

/**** Legacy Functions ****/
function convert(downUp) {
	if (downUp) {
		var from_s = document.getElementById("to_s");
		var to_s = document.getElementById("from_s");
		var from_v = document.getElementById("to_v");
		var to_v = document.getElementById("from_v");
	} else {
		var from_s = document.getElementById("from_s");
		var to_s = document.getElementById("to_s");
		var from_v = document.getElementById("from_v");
		var to_v = document.getElementById("to_v");
	}
	var suma = (from_s.value*from_v.value)/to_v.value;
	if (isNaN(parseFloat(suma))) {
		to_s.value = 0;
	} else {
		to_s.value = suma.toFixed(4);
	}
}

/**** End Legacy Functions ****/

jQuery(document).ready(function() {	
	App.initialise();	
});