(function($) {

	// fix accordion heading state
	$('.accordion-heading').each(function(){
		var $this = $(this), $body = $this.siblings('.accordion-body');
		if (!$body.hasClass('in')){
			$this.find('.accordion-toggle').addClass('collapsed');
		}
	});
	
	// twice click
	$(document).on('click.twice', '.open [data-toggle="dropdown"]', function(e){
		var $this = $(this), href = $this.attr('href');
		e.preventDefault();
		window.location.href = href;
		return false;
	});
	
    $('#cpanel').collapse();
	
    $('#cpanel-reset').on('click', function(e) {

    	if (document.cookie && document.cookie != '') {
    		var split = document.cookie.split(';');
    		for (var i = 0; i < split.length; i++) {
    			var name_value = split[i].split("=");
    			name_value[0] = name_value[0].replace(/^ /, '');
    			
    			if (name_value[0].indexOf(cpanel_name)===0) {
    				$.cookie(name_value[0], 1, { path: '/', expires: -1 });
    			}
    		}
    	}
    	
    	location.reload();
    });
    
	$('#cpanel-form').on('submit', function(e){
		var $this = $(this), data = $this.data(), values = $this.serializeArray();
		
		var checkbox = $this.find('input:checkbox');
		$.each(checkbox, function() {
			
			if( !$(this).is(':checked') ) {
				name = $(this).attr('name');
				name = name.replace(/([^\[]*)\[(.*)\]/g, '$1_$2');
				
				$.cookie( name , 0, { path: '/', expires: 7 });
			} 
			
		})
		
		$.each(values, function(){
			var $nvp = this;
			var name = $nvp.name;
			var value = $nvp.value;
			
			if ( !(name.indexOf(cpanel_name + '[')===0) ) return ;
			
			//console.log('name: ' + name + ' -> value: ' +value);
			name = name.replace(/([^\[]*)\[(.*)\]/g, '$1_$2');
			
			$.cookie( name , value, { path: '/', expires: 7 });
			 
		});
		
		location.reload();
		
		return false;
		
	});
	
	$('a[href="#cpanel-form"]').on( 'click', function() {
		var parent = $('#cpanel-form'), right = parent.css('right'), width = parent.width();
		
		if ( parseFloat(right) < -10 ) {
			parent.animate({
				right: '0px',
			}, "slow");
		} else {
			parent.animate({
				right: '-' + width ,
			}, "slow");
		}
		
		if ( $(this).hasClass('active') ) {
			$(this).removeClass('active');
		} else $(this).addClass('active');
		
	});
$(window).on("load resize",function(){
	$('.nav-mega').each(function(){
		var $menu = $(this), $menu_dim = {
			left: $menu.offset().left,
			width: $menu.width()
		};
		//console.log( $menu );
		//console.log( $menu_dim );
						
		$('.dropdown', $menu).each(function(){
			var $parent_dim = {
				left: $(this).offset().left,
				width: $(this).outerWidth(true)
			};
			//console.log( $(this) );
			//console.log( $parent_dim );
						
			$dropdown = $('.dropdown-menu', this);
			if ( $dropdown.length ){
				var $drop_dim = {
					left: $dropdown.offset().left,
					width: $dropdown.outerWidth(true)
				}
				if ( $drop_dim.width > $menu_dim.width ){
					$drop_dim_left = $menu_dim.left - $parent_dim.left - ($drop_dim.width-$menu_dim.width)/2;
					$dropdown.css({left: $drop_dim_left});
				} else {
					var overright = overleft = false;
					// over right
					if ( $parent_dim.left + $drop_dim.left + $drop_dim.width > $menu_dim.left + $menu_dim.width){
						var $drop_dim_by_right = - $parent_dim.left - $drop_dim.width + $menu_dim.left + $menu_dim.width;
						overright = true;
					} else if ( $parent_dim.left + $parent_dim.width - $drop_dim.width < $menu_dim.left ){
						var $drop_dim_by_left = $menu_dim.left - $parent_dim.left;
						overleft = true;
					}
					//console.log( $dropdown );
					//console.log( $drop_dim );
					//overleft && console.log('->toleft: '+ $drop_dim_by_right );
					//overright && console.log('->toright: '+ $drop_dim_by_right );
					//console.log('------------------------------------------------------------------');
					if ( overright ){						
						$dropdown.css({left: $drop_dim_by_right});
					} else if ( overleft ){
						$dropdown.css({left: $drop_dim_by_left});
					}
				}
			}
		});
		// $menu.on('hover');
		// console.log( $menu_dim );
	});
});
	$(window).on("load resize",function() {
		$('.yaslider').each(function(){
			var height = $("#"+this.id).height();
			$("#"+this.id + ' .carousel-control').css('top', height/2);
		});
	});
}(jQuery));
	
	
	
	
	