//$(document).ready(function(){
     jQuery(document).ready(function($){  
	$('select.customSelect').customSelect();
	$('.scroll-block').jScrollPane({scrollbarWidth:7});
    initMap();
    $('area').easyTooltip();
    
});
/*************/

function initMap(){
    var state_list = jQuery("#map-hover li")
    var areas = jQuery("#map area");
    var active;
    
    areas.hover(function(){
        active = areas.index(this);
        state_list.eq(active).addClass(" hoverstate");
    }, function(){
        active = areas.index(this);
        state_list.eq(active).removeClass(" hoverstate");
    });
    jQuery('#map').each(function(){
        var btn_h = jQuery(this);
        var _btn = jQuery(this).find('area');
        var li = jQuery(this).parent().find('#map-hover li');
        var _a = li.index(li.filter('.active:eq(0)'));
        if(_a == -1) _a = 0;
    //    _btn.removeClass('active').eq(_a).addClass('active');
    //    li.removeClass('active').eq(_a).addClass('active');
        _btn.each(function(_i){
            this._box = this.alt;
            this._title = this.title;
            if(this._box){
                this._box = jQuery('div.tab-'+this._box);
              //  if(_i == _a) this._box.show();
             //   else this._box.hide();
            }
            this.onclick = function(){                
                jQuery('div.tab-area0').hide();  /* add*/                
                jQuery('div.tab').hide();  /* add*/                
                jQuery('div.tab').hide();  /* add*/                
               state_list.removeClass('active');  /* add*/
                changeTab(_i);
                jQuery(this).trigger('blur');
                 jQuery('#titleh2').html(this._title)  /*add*/       
                return false;
            }
        });
        function changeTab(_ind){
            if(_ind != _a){
               
                if(_btn.get(_a)._box) _btn.get(_a)._box.hide();
                if(_btn.get(_ind)._box) _btn.get(_ind)._box.show();
                jQuery('.scroll-block').jScrollPane({scrollbarWidth:7});
                li.eq(_a).removeClass('active');
                li.eq(_ind).addClass('active');
                _a = _ind;
            }
        }
    });
}

    jQuery.fn.easyTooltip = function(options){
      
        // default configuration properties
        var defaults = {    
            xOffset: 10,        
            yOffset: 33,
            tooltipId: "easyTooltip",
            clickRemove: false,
            content: "",
            useElement: ""
        }; 
            
        var options = jQuery.extend(defaults, options);  
        var content;
                
        this.each(function() {                  
            var title = jQuery(this).attr("title");                
            jQuery(this).hover(function(e){                                                                            
                content = (options.content != "") ? options.content : title;
                content = (options.useElement != "") ? jQuery("#" + options.useElement).html() : content;
                jQuery(this).attr("title","");                                                      
                if (content != "" && content != undefined){            
                    jQuery("body").append("<div id='"+ options.tooltipId +"'>"+ content +"</div>");        
                    jQuery("#" + options.tooltipId)
                        .css("position","absolute")
                        .css("top",(e.pageY - options.yOffset) + "px")
                        .css("left",(e.pageX + options.xOffset) + "px")                        
                        .css({marginLeft: -jQuery("#" + options.tooltipId).outerWidth()/2})
                        .css("display","none")
                        .fadeIn("fast")
                }
            },
            function(){    
                jQuery("#" + options.tooltipId).remove();
                jQuery(this).attr("title",title);
            });    
            jQuery(this).mousemove(function(e){
                jQuery("#" + options.tooltipId)
                    .css("top",(e.pageY - options.yOffset) + "px")
                    .css("left",(e.pageX + options.xOffset) + "px")                    
            });    
            if(options.clickRemove){
                jQuery(this).mousedown(function(e){
                   jQuery("#" + options.tooltipId).remove();
                   jQuery(this).attr("title",title);
                });                
            }
        });
      
    };


/******************/	
jQuery.fn.customSelect = function(_options) {
var _options = jQuery.extend({
	selectStructure: '<div class="selectArea"><div class="selected"></div></div>',
	selectText: '.selected',
	selectBtn: '.selected',
	selectDisabled: '.disabled',
	optStructure: '<div class="select-sub"><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 = jQuery(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');
					jQuery(this).parent().addClass('selected');
					selOpt.attr('selected', 'selected');
					selectText.html(selOpt.html());
					select.change();
					optHolder.hide();
					return false;
				});
				optList.append(_opt);
			});
			if (select.attr('title')) selectText.html(select.attr('title'));
			replaced.width(select.outerWidth());
			replaced.insertBefore(select);
			replaced.addClass(select.attr('class'));
				optHolder.css({
					width: select.outerWidth(),
					display: 'none',
					position: 'absolute',
					zIndex: 999
				});
			optHolder.addClass(select.attr('class'));
			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() +2,
						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');
		}
	}
});
}
jQuery.fn.customRadio = function(_options){
	var _options = jQuery.extend({
		radioStructure: '<span></span>',
		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);
			replaced.addClass(radio.attr('class'));
			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(jQuery(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){
		jQuery('input:radio[name='+jQuery(_this).attr("name")+']').not(_this).each(function(){
			if(this._replaced && !jQuery(this).is(':disabled')) this._replaced.removeClass('radioAreaChecked').removeClass('radioArea').addClass(_options.radioDefault);
		});
		_this._replaced.removeClass('radioAreaChecked').removeClass('radioArea').addClass(_options.radioChecked);
	}
}
jQuery.fn.customCheckbox = function(_options){
	var _options = jQuery.extend({
		checkboxStructure: '<span></span>',
		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);
			replaced.addClass(checkbox.attr('class'));
			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('checkboxArea').removeClass('checkboxAreaChecked').addClass(_options.checkboxChecked);
		else _this.get(0)._replaced.removeClass('checkboxArea').removeClass('checkboxAreaChecked').addClass(_options.checkboxDefault);
	}
	
}

