/* Header gallery */

var pgall = null;

function gall_preload(list) {
	img = new Image();
	for(a in list) img.src = list[a].url;
}

function pgall_in(carousel, item, i, state, evt) {
    var idx = carousel.index(i, pgall_itemList.length);
    carousel.add(i, pgall_getItemHTML(pgall_itemList[idx - 1]));
}

function pgall_out(carousel, item, i, state, evt) { carousel.remove(i); }

function pgall_getItemHTML(item) { return (item ? '<img src="' + item.url + '" width="1200" height="576" alt="' + item.title + '" />' : ''); }

var t_anim_l = null;
var anim_l = function() {
	$('#hgall_left').animate({left: '-=20'}, 500, 'easeInSine', function() {
		$(this).animate({left: '+=20'}, 300, 'easeOutBounce' , function() {
			t_anim_l = setTimeout('anim_l();', Math.ceil(Math.random() * 8000)+ 5000);
		});
	});
};

var t_anim_r = null;
var anim_r = function() {
	$('#hgall_right').animate({right: '-=20'}, 500, 'easeInSine', function() {
		$(this).animate({right: '+=20'}, 300, 'easeOutBounce' , function() {
			t_anim_r = setTimeout('anim_r();', Math.ceil(Math.random() * 8000)+ 5000);
		});
	});
};

function pgall_Init(img_path) {
	gall_preload(pgall_itemList);
	var autoCh = 10;
	var nextB = '<img src="'+img_path+'img/gall_right.png" width="41" height="58" id="hgall_right" />';
	var prevB = '<img src="'+img_path+'img/gall_left.png" width="41" height="58" id="hgall_left" />';
	if(pgall_itemList.length < 2) {
		autoCh = 0;
		nextB = null;
		prevB = null;
	}
	pgall = $('#head_gallery_panels').jcarousel({
		wrap: 'circular',
		scroll: 1,
		itemVisibleInCallback: {onBeforeAnimation: pgall_in},
		itemVisibleOutCallback: {onAfterAnimation: pgall_out},
		buttonNextHTML: nextB,
		buttonPrevHTML: prevB,
		auto: autoCh,
		animation: 1000
    });
	
	t_anim_l = setTimeout('anim_l();', Math.ceil(Math.random() * 8000));
	$('#hgall_left').hover(
	   function() { clearTimeout(t_anim_l); },
	   function() { t_anim_l = setTimeout('anim_l();', Math.ceil(Math.random() * 8000)); }
	);
	
	t_anim_r = setTimeout('anim_r();', Math.ceil(Math.random() * 8000));
	$('#hgall_right').hover(
	   function() { clearTimeout(t_anim_r); },
	   function() { t_anim_r = setTimeout('anim_r();', Math.ceil(Math.random() * 8000)); }
	);
}

/* /header gallery */

/* Nabidky - homepage */
function offersInit(car) {
	$('#offers_wrap .ctrls span a').bind('click', function() {
		car.scroll($.jcarousel.intval($(this).attr('nu')));
		return false;
	});
	
	$('#offers_wrap a.cp_prev').bind('click', function() {
		car.next();
		return false;
	});
	
	$('#offers_wrap a.cp_next').bind('click', function() {
		car.prev();
		return false;
	});
}

function offersLoad(interval, speed) {
	
	if(isNaN(interval)) interval = 5;
	if(isNaN(speed)) speed = 800;
	
	$("#offers").jcarousel({
		scroll: 1,
		wrap: 'both',
		auto: interval,
		animation: speed,
		initCallback: offersInit,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		itemVisibleInCallback: {
			onBeforeAnimation: function(carousel, item, i, state, evt) {
				$('#offers_wrap .ctrls a.sel').removeClass('sel');
				$('#offers_wrap .ctrls a[nu="'+i+'"]').addClass('sel');
			}
		}
	});
}

/* /Nabidky - homepage */

function getElement(aID) {
	return (document.getElementById) ?
	    document.getElementById(aID) : document.all[aID];
}

function getIFrameDocument(aID) { 
    var rv = null; 
    var frame=getElement(aID);
    if (frame.contentDocument)
        rv = frame.contentDocument;
    else // bad Internet Explorer  ;)
        rv = document.frames[aID].document;
    return rv;
}
