$(document).ready(init);

function init() {
	
	if (0 < $('div.novice').length) {
		nCall();
	}
	if (0 < $('div.medijskenovice').length) {
		mnCall();
	}
	if (0 < $('div.emedijskenovice').length) {
		mneCall();
	}
	if (0 < $('#bigImage').length) {
		$.getJSON('gallery.js', function(gd) {gCall_onLoad(gd, '#thumbs', gd.items.length > 15);});
	}

	if (0 < $('#bigexhibitionImage').length) {
		$.getJSON('gallery.js', function(gd) {eCall_onLoad(gd, '#exhibitionthumbs', gd.items.length > 15);});
	}

	if (0 < $('#bigCollectionImage').length) {
		$.getJSON('collection.js', function(gd) {cCall_onLoad(gd, '#collectionThumbs', gd.items.length > 15);});
	}


}


// Pier cutting edge technology web 3.0 ready !
function swapImage(me, img){
	me.src = img;
	return true;
}



/**********************************************************
 * News magick
 */
function nCall(page) {
	$('.novice').load(page == undefined ? 'news.html' : page);
}

/**********************************************************
 * Media news magick
 */
function mnCall(page) {
	$('.medijskenovice').load(page == undefined ? 'media_news.html' : page);
}

/**********************************************************
 * Media news magick
 */
function mneCall(page) {
	$('.emedijskenovice').load(page == undefined ? 'media_news_exhibition.html' : page);
}

/**********************************************************
 * Gallery magick
 */
function gCall_onLoad(gd, holder, reqSmall) {
	$(holder).empty();

	var img, item;

	for (var i = 0; i < gd.items.length; i++) {
		item = gd.items[i];
		img          = document.createElement('img');

		img.src      = reqSmall ? item.thumb2 : item.thumb;

		img.isImage  = item.isImage;
		img.title    = item.title ? item.title : '';
		img.alt      = item.title ? item.title : '';
		img.big      = item.big;
		img.desc     = item.desc  ? item.desc  : '';
		img.folderId = item.folderId;

		$(holder).append(img);
	}

	try {
		$('#galleryFile'   ).empty();
		$('#galleryTitle'  ).empty();
		$('#galleryComment').empty()

		$('#galleryFile'   ).html(gd.adt.fileDescription.length > 0 ? gd.adt.fileDescription : gd.adt.file);
		$('#galleryFile'   ).attr('href', gd.adt.file);
		$('#galleryTitle'  ).html(gd.adt.title);
		$('#galleryComment').html(gd.adt.comment);
	} catch (e) {}


	// Bind click and mouse(over|out) events to all images
	$('img', holder)
	  .bind('click', gItem_onClick)
		.bind('mouseover', function() { $(this).addClass('hover'   );})
		.bind('mouseout',  function() { $(this).removeClass('hover');})
		.eq(0).click();
}

function gItem_onClick() {
	if (this.isImage) {
		$('#videoPlayer').hide();
		$('#bigImage')
			.show()
			.attr('src', this.big)
		;
	} else {
		$('#bigImage').hide();
		$('#videoPlayer').show();
		var so = new FlashObject("/Static/flash/video.swf", "player", "425", "315", "#888888");
		so.addParam("scale", "noscale");
		so.addParam("menu", "false");
		so.addVariable("pot", this.big)
		so.write('videoPlayer');
	}

	$('div.opisFoto').html(this.desc ? this.desc : '');

	$('img', $(this).parent()).removeClass('hover');
	$(this).addClass('hover');

	if (-1 != this.folderId) {
		$.getJSON(
			'../'+ this.folderId +',0/gallery.js',
			function(gd) {gCall_onLoad(gd, '#subthumbs', true);}
		);
	}
}

/**********************************************************
 * Exhibition magick
 */
function eItem_onClick() {
	if (this.isImage) {
		$('#videoexhibitionPlayer').hide();
		$('#bigexhibitionImage')
			.show()
			.attr('src', this.big)
		;
	} else {
		$('#bigexhibitionImage').hide();
		$('#videoexhibitionPlayer').show();
		var so = new FlashObject("/Static/flash/video.swf", "player", "425", "315", "#888888");
		so.addParam("scale", "noscale");
		so.addParam("menu", "false");
		so.addVariable("pot", this.big)
		so.write('videoexhibitionPlayer');
	}

	$('div.opisexhibitionFoto').html(this.desc ? this.desc : '');

	$('img', $(this).parent()).removeClass('selected');
	$(this).addClass('selected');

}

function eCall_onLoad(gd, holder, reqSmall) {
	$(holder).empty();

	var img, item;

	for (var i = 0; i < gd.items.length; i++) {
		item = gd.items[i];
		img          = document.createElement('img');

		img.src      = reqSmall ? item.thumb2 : item.thumb;

		img.isImage  = item.isImage;
		img.title    = item.title ? item.title : '';
		img.alt      = item.title ? item.title : '';
		img.big      = item.big;
		img.desc     = item.desc  ? item.desc  : '';
		img.folderId = item.folderId;

		$(holder).append(img);
	}

	try {
		$('#exhibitionFile'				).empty();
		$('#exhibitionTitle'			).empty();
		$('#exhibitionComment'			).empty();

		if (gd.adt.file) {
			$('#exhibitionFile'				).html(gd.adt.fileDescription.length > 0 ? gd.adt.fileDescription : gd.adt.file);
			$('#exhibitionFile'				).attr('href', gd.adt.file);
			$('#exhibitionFile'				).attr('class', 'doc');
		}

		$('#exhibitionTitle'			).html(gd.adt.title);
		$('#exhibitionDateStart'		).html(gd.adt.date_start);
		$('#exhibitionDateEnd'			).html(gd.adt.date_end);
		$('#exhibitionAuthor'			).html(gd.adt.authorName);
		$('#exhibitionAuthor'			).attr('href', gd.adt.authorLink);
		$('#exhibitionAuthorDescription').html(gd.adt.authorDescription);

		$('#exhibitionComment').html(gd.adt.comment);
	} catch (e) {}


	// Bind click and mouse(over|out) events to all images
	$('img', holder)
		.bind('click', eItem_onClick)
		.bind('mouseover', function() { $(this).addClass('hover'   );})
		.bind('mouseout',  function() { $(this).removeClass('hover');})
		.eq(0).click();
		
	$('#exhibitionAuthorName', holder)
		.bind('click', function() { alert('fpp');$('div.exhibitionmoreinfo').html(gd.adt.authorDescription ? gd.adt.authorDescription : 'Description'); });

}

/**********************************************************
 * Collection magick
 */
function cItem_onClick() {
	if (this.isImage) {
		$('#videoCollectionPlayer').hide();
		$('#bigCollectionImage')
			.show()
			.attr('src', this.big)
		;
	} else {
		$('#bigCollectionImage').hide();
		$('#videoCollectionPlayer').show();
		var so = new FlashObject("/Static/flash/video.swf", "player", "425", "315", "#888888");
		so.addParam("scale", "noscale");
		so.addParam("menu", "false");
		so.addVariable("pot", this.big)
		so.write('videoCollectionPlayer');
	}

	$('div.opisCollectionFoto'	).html(this.desc ? this.desc : '');

	$('#collectionTitle'		).html(this.authorName ? this.authorName : 'author');
	$('#collectionLink'			).attr('href', this.authorLink);

	$('#collectionMoreInfo'	).html(this.authorDescription ? this.authorDescription : 'author description');

	$('img', $(this).parent()).removeClass('selected');
	$(this).addClass('selected');

}

function cCall_onLoad(gd, holder, reqSmall) {
	$(holder).empty();

	var img, item;

	for (var i = 0; i < gd.items.length; i++) {
		item = gd.items[i];
		img          = document.createElement('img');

		img.src      = reqSmall ? item.thumb2 : item.thumb;

		img.isImage  = item.isImage;
		img.title    = item.title ? item.title : '';
		img.alt      = item.title ? item.title : '';
		img.big      = item.big;
		img.desc     = item.desc  ? item.desc  : '';
		img.authorLink = item.authorLink  ? item.authorLink  : '';
		img.authorName = item.authorName  ? item.authorName  : '';
		img.authorDescription = item.authorDescription  ? item.authorDescription  : '';

		$(holder).append(img);
	}

	try {

		$('#collectionTitle'			).empty();
		$('#collectionComment'			).empty();

		$('#collectionTitle'			).html(gd.adt.authorName);
		$('#collectionTitle'			).attr('href', gd.adt.authorLink);
		$('#collectionComment'			).html(gd.adt.comment);

	} catch (e) {}


	// Bind click and mouse(over|out) events to all images
	$('img', holder)
		.bind('click', cItem_onClick)
		.bind('mouseover', function() { $(this).addClass('hover'   );})
		.bind('mouseout',  function() { $(this).removeClass('hover');})
		.eq(0).click();

}
