/*------------------------ PRINT -----------------------------*/
(function($) {
    $.fn.print = function(customOptions) {
        var options = $.extend({}, $.fn.print.defaultOptions, customOptions);

        return this.each(function() {
            $(this).append('<a href="" class="' + options.printClass + '">' + options.printText + '</a>');

            $('a.' + options.printClass + '').click(function() {
                window.print();
                return false;
            });
        });
    };

    $.fn.print.defaultOptions = {
        printClass: 'print',
        printText: 'Print'
    };
});

$(document).ready(function () {
	/*
    $('.fagomrade-btn').hover(function () {
        $('.fagomrade-popup').show();
    }, function () {
        $('.fagomrade-popup').hide();
    });
	*/
    $('.fagomrade-btn').parent().hover(
		function(){
			$('.fagomrade-popup').show();
		},
		function(){
			$('.fagomrade-popup').hide();
		}
	);
	


    // slide block *************************************************************
    /*
    var _stayTime = 300;
    var _slideSpeed = 200;
    $('.content-box03').each(function(){
    var _holder = $(this);
    var _opener = _holder.find('.add01 strong');
    var _slider = _holder.find('.slide').hide();
    var _timer;

    _opener.hover(function(){
    _slider.slideDown(_slideSpeed);
    },function(){
    _timer = setTimeout(function(){
    _slider.slideUp(_slideSpeed);
    },_stayTime)
    });

    _slider.hover(function(){
    if(_timer) clearTimeout(_timer);
    },function(){
    _timer = setTimeout(function(){
    _slider.slideUp(_slideSpeed);
    },_stayTime)
    });
    });
    */
    // custom forms ************************************************************
    $('.tabset').each(function (i, tabset) {
        var _tabLinks = $('a.tab', tabset);
        _tabLinks.each(function (j, link) {
            var _id = link.href.substr(link.href.indexOf('#'));
            $(_id).show();
        });
    });
    /*

    $('select').customSelect({
        selectStructure: '<div class="selectArea"><div class="left"></div><div class="center"></div><a href="#" class="selectButton">&nbsp;</a><div class="disabled"></div></div>',
        selectText: '.center',
        selectBtn: '.selectButton',
        selectDisabled: '.disabled',
        optStructure: '<div class="optionsDivVisible"><ul></ul></div>',
        optList: 'ul'
    });
    $('input[type=checkbox]').customCheckbox({
        checkboxStructure: '<div></div>',
        checkboxDisabled: 'disabled',
        checkboxDefault: 'checkboxArea',
        checkboxChecked: 'checkboxAreaChecked'
    });
    $('input[type=radio]').customRadio({
        radioStructure: '<div></div>',
        radioDisabled: 'disabled',
        radioDefault: 'radioArea',
        radioChecked: 'radioAreaChecked'
    });

    */
    $('.tabset').each(function (i, tabset) {
        var _tabLinks = $('a.tab', tabset);
        _tabLinks.each(function (j, link) {
            var _id = link.href.substr(link.href.indexOf('#'));
            $(_id).hide();
        });
    });

    // tabs ********************************************************************
    $('.tabset').each(function (i, tabset) {
        var _tabLinks = $('a.tab', tabset);
        $(this).show();
        _tabLinks.each(function (j, link) {
            var _id = link.href.substr(link.href.indexOf('#'));
            if ($(link).hasClass('active')) {
                $(_id).show();
            } else {
                $(_id).hide();
            }
            $(link).click(function () {
                _tabLinks.each(function (k, hideLink) {
                    var _hideId = hideLink.href.substr(hideLink.href.indexOf('#'));
                    if (hideLink != link && $(hideLink).hasClass('active')) {
                        $(hideLink).removeClass('active');
                        $(_hideId).hide();
                    }
                });
                $(link).addClass('active');
                $(_id).show();
                Cufon.refresh('div.tabset a');
                return false;
            })
        });
    });
});


/*------------------------ CUSTOM SELECT'S -----------------------------*/
jQuery.fn.customSelect = function(_options) {
var _options = jQuery.extend({
	selectStructure: '<div class="selectArea"><div class="left"></div><div class="center"></div><a href="#" class="selectButton">&nbsp;</a><div class="disabled"></div></div>',
	selectText: '.center',
	selectBtn: '.selectButton',
	selectDisabled: '.disabled',
	optStructure: '<div class="selectOptions"><ul></ul></div>',
	optList: 'ul'
}, _options);
return this.each(function() {
	var select = jQuery(this);
	if(!select.hasClass('outtaHere')) {
		if(select.is(':visible')) {
			var replaced = jQuery(_options.selectStructure);
			var selectText = replaced.find(_options.selectText);
			var selectBtn = replaced.find(_options.selectBtn);
			var selectDisabled = replaced.find(_options.selectDisabled).hide();
			var optHolder = jQuery(_options.optStructure);
			var optList = optHolder.find(_options.optList);
			if(select.attr('disabled')) selectDisabled.show();
			select.find('option').each(function() {
				var selOpt = $(this);
				var _opt = jQuery('<li><a href="#">' + selOpt.html() + '</a></li>');
				if(selOpt.attr('selected')) {
					selectText.html(selOpt.html());
					_opt.addClass('selected');
				}
				_opt.children('a').click(function() {
					optList.find('li').removeClass('selected');
					select.find('option').removeAttr('selected');
					$(this).parent().addClass('selected');
					selOpt.attr('selected', 'selected');
					selectText.html(selOpt.html());
					select.change();
					optHolder.hide();
					return false;
				});
				optList.append(_opt);
			});
			replaced.width(select.outerWidth());
			replaced.insertBefore(select);
			optHolder.css({
				width: select.outerWidth(),
				display: 'none',
				position: 'absolute'
			});
			jQuery(document.body).append(optHolder);
			
			var optTimer;
			replaced.hover(function() {
				if(optTimer) clearTimeout(optTimer);
			}, function() {
				optTimer = setTimeout(function() {
					optHolder.hide();
				}, 200);
			});
			optHolder.hover(function(){
				if(optTimer) clearTimeout(optTimer);
			}, function() {
				optTimer = setTimeout(function() {
					optHolder.hide();
				}, 200);
			});
			selectBtn.click(function() {
				if(optHolder.is(':visible')) {
					optHolder.hide();
				}
				else{
					optHolder.children('ul').css({height:'auto', overflow:'hidden'});
					optHolder.css({
						top: replaced.offset().top + replaced.outerHeight(),
						left: replaced.offset().left,
						display: 'block'
					});
					if(optHolder.children('ul').height() > 100) optHolder.children('ul').css({height:100, overflow:'auto'});
				}
				return false;
			});
			select.addClass('outtaHere');
		}
	}
});
}
/*------------------------ CUSTOM RADIO'S -----------------------------*/
jQuery.fn.customRadio = function(_options){
	var _options = jQuery.extend({
		radioStructure: '<div></div>',
		radioDisabled: 'disabled',
		radioDefault: 'radioArea',
		radioChecked: 'radioAreaChecked'
	}, _options);
	return this.each(function(){
		var radio = jQuery(this);
		if(!radio.hasClass('outtaHere') && radio.is(':radio')){
			var replaced = jQuery(_options.radioStructure);
			this._replaced = replaced;
			if(radio.is(':disabled')) replaced.addClass(_options.radioDisabled);
			else if(radio.is(':checked')) replaced.addClass(_options.radioChecked);
			else replaced.addClass(_options.radioDefault);
			replaced.click(function(){
				if($(this).hasClass(_options.radioDefault)){
					radio.change();
					radio.attr('checked', 'checked');
					changeRadio(radio.get(0));
				}
			});
			radio.click(function(){
				changeRadio(this);
			});
			replaced.insertBefore(radio);
			radio.addClass('outtaHere');
		}
	});
	function changeRadio(_this){
		$('input:radio[name='+$(_this).attr("name")+']').not(_this).each(function(){
			if(this._replaced && !$(this).is(':disabled')) this._replaced.removeClass().addClass(_options.radioDefault);
		});
		_this._replaced.removeClass().addClass(_options.radioChecked);
	}
}
/*------------------------ CUSTOM CHECKBOX'S -----------------------------*/
jQuery.fn.customCheckbox = function(_options){
	var _options = jQuery.extend({
		checkboxStructure: '<div></div>',
		checkboxDisabled: 'disabled',
		checkboxDefault: 'checkboxArea',
		checkboxChecked: 'checkboxAreaChecked'
	}, _options);
	return this.each(function(){
		var checkbox = jQuery(this);
		if(!checkbox.hasClass('outtaHere') && checkbox.is(':checkbox')){
			var replaced = jQuery(_options.checkboxStructure);
			this._replaced = replaced;
			if(checkbox.is(':disabled')) replaced.addClass(_options.checkboxDisabled);
			else if(checkbox.is(':checked')) replaced.addClass(_options.checkboxChecked);
			else replaced.addClass(_options.checkboxDefault);
			
			replaced.click(function(){
				if(checkbox.is(':checked')) checkbox.removeAttr('checked');
				else checkbox.attr('checked', 'checked');
				changeCheckbox(checkbox);
			});
			checkbox.click(function(){
				changeCheckbox(checkbox);
			});
			replaced.insertBefore(checkbox);
			checkbox.addClass('outtaHere');
		}
	});
	function changeCheckbox(_this){
		if(_this.is(':checked')) _this.get(0)._replaced.removeClass().addClass(_options.checkboxChecked);
		else _this.get(0)._replaced.removeClass().addClass(_options.checkboxDefault);
	}
}



