/**
 * IDMultimedia JavaScript public scripts
 * 
 * @author Bartłomiej Marciniuk (redliquid)
 * @version 0.1
 */

var hostName = 'http://'+window.location.hostname+'/';
var siteWidth = 0;
var siteHeight = 0;
var menuHeight = 44;
var newsHeight = 0;
var boxTop = 70;

function validateProperties()
{
	
	function renderPhotoGallery()
	{
		var data = 'page='+$('#pageNr').val()+'&rowCount=11';

		$.ajax({
			url: hostName + 'galeria-foto',
			cache: false,
			type: "POST",
			data: data,
			success: function(data){
				data = $.JSON.decode(data);
				$('#photoContainer').html(data['photoGallery']);
				$('#photoContainer').fadeTo(1);
				$('#pages').html(data['currentPage'] + ' / '+data['totalPages']);
				$('#totalPages').val(data['totalPages']);

				// correct next/prev buttons
				if (data['currentPage'] == data['totalPages']) $('.nextNews').hide();
				else $('.nextNews').show();
				if (data['currentPage'] == 1) $('.prevNews').hide();
				else $('.prevNews').show();
			},
			beforeSend: function(XMLHttpRequest){
				$('#photoContainer').fadeTo(0.3);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				alert(textStatus);
			}
		});
		
	}

	/*
	switch(controller)
	{
		case 'news': renderNews(); break;
		case 'galeria-foto': renderPhotoGallery(); break;
	}*/
	
}

$(document).ready(function(){
	// enviroment vars
	siteWidth = $(document).width();
	siteHeight = $(document).height();
	
	// window resize event for update browser size
	var resizeTimer = null;
	$(window).bind('resize', function() {
		if (resizeTimer) clearTimeout(resizeTimer);
		resizeTimer = setTimeout(function(){
			siteWidth = $(document).width();
			siteHeight = $(document).height();
			
			validateProperties();
			
		}, 100);
	});
	
	// Cufon initialization
	Cufon.replace('#menu li a', {
		color: '#c6c6c6',
		fontFamily: 'Malgun Gothic',
		hover: {
			color: '#ff7611'
		}
	});
	Cufon.replace('#menu li a.active', {
		color: '#ff7611',
		fontFamily: 'Malgun Gothic',
		hover: {
			color: '#ff7611'
		}
	});	
	Cufon.replace('div.authors a', {
		color: '#c6c6c6',
		fontFamily: 'Malgun Gothic',
		hover: {
			color: '#ff7611'
		}
	});	
	
	Cufon.replace('.box .inner h1', {
		color: '#fff',
		fontFamily: 'Malgun Gothic',
		fontSize: '30px'
	});

	Cufon.replace('.boxSmall .inner h1', {
		color: '#fff',
		fontFamily: 'Malgun Gothic',
		fontSize: '30px'
	});
	Cufon.replace('.boxSmall2 .inner h1', {
		color: '#fff',
		fontFamily: 'Malgun Gothic',
		fontSize: '30px'
	});
	Cufon.replace('.boxSmall .inner h2', {
		color: '#fff',
		fontFamily: 'Malgun Gothic',
		fontSize: '16px'
	});
	// validate all properties (width, height and other things)
	validateProperties();
	
	// preloader initialization
	//QueryLoader.selectorPreload = "body";
	//QueryLoader.init();
	
});

$(document).ready(function(){

	$('#menu li a').click(function(){
		// TODO
		var allowedModules = ['news', 'galeria-foto', 'oferta', 'strefa-klienta', 'eventy', 'referencje', 'kontakt'];
		var module = $(this).attr('href').substr(1);

		// if element (clicked link) is on the list of allowed modules
		if (allowedModules.indexOf(module) !== -1)
		{

		}
	});

});

$.fn.getIndex = function(){
	var $p = $(this).parent().children();
	return $p.index(this);
}

