$(document).ready(function(){
	
	// for css fixes
	if ($.browser.mozilla && (/Win/).test(navigator.platform))
		$('.b').addClass('b-win-ff');
	if ($.browser.msie)
		$('.b-list>li:last-child').addClass('last-child');
	//if ($.browser.msie && $.browser.version <= 7)
	//	$('.primetimeBuy .select-box-scroll ul').width($('.primetimeBuy .select-box-scroll').width());
	
	// select on click
	$('.b').live('click', function(e){ //click(function() {
		if ($(this).hasClass('not-selectable')) return;
		ul = $(this).parent().parent();
		if (ul.hasClass('b-list') || ul.hasClass('b-tab'))
			ul.find('.b').removeClass('selected');
		$(this).toggleClass('selected');
		
		// checkbox value to hidden input next to it
		if ($(this).hasClass('b-icon-name-checkbox')) {
			$(this).removeClass('error');
			$(this).hasClass('selected') ? $(this).next().val("on") : $(this).next().val("off");
		}
	});
	
	// select tab initial set and on click action
	$('.b-tab>li').each(function(){
		if ($(this).children('a').hasClass('selected'))
		{
			$(this).addClass('selected-tab');
			$(this).next().addClass('selected-tab-next')
		}
	});
	$('.b-tab>li').click(function(){
		if ($(this).children('a.not-selectable').length) return;
		$(this).siblings().removeClass('selected-tab selected-tab-next');
		$(this).addClass('selected-tab');
		$(this).next().addClass('selected-tab-next');
	});
	
	// play-pause handling
	$('.b-icon-name-play-pause').addClass('b-icon-name-play');
	$('.b-icon-name-play-pause').click(function(){
		window['playSound'] && playSound($(this).attr("playpauseid"), $(this).attr("playpauseurl"));
		return false;
	});
	
	initializeButtons($('body'));
	
	// select-box: show dropdown (both click and live-click)
	$(document).click(function(e) { 
		elem = e.target;
		while (elem != null)
		{
			if ($(elem).hasClass('select-box-scroll')) return;
			elem = elem.parentNode;
		}
		hideAllDropdowns();
	});
	
	// select item in dropdown
	$('.select-box-list ul li a').live('click', function(e){
		href = $(this).attr('href');
		if ($(this).attr('target') == '_blank' || href.indexOf('http') == 0 || (href.indexOf('/') == 0 && href.indexOf('/api/') != 0))
			return;
		list = $(this).closest('.select-box-list');
		button = list.prev();
		input = list.next();
		isMultiple = button.hasClass('b-select-box-multiple');
		if (!isMultiple) {
			list.find('li').removeClass('selected');
			button.children().html('<i></i>' + $(this).text());
			input.val($(this).attr('href'));
			
            // trigger custom event
            $(input).trigger('onItemSelected');
		}
		if (isMultiple && $(this).hasClass('b-icon-name-play-pause')) {
			isSelected = $(this).parent().hasClass('selected');
			list.find('li').removeClass('selected');
			isSelected && $(this).parent().addClass('selected');
			b_span = button.children();
			b_span_i = b_span.html().substring(0, b_span.html().indexOf('</i>') + 4);
			b_text = isSelected ? list.attr('default') : $(this).children('span').text();
			b_span.html(b_span_i + b_text);
		}
		$(this).parent().toggleClass('selected');
		isMultiple && ($(this).parent().hasClass('selected') ? $(this).next().val($(this).attr('href')) : $(this).next().val(''));
		if (!list.hasClass('push') && !isMultiple) {
			button.toggleClass('b-icon-name-arrow-down').toggleClass('b-icon-name-arrow-up');
			list.children().slideToggle('fast');
		}
		button.removeClass('error');
		$('#errorsContainer').remove();
        return false;
	});
	
	// autoscroll to element position in select boxes
	$(document).keyup(function(e){
		if (timeoutForKeypress) clearTimeout(timeoutForKeypress);
		sbs = $('.select-box-scroll:visible');
		if (!sbs.length) return;
		if (sbs.hasClass('nohelp')) return;
		if (sbs.parent().hasClass('push')) return;
		if (65 > e.keyCode || e.keyCode > 90) { if (e.keyCode != 13) return; }
		
		if (e.keyCode != 13) {
			!$('#dropdownHelp').length && sbs.after('<div id="dropdownHelp" style="display: none"></div>');
			$('#dropdownHelp').width( sbs.parent().width() - 43 );
			$('#dropdownHelp').fadeIn();
			str = $('#dropdownHelp').text() + (String.fromCharCode(e.keyCode)).toLowerCase();
			$('#dropdownHelp').text(str);
		} else
		str = $('#dropdownHelp').text();
		
		sbs.find('li').each(function(){
			if ($(this).text().toLowerCase().indexOf(str) == 0) {
				if (e.keyCode == 13) { $(this).children().click(); if ($.browser.mozilla) $(this).closest('.select-box-list').prev().click(); return false; }
				sbs.data('jsp').scrollToElement($(this), true);
				return false;
			}
		});
		if (e.keyCode == 13) { return false; e.stopPropagation(); }
		var timeoutForKeypress = setTimeout(function(){
			$('#dropdownHelp').fadeOut(function(){ $('#dropdownHelp').remove(); } );
		}, 2000);
	});
	
	$(window).load(function(){ $('.b, .b-list').show(); });
	// for very slow connections
	setTimeout(function() { $('.b, .b-list').show(); }, 20000)
});

function initializeButtons(cont)
{
	if ($.browser.msie)
		cont.find('.b-list>li:last-child').addClass('last-child');
	
	// elements that are not selectable
	cont.find('.b').each(function(){
		if ($(this).hasClass('b-icon-name-checkbox')) return;
		if ($(this).parent().parent().hasClass('b-list')) return;
		if ($(this).parent().parent().hasClass('b-tab')) return;
		$(this).addClass('not-selectable');
	});
	cont.find('.b-tab .b').addClass('b-noshadow');
	
	// stupid fix for stupid IE7
	if (!($.browser.msie && $.browser.version <= 7 && $('.select-box-list').parent().hasClass('primetimeBuy')))
	// select-box: generate scroll for dropdown
	cont.find('.select-box-list').each(function(){
		$(this).children().show();
		$('.b').show();
		var elem = $(this), arr = [];
		while (elem[0].tagName.toUpperCase() != "BODY"){
			if (elem.css('display') == "none") { arr.push(elem); elem.show(); }
			elem = elem.parent();
		}
		$(this).children().jScrollPane({verticalDragMinHeight : 42, verticalDragMaxHeight : 42});
		$(this).children().hide();
		$.each(arr, function(){ $(this).hide(); });
		$('.b').hide();
	});
	
	cont.find('.b-select-box').each(function(){
		if ($(this).next().find('li.selected').length == 0) return;
		b_span = $(this).children();
		b_span_html = b_span.html().toLowerCase();
		b_span_i = b_span_html.substring(0, b_span_html.indexOf('</i>') + 4);
		b_span.html(b_span_i + $(this).next().find('li.selected a').text());
	});
	
	cont.find('.b-select-box').click(function(){
		listScroll = $(this).parent().find('.select-box-scroll');
		isVisible = listScroll.is(':visible');
		hideAllDropdowns();
		if (!isVisible || listScroll.parent().hasClass('push')) {
			$(this).toggleClass('b-icon-name-arrow-down').toggleClass('b-icon-name-arrow-up');
			listScroll.slideToggle('fast');
		}
		return false;
	});
	
	$('.select-box-list ul li a').each(function(){
		href = $(this).attr('href');
		if (href.indexOf(window.location.href) == 0)
			href = href.substring(window.location.href.length);
		$(this).attr('href', href);
	});
	
	(cont[0].tagName.toUpperCase() != "BODY") && $('.b, .b-list').show();
}

function hideAllDropdowns()
{
	$('.select-box-scroll').each(function(){ !$(this).parent().hasClass('push') && $(this).slideUp('fast'); });
	$('.b-select-box').each(function(){ !$(this).hasClass('b-select-box-push') && $(this).addClass('b-icon-name-arrow-down').removeClass('b-icon-name-arrow-up'); });
}
