/********************************************/
/*	core.js
/*	author: davide reppucci
/*	author uri: http://www.gdlabs.it
/*
/*  latest update: 21/07/2011
/*******************************************/
var core = {
	bootstrap : function() {
		var externals = new externalLink();
		core.enhanceMenu();
		core.labelSwap();
		core.scrollPane();
		core.hasSub();
		core.skinSelect();
		core.adapteImage();
		Forms.init();
	},
	
	enhanceMenu : function() { $("nav ul").superfish({ speed: 350, delay: 150 }); },
		
	labelSwap : function() {	
		$("label + .focus-hide").each(function (type) {
			$(this).focus(function () { $(this).prev("label").addClass("focus"); });
			$(this).keyup(function () { $(this).prev("label").addClass("has-text").removeClass("focus"); });
			$(this).blur(function () { if($(this).val() == "") { $(this).prev("label").removeClass("has-text").removeClass("focus"); } });
			if( $(this).val() != "" ) { $(this).prev("label").addClass("has-text"); }
		});
		$("#attachment").each(function (type) {
			$(this).focus(function () { $(this).parent().prev("label").addClass("focus"); });
			$(this).change(function () { $(this).parent().prev("label").addClass("has-text").removeClass("focus"); });
			$(this).blur(function () { if($(this).val() == "") { $(this).parent().prev("label").removeClass("has-text").removeClass("focus"); } });
			if( $(this).val() != "" ) { $(this).parent().prev("label").addClass("has-text"); }
		});
	},
	
	scrollPane : function() {
		if( $('.scroll-pane').length != 0 ) {
			var api = $('.scroll-pane').jScrollPane({ autoReinitialise: true }).data('jsp');
		}
	},
	
	hasSub : function() {
		if( $('#main .subnav').length != 0 ) {
			$('#main .subnav ul li').each( function() {
				if( $(this).find('ul').length != 0 ) {
					$(this).find('>a').addClass('hasSub');
					$(this).find('a.hasSub').addClass('opened');
					//$(this).find('ul').addClass('closed').slideUp(1);
				}
			});
			/*
			$('#main .subnav ul li a.hasSub').live('click', function() {
				if( $(this).next().hasClass('closed') ) {
					$(this).addClass('opened');
					$(this).next().removeClass('closed').slideDown(300, "easeInOutCirc");
				}
				else {
					$(this).removeClass('opened');
					$(this).next().addClass('closed').slideUp(300, "easeInOutCirc");
				}
			});
			*/
		}
		if( $('footer ul li').length != 0 ) {
			$('footer ul li:first-child').addClass('first-elm');
		}
	},
	
	skinSelect : function() {
		$('.select-inner').each( function(i) {
		    selectContainer = $(this);
		    selectContainer.addClass('skinned-select');
		    selectContainer.append('<div class="select-text">&nbsp;</div>');
		    selectFake = selectContainer.find('.select-text');
		    selectFake.html( selectContainer.find('select option:selected').text() );
		    var parentTextObj = selectContainer.children().next();
		    parentTextObj.change(function() { $(this).parent().find('.select-text').html(parentTextObj.find(':selected').text()); });      
		});
	},
	
	adapteImage : function() {
		if( $('.post-list').length != 0 ) {
			$('.post-list article .image img').each( function() {
				if( $(this).height() < 140 ) { $(this).removeAttr('width').removeAttr('height').css({'height': 140, 'width': '' }); }
				if( $(this).width() < 190 ) { $(this).removeAttr('width').removeAttr('height').css({'width': 190, 'height': '' }); } 
			});
		}
	}
};
