

$(document).ready(function(){


	//$('#q').focus( function() { if ($(this).val() == 'Everything') $(this).val(''); });
	//$('#q').blur( function() { if ($(this).val() == '') $(this).val('Everything'); });
	$('#q').fieldHelp('Everything');

	$('.toggle').click(function(){
		var $this = $(this);
		var toggle_id = $this.attr('id');
		if ($this.is('.expanded')) {
			$this.next().slideUp('fast');
			$this.removeClass('expanded');
			$this.addClass('collapsed');
			$.cookie('toggle_'+toggle_id, 1);
		} else {
			$this.next().slideDown('fast');
			$this.removeClass('collapsed');
			$this.addClass('expanded');
			$.cookie('toggle_'+toggle_id, null);
		}
		return false;
	});
	$('.toggle').each(function(){
		var toggle_id = $(this).attr('id');
		if ($.cookie('toggle_'+toggle_id)){
			$(this).next().css({ display: 'none'});
			$(this).removeClass('expanded');
			$(this).addClass('collapsed');
		}
	});

	$('#news-ticker').ticker();
	setTimer($('#news-ticker'), 3000);

	rollOverEvent('#menu li img');
	
});



/**
 * Form Field TIP
 * @param {Object} text
 */
jQuery.fn.fieldHelp = function(text){
    return this.each(function(){

		if (this.type != 'text' && this.type != 'textarea') {
			return;
		}

		var fld_current = this;
		
        if(this.value=='') {
			this.value = text;
			$(this).addClass('helper');
		} else {
			return;
		}

		$(this).focus(function() {
			if (this.value == text || this.value == '') {
				this.value = '';
				$(this).removeClass('helper');
			}
		});

		$(this).blur(function() {
			if (this.value == text || this.value == '') {
				$(this).addClass('helper');
				this.value = text;
			}
		});
		
		$(this).parents('form').each(function() {
			$(this).submit(function() {
				if(fld_current.value == text) { fld_current.value = ''; }
			});
		});

    });

};





function showDiv(_name){
	$('#'+_name).removeClass('hide');
}




rollOverEvent = function(id){
	$(id).mouseover( function(){ $(this).attr('src', $(this).attr('src').split('_0').join('_1')) } );
	$(id).mouseout( function(){ $(this).attr('src', $(this).attr('src').split('_1').join('_0')) } );

}

$().ready(function(){
	rollOverEvent('#menu li img');
});




function winbox(win) {
	$('body').append('<div id="winbox-overlay"></div><div id="winbox-wrap"><div id="winbox-container">gh fg hfhfh fh</div></div>');	
}



/* Slider */
jQuery.fn.slideView = function() {
	var container = jQuery(this);
	var slides = container.find("ul");
	var images = slides.find("li");
	images.css('display', 'inline');
	var imgWidth = images.find("img").width();
	var s = 0;
	container.addClass("strip-viewer");
	slides.click(function() { showSlide(s++); });
	container.find(".prev").click(function() { showSlide(s--); });
	container.find(".next").click(function() { showSlide(s++); });

	function showSlide(n){
		if (s < 0) s = images.size() - 1;
		if (s >= images.size()) s = 0;
		container.find("p").html($("img", slides).eq(s).attr("alt"));
		slides.animate({'left' : '-'+(s*imgWidth)+'px'}, 300, 'linear');
	}

	showSlide(0);

};




(function($) { 
$.fn.ticker = function(){
	var o = this;
	initTicker = function(_obj){
		el = $('ul', _obj)[0];
		el.items = $('li', el);
		el.items.not(":eq(0)").hide().end();
		el.currentitem = 0;
		$('ul', _obj).show();
		doTick(o, 0);
	};
	doTick = function(_obj, _dir){
		el = $('ul', _obj)[0];
		$(el.items[el.currentitem]).hide();
		el.currentitem += _dir;
		if (el.currentitem<0) el.currentitem = el.items.size()-1;
		if (el.currentitem>=el.items.size()) el.currentitem = 0;
		$(el.items[el.currentitem]).show();
		$('.counter', _obj).html((el.currentitem+1)+' OF '+el.items.size());
	};
	setTimer = function(_obj, delay){
		el = $('ul', _obj)[0];
		el.timer = setInterval( function() { doTick(_obj, 1) }, delay);
	};
	stopTimer = function(_obj, delay){
		el = $('ul', _obj)[0];
		clearInterval(el.timer);
	};
	this.each(function(){ initTicker(this); });
	$('.prev', this).click(function(){ stopTimer(o); doTick(o, -1); });
	$('.next', this).click(function(){ stopTimer(o); doTick(o, 1); });
};
})(jQuery);






net = (document.layers && true);
widgetBuf = (net) ? 0:50
function popWin(url, name, width, height, scroll, resizable) {
	bWidth = (net) ? top.innerWidth:parseInt(top.document.body.clientWidth);
	bHeight = (net) ? top.innerHeight:parseInt(top.document.body.clientHeight);
	bLeft = (net) ? top.screenX:parseInt(top.screenLeft);
	bTop = (net) ? top.screenY:parseInt(top.screenTop);
	scroll = (scroll) ? scroll:"auto"
	resizable = (resizable) ? resizable:"yes"
	if (!bLeft) {
		bLeft = 0;
		bWidth = screen.width;
	}
	if (!bTop) {
		bTop = 0;
		bHeight = screen.height;
	}
	xer = bLeft + ((bWidth/2) - width/2);
	yer = (bTop - widgetBuf) + ((bHeight/2) - height/2);
	if (xer < 2) {
		xer = 2;
	}
	if (xer < 2) {
		xer = 2;
	}
	paramStr = "width=" + width + ",height=" + height + ",screenX=" + xer + ",screenY=" + yer + ",left=" + xer + ",top=" + yer + ",dependent=1,directories=0,location=0,menubar=0,resizable="+resizable+",scrollbars="+scroll+",toolbar=0";
	top[name] = window.open(url, name, paramStr);
	top[name].focus();
}


function survey(){
	popWin('http://bexelsurvey.sogma.net', 'survey', 900, 800, 1, 1);
}
