 $(document).ready(function() {

//	$.taconite.debug = true; 

//	$('#b00 a').makeAjaxLink();
	initMenu();

animirajPocetnu();

$b1 = $('#b1');
$b2 = $('#b2');
$b3 = $('#b3');

boxevi = {
	b1_width: "470px",
	b1_visible: true,
	b2_height: '200px',
	b2_visible: true,
	b3_height: '40px',
	b3_visible: true
};

$sa2 = $('#img_sa2').hide();
$sa3 = $('#img_sa3').hide();

/**/
$('a').makeAjaxLink();
/**/

if ($('#adminMenu').text().length > 0) {
	$adminButton = $('#adminButton').click(function() {
											$(this).hide(); 
											$adminMenu.dialog('open');
										}).show();
	$adminMenu = $('#adminMenu').dialog({
		autoOpen: false,
		width: 200,
		position: ['right','top'],
		draggable: false,
		resizable: false,
		zIndex: 10000,
		beforeclose: function(event, ui) {
			$adminButton.show();
		}});
}

 });


function animirajPocetnu() {
	$('#p0').cycle({timeout: 3500});
	$('#p1').cycle({timeout: 5100});
	$('#p2').cycle({timeout: 4300});
}


function initMenu() {	
       $("ul.sf-menu").supersubs({ 
            minWidth:    12,   // minimum width of sub-menus in em units 
            maxWidth:    27,   // maximum width of sub-menus in em units 
            extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
                               // due to slight rounding differences and font-family 
        }).superfish({ 
            delay:       0,                            // one second delay on mouseout 
            animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
            speed:       0,                          // faster animation speed 
            autoArrows:  true,                           // disable generation of arrow mark-up 
            dropShadows: false                            // disable drop shadows 
        });  // call supersubs first, then superfish, so that subs are 
                         // not display:none when measuring. Call before initialising 
                         // containing tabs for same reason. 
}


function animirajSliku() {
	$img = $('#b1 .s img:first');
	$imgH = $sa2.height();
	$imgH2 = Math.round($imgH / 10);

	$imgDiv = $('<div />').attr({id: 'superAnimacija', style: $img.attr('style')})
									.width($sa2.width()).height($imgH)
									.append($img.clone())
									.append($sa2.clone()).append($sa3.clone());
	$imgDiv.find('img').each(function(index, domElem) {
		if (index == 0) {
			$(this).removeAttr('style').hide();
		} else {
			$(this).show();
		}
		$(this).css({'z-index': index+1, 'top': '0px', 'left': '0px'}).attr('id', 'sa'+(index+1));
	});
	$img.replaceWith($imgDiv);
	$img = $imgDiv.find('img:first');

	var funkcijaLoad = function() {
		$img.show();
		$('#superAnimacija #sa2').animate({top: -$imgH2, left: '0px', opacity: '0.9'}, 1000);
		$('#superAnimacija #sa3').animate({top: $imgH2, left: '0px', opacity: '0.9'}, 1000);
		$('#superAnimacija #sa2').animate({top: -$imgH, left: '0px', opacity: '0' }, 1000);
		$('#superAnimacija #sa3').animate({top: $imgH, left: '0px', opacity: '0'}, 1000);
	};

	if ($.browser.opera) {
		$img.ready(funkcijaLoad);
	} else {
		$img.load(funkcijaLoad);
	}
}

$.fn.makeAjaxLink = function() {
	   this.click(function(e) {
	   	if ($(this).attr('class') == 'delete') {
				if (!eval($(this).attr('forClick').toString().replace('return ', ''))) {
					return false;
				}
	   	}
			if (this.hostname == location.hostname) {
				e.preventDefault();
				var href = this.href
				if (href.indexOf('#') > 0) {
					return false;
				} else if (href.indexOf('index.php') > 0) {
					this.href = href.replace('index.php', 'ajax.php');
				} else {
					this.href = href.replace('/?', '/ajax.php?');
				}
				$.get(this.href + '&cCache=' + (new Date()).getTime());
			} else {
				this.target = '_blank';
			}
   	});
}
$.fn.makeAjaxForm = function() {
	var $form = $(this);
	var options = {url: 'ajax.php'+$form.attr('action'), beforeSubmit: function(data, jQform, opt) {
		if (typeof(FCKeditorAPI) != 'undefined') {
			$.each(data, function(i, val) {
				var oEditor = FCKeditorAPI.GetInstance(val.name);
				if (oEditor) data[i].value = oEditor.GetHTML();
			});
		};
	}};
	$(':file', $form).each(function() {
		var $upload = $(this);
		var $myButtonID = $upload.attr('id') + 'Uploader';
		var $imgOld = $('#img_old');
		var options = {
			'uploader':'./lib/uploader.swf',
			'script':'./upload.php',
			'cancelImg':'./lib/cancel.png',
			'folder':'./slike',
			'fileDesc': 'JPG',
			'fileExt': '*.jpg; *.jpeg',
			'auto': true,
			'onSelect': function(e, ID, f) {
//				$('#' + $myButtonID).hide();
			},
			'onCancel': function(e, ID, f, d) {
//				$('#' + $myButtonID).show();
			},
			'onComplete': function() {
				if (typeof($imgOld.attr('id')) == 'undefined') {
					$imgOld = $('<img />').attr({'id': 'img_old', 'src': './tumb.php?img=P_0.jpg&w=100'+ '&cCache=' + (new Date()).getTime(), 'border': '0'});

					var $tr = $upload.parent().parent().clone();
					$tr.children().eq(0).empty().html('Dodana slika:');
					$tr.children().eq(1).empty().append($imgOld);
					$upload.parent().parent().before($tr);					

				} else {
					$imgOld.hide().attr('src', './tumb.php?img=P_0.jpg&w=100'+ '&cCache=' + (new Date()).getTime()).load(function() { $(this).show(); });
				}
				$('#slika').attr('value', '1');
			}
		};
		$(this).fileUpload(options);
	});
	
	$form.ajaxForm(options);
}

function prepareAllBox(options) {
	var empty = {};
	var settings = $.extend(empty, boxevi, options);

	if (settings.b1_visible) $('#b1:hidden').fadeIn(); else $('#b1:visible').fadeOut();
	if (settings.b1_visible && $b1.css('width') != settings.b1_width) $b1.animate({width: settings.b1_width}, 500);

	if (settings.b2_visible) $('#b2:hidden').fadeIn(); else $('#b2:visible').fadeOut();
	if (settings.b2_visible && $b2.find('.s').css('height') != settings.b2_height) $b2.find('.s').animate({height: settings.b2_height}, 500);

	if (settings.b3_visible) $('#b3:hidden').fadeIn(); else $('#b3:visible').fadeOut();
	if (settings.b3_visible && $b3.find('.s').css('height') != settings.b3_height) $b3.find('.s').animate({height: settings.b3_height}, 500);
}
/**/
function prepareAllBox2(options) {
	var settings = $.extend({}, boxevi, options);

	if (!settings.b2_visible) {                         
		settings.b3_visible = false; 
		settings.b1_width   = '940px';
	} else {
		settings.b1_width  = '470px';
	}
	if (settings.b2_visible && !settings.b3_visible) {
		settings.b2_height  = '305px'; 
	} else {
		settings.b2_height = '200px';
	}
	
//	if (settings.b1_visible) $('#b1:hidden').show(); else $('#b1:visible').hide();
	if ($b1.css('width') != settings.b1_width) $b1.animate({width: settings.b1_width}, 1000);

	if (settings.b2_visible) $('#b2:hidden').show(); else $('#b2:visible').hide();
	if (settings.b2_visible && $b2.find('.s').css('height') != settings.b2_height) $b2.find('.s').animate({height: settings.b2_height}, 1000);

	if (settings.b3_visible) $('#b3:hidden').show(); else $('#b3:visible').hide();
//	if (settings.b3_visible && $b3.find('.s').css('height') != settings.b3_height) $b3.find('.s').animate({height: settings.b3_height}, 1000);
}
/**/

$.fn.replaceBox = function(newContentElements) {
	return this.each(function() {
		$(this).hide().empty().append(newContentElements).fadeIn();
	});
}
$.fn.replaceAndResizeBox1 = function(newContentElements) {
	return this.each(function() {
		prepareAllBox2({b2_visible: false});
		$(this).hide().empty().append(newContentElements).fadeIn();
	});
}
$.fn.replaceAndResizeBox2 = function(newContentElements) {
	return this.each(function() {
		prepareAllBox2({b3_visible: false});
		$(this).hide().empty().append(newContentElements).fadeIn();
	});
}
$.fn.replaceAndResizeBox3 = function(newContentElements) {
	return this.each(function() {
		prepareAllBox2({});
		$(this).hide().empty().append(newContentElements).fadeIn();
	});
}


