$enet = jQuery.noConflict();

$enet(function(){
	enet_counter();
	
	$enet('div.enet')
	.delegate('ul.top_menu > li', 'mouseover', function(){$enet(this).addClass('hover').siblings().removeClass('hover')})
	.delegate('div.placeholder', 'click', function(){$enet(this).prev('input').focus();})
	.delegate('input.has_placeholder', 'keyup', function(){
		if(!$enet(this).val()){
			$enet(this).next('.placeholder').show();
		}else{
			$enet(this).next('.placeholder').hide();
		}
	});
	
	$enet('input.has_placeholder','div.enet').each(function(){
		var placeholder = $enet('<div class="placeholder">' + $enet(this).attr('data-placeholder') + '</div>')
		.css({'top': $enet(this).position().top, 'left': $enet(this).position().left})
		$enet(this).after(placeholder);
	});
});


function enet_counter(){
	var SELF = $enet('#enet_countdown');
	if(!SELF.length){return false;}
	if(!SELF.data('deadline')){return false;}
	var date = SELF.data('deadline').toDate();
	var today = new Date();
	var diff = parseInt((date - today) / 1000, 10);
	var counter = [0,0,0,0];
	if(diff>0){
		var d = Math.floor(diff/60/60/24);
		var h = Math.floor((diff/60/60) - (d*24));
		var m = Math.floor((diff/60) - (h*60) - (d*60*24));
		var s = Math.floor((diff) - (m*60) - (h*60*60) - (d*60*60*24));
		counter = [d.leadZero(), h.leadZero(), m.leadZero(), s.leadZero()]
	}
	
	for(i in counter){
		var tmp = counter[i].split('');
		SELF.find('li:eq(' + (i*2) + ')').html(tmp[0]);
		SELF.find('li:eq(' + ((i*2)+1) + ')').html(tmp[1]);
	}
	setTimeout(enet_counter, 1000);
}


/*String To Date*/
String.prototype.toDate = function(){
	var string = this.toString();
	var date_types = [
		{'pattern': new RegExp("(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})")},
		{'pattern': new RegExp("(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2})")},
		{'pattern': new RegExp("(\\d{4})-(\\d{2})-(\\d{2})")}
	];
	var date = false;
	for(var i in date_types){
		if(date_types.hasOwnProperty(i)){
			date = string.match(date_types[i].pattern);
			if(date){date.shift(); break;}
		}
	}
	if(date){
		return new Date(date[0], date[1]-1, date[2], date[3]||0, date[4]||0, date[5]||0);
	}else{
		return new Date();
	}
};

/*Add Leading Zero (String)*/
String.prototype.leadZero = function(count){
	count=(count)?count:2;
	var string = this.toString();
	count = count - string.length;
	for(var i=1; i<=count; i++){string='0'+string;}
	return string;
};

/*Add Leading Zero (Number)*/
Number.prototype.leadZero = function(count){return this.toString().leadZero(count);};

/*Date add*/
Date.prototype.add = function(inc, what){
	what=(what)?what:'d';
	switch(what){
		case 'd': return new Date(this.setDate(this.getDate()+inc));
		case 'm': return new Date(this.setMonth(this.getMonth()+inc));
		case 'y': return new Date(this.setYear(this.getFullYear()+inc));
		case 'h': return new Date(this.setHours(this.getHours()+inc));
		case 'i': return new Date(this.setMinutes(this.getMinutes()+inc));
		default: return this;
	}
};

/*Date Format Prototype*/
Date.prototype.format = function(formatStr) {
	var heap = formatStr.split("");
	var resHeap = new Array(heap.length);
	for (var i = 0; i < heap.length; i++) {
		var temp, firstDay, thisDay;
		switch(heap[i]) {
			case "\\":
				resHeap[i] = heap[i+1]; i++; break;
			case "a": /* "am" or "pm"*/
				temp = this.getHours(); resHeap[i] = (temp < 12) ? "am" : "pm"; break;
			case "A": /* "AM" or "PM"*/
				temp = this.getHours(); resHeap[i] = (temp < 12) ? "AM" : "PM"; break;
			case "d": /* day of the month, 2 digits with leading zeros; i.e. "01" to "31"*/
				temp = String(this.getDate()); resHeap[i] = (temp.length > 1) ? temp : "0" + temp; break;
			case "g": /* hour, 12-hour format without leading zeros; i.e. "1" to "12"*/
				temp = this.getHours(); resHeap[i] = (temp <= 12) ? temp : (temp - 12); break;
			case "G": /* hour, 24-hour format without leading zeros; i.e. "0" to "23"*/
				resHeap[i] = String(this.getHours()); break;
			case "h": /* hour, 12-hour format; i.e. "01" to "12"*/
				temp = String(this.getHours()); temp = (temp <= 12) ? temp : (temp - 12); resHeap[i] = (temp.length > 1) ? temp : "0" + temp; break;
			case "H": /* hour, 24-hour format; i.e. "00" to "23"*/
				temp = String(this.getHours()); resHeap[i] = (temp.length > 1) ? temp : "0" + temp; break;
			case "i": /* minutes; i.e. "00" to "59" */
				temp = String(this.getMinutes()); resHeap[i] = (temp.length > 1) ? temp : "0" + temp; break;
			case "I": /* "1" if Daylight Savings Time, "0" otherwise. Works only on the northern hemisphere*/
				firstDay = new Date(this.getFullYear(), 0, 1); resHeap[i] = (this.getTimezoneOffset() !== firstDay.getTimezoneOffset()) ? (1) : (0); break;
			case "J": /* day of the month without leading zeros; i.e. "1" to "31" */
				resHeap[i] = this.getDate(); break;
			case "m": /* month; i.e. "01" to "12"*/
				temp = String(this.getMonth() + 1); resHeap[i] = (temp.length > 1) ? temp : "0" + temp; break;
			case "n": /* month without leading zeros; i.e. "1" to "12"*/
				resHeap[i] = this.getMonth() + 1; break;
			case "s": /* seconds; i.e. "00" to "59"*/
				temp = String(this.getSeconds()); resHeap[i] = (temp.length > 1) ? temp : "0" + temp; break;
			case "y": /* year, 2 digits; i.e. "99"*/
				resHeap[i] = String(this.getFullYear()).substring(2); break;
			case "Y": /* year, 4 digits; i.e. "1999"*/
				resHeap[i] = this.getFullYear(); break;
			case "z": /* day of the year; i.e. "0" to "365" */
				firstDay = Date.UTC(this.getFullYear(), 0, 0);
				thisDay = Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()); 
				resHeap[i] = Math.floor((thisDay - firstDay) / (1000 * 60 * 60 * 24));
				break;
			case "W": /* ISO-8601 week number of year, weeks starting on Monday */
				var startOfYear = new Date(this.getFullYear(), 0, 1, 0, 0, 0, 0);
				firstDay = startOfYear.getDay() - 1;
				if (firstDay < 0) {firstDay = 6;}
				var firstMonday = Date.UTC(this.getFullYear(), 0, 8 - firstDay);
				thisDay = Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()); 
				resHeap[i] = Math.floor((thisDay - firstMonday) / (1000 * 60 * 60 * 24 * 7)) + 2;
				break;
			default:
				resHeap[i] = heap[i];
		}
	}
	return resHeap.join('');
};

/*Date Compare*/
Date.prototype.compare = function(date2){
	date1 = this.getFullYear()+'-'+this.getMonth()+'-'+this.getDate();
	date2 = date2.getFullYear()+'-'+date2.getMonth()+'-'+date2.getDate();
	return date1===date2;
}

/*Date Diff*/
Date.prototype.diff = function(date, kind){
	var today = new Date(this.getFullYear() + '-' + (this.getMonth()+1).leadZero() + '-' + (this.getDate()).leadZero());
	date = new Date(date);
	date = new Date(date.getFullYear() + '-' + (date.getMonth()+1).leadZero() + '-' + (date.getDate()).leadZero());
	switch(kind){
		default: /*Default 'd' (days)*/
			return (date-today) / 1000 / 60 / 60 / 24;
			break;
	}
}

