$(function(){

	/*-------------------------------
	| TAB MENU
	--------------------------------*/
	$(function(){
		$('#tabmenu ul a').livequery('click', function(){
			var uri = $(this).attr('href') + ' #content';
			$('#project_info').hide();
			$('#ajax_container').html('').hide().load(uri).slideDown();
			
			$('#tabmenu ul a').removeClass('active');
			$(this).addClass('active');
			
			return false;
		});
	});

	/*-------------------------------
	| SETTING MENU (HEADER)
	--------------------------------*/

	$('#cpanel')
		.hover(function(){
			$('#cpanel_inner').show();
		}, function(){
			$('#cpanel_inner').slideUp();
		});			

	/*-------------------------------
	| DETAIL NAVIGATION
	--------------------------------*/

	// first level ajax
	$('a#detail_1').livequery('click', function(){
		
		if ($('#parent').length) {
			var detailURI = $(this).attr('href');
			
			$('#parent').hide();
			$('#child_1').hide().load(detailURI + ' #result').fadeIn();
			
			return false;
		} else {
			return true;
		}
	});

	// second level ajax
	$('a#detail_2').livequery('click', function(){
		
		if ($('#parent').length) {
			var detailURI = $(this).attr('href');
			
			$('#parent').hide();
			$('#child_1').hide();
			$('#child_2').hide().load(detailURI + ' #result').fadeIn();
			
			return false;
		} else {
			return true;
		}
	});

	// third level ajax
	$('a#detail_3').livequery('click', function(){
		console.log('level 3'+$(this).attr('href'));
		if ($('#parent').length) {
			var detailURI = $(this).attr('href');
			
			$('#parent').hide();
			$('#child_1').hide();
			$('#child_2').hide();
			$('#child_3').hide().load(detailURI + ' #result').fadeIn();
			
			return false;
		} else {
			return true;
		}
	});

	// back to first level button
	$('a#back_1').livequery('click', function(){
		if ($('#parent').length) {
			$('#child_1').html('');
			$('#parent').fadeIn();
			return false;
		} else {
			return true;
		}
		
	});

	// back to second level button
	$('a#back_2').livequery('click', function(){
		if ($('#parent').length) {
			$('#child_2').html('');
			$('#child_1').fadeIn();
			return false;
		} else {
			return true;
		}
	});

	// back to third level button
	$('a#back_3').livequery('click', function(){
		if ($('#parent').length) {
			$('#child_3').html('');
			$('#child_2').fadeIn();
			return false;
		} else {
			return true;
		}
	});

	/*-------------------------------
	| SIDEBAR NAVIGATION
	--------------------------------*/
	

	$('.navigation a').livequery('click', function(){
		if (!$('#ajaxload').length){
			$('#container').append('<div id="ajaxload"><div>Loading Page..</div></div>');
		}
		$('#ajaxload').show();
		
		var pageURI = $(this).attr('href') + ' #data';
		
		if(pageURI.substring(0,1)=="#"){	
		} else {
			//console.log($('#content').height());
			$('.navigation a').removeClass('selected');
			$(this).addClass('selected');
			
			$('#content').html('').hide().load(pageURI).fadeIn();
		}
		$('#ajaxload').fadeOut();
		
		return false;
	});

	//Request a page but ignore the return results.
	$('a#ignore').livequery('click', function(){		
		var pageURI = $(this).attr('href');
		$.get(pageURI);
		return false;
	});

	/*--------------------------------------------------------------
	| OTHER JQUERY
	---------------------------------------------------------------*/

	$('.datepicker').livequery(function(){
		//$(this).datepicker({ dateFormat: 'yy-mm-dd', maxDate: '+1M' });
		$(this).datepicker({ dateFormat: 'yy-mm-dd'});
	});


	/*-------------------------------
	| AJAX LOADING ANIMATION
	--------------------------------*/

	$('#ajaxload').livequery(function(){
		var headerDimensions = $('#header').height();
		var subheaderDimensions = $('#subheader').height();
		var contentWidth = $('#content').width();
		var sidebarWidth = $('#sidebar').width();
		
		$(this).height( $(window).height() - headerDimensions - subheaderDimensions - 1 ) ;
		$(this).width( $(window).width() - sidebarWidth - 1);
		$(this).css('top', headerDimensions + subheaderDimensions + 41);
		$(this).find('div').css('margin-top', $(this).height() / 2 - 50);

	});

	/*-------------------------------
	| MINOR FUNCTIONS (PERNAK-PERNIK)
	--------------------------------*/
	
	//company -> project
	$('#error_company').livequery('change',function(){

		var idcompany 	= 	$('#error_company').val();
		var url 		= 	$(this).attr('rel');

		if (idcompany!='default'){
			$.getJSON(url+'/'+idcompany, function(data){
				$('.optionProject').remove();
				$('#error_newProject').slideDown();
				
					$.each(data, function(i,item){
						var opt = "<option value='" + item.id + "' class='optionProject' >" +item.project+ "</option>";
						$('#error_project').append(opt);
						//console.log(opt);
					});
				
			});
		} else {
			$('.optionProject').remove();
		}
		
	});
	
	//new price category
	$('#price_cat').livequery('change', 
		function(){
			var price_cat = $('#price_cat').val();
			//console.log(price_cat);
			if(price_cat == 'create_new'){
				$('#new_cat').slideDown();
				$('#new_cat').removeAttr('disabled');
			} else {
				$('#new_cat').slideUp();
				$('#new_cat').attr('disabled','disabled');
			}
		}
	);

	//Confirm delete error
	$('a.confirm_delete').livequery('click', function(){
		var id = $(this).attr('rel');
		var url = $(this).attr('href');
		
		var answer = confirm("Delete Error number "+ id +"?");
		if (answer){
			alert("Deleted")
			window.location = url;
		} else {
			return false;
		}		
	});

	$('#reupload_cbox').livequery('change',function(){
		var thisCheck = $(this);
		//console.log('aghaha');
		if (thisCheck.is (':checked')){
			$('#album_seq_textbox').removeAttr('disabled');
			$('#album_seq').slideDown();
		} else {
			$('#album_seq_textbox').attr('disabled','disabled');
			$('#album_seq').slideUp();
		}
	});
	//select all for add catalogue
	$("input.select_all").livequery('change', function(){
		var checked_status = this.checked;
		$("input.member").each(function(){
			this.checked = checked_status;
		});
		
		var thisCheck = $(this);
		if (thisCheck.is (':checked')){
			$('select.member').removeAttr('disabled');
		} else {
			$('select.member').attr('disabled','disabled');
		}
	});
	
	//disable select price if unchecked
	$("input.member").livequery('change', function(){
		var thisCheck = $(this);
		if (thisCheck.is (':checked')){
			$('#member_'+this.value).removeAttr('disabled');
		} else {
			$('#member_'+this.value).attr('disabled','disabled');
		}
	});	
	
	//disable select price if unchecked
	$("input.member_edit_cat").livequery('change', function(){
		var thisCheck = $(this);
		if (thisCheck.is (':checked')){
			$('#member_edit_cat_'+this.value).attr('disabled','disabled');
		} else {
			$('#member_edit_cat_'+this.value).removeAttr('disabled');
		}
	});
	
	//Auto complete label	
	$("#search_label").livequery('keypress', function(){
		//console.log(this.value);
		$("#search_label").autocomplete('http://cms.idmusiccloud.com/contract/search_label/', {
			multiple: false,
			dataType: "json",
			parse: function(data) {
				return $.map(data, function(row) {
					return {
						data: row,
						value: row.name,
						result: row.form_number
					}
				});
			},
			formatItem: function(item) {
				return format(item);
			}
		})
	});
	
	function format(label) {
		return label.name/* + " &lt;" + label.form_number + "&gt"*/;
	}
	
	//Auto complete artist	
	$("#search_artist").livequery('keypress', function(){
		//console.log(this.value);
		$("#search_artist").autocomplete('http://cms.idmusiccloud.com/track/search_artist/', {
			multiple: false,
			dataType: "json",
			parse: function(data) {
				return $.map(data, function(row) {
					return {
						data: row,
						value: row.name,
						result: row.artist_id
					}
				});
			},
			formatItem: function(item) {
				return format(item);
			}
		})
	});
	
	//Validate file upload
	/*$('form').submit(function(event) {
		alert('xxx');
		return false;
	});*/
	
	$(".upload_content").livequery('change', function(){
		var filename = $(this).val().replace(/C:\\fakepath\\/i, '')
		filename	=	filename.substring(0, 13);//filename
		idring		=	$(this).attr('name').substring(0, 13);
		if (filename!=idring){
			console.log('Wrong filename!');
			if (!$('#notification_'+idring).length) {
				$('#notification').prepend('<p class="notification error" id="notification_'+idring+'">Check filename for '+idring+'!</p>');
			}
		} else {
			$('#notification_'+idring).remove();
		}
	});
	
	//notification close
	$(".notification a.close").livequery('click', function(){
		$(this).parent().fadeTo(400, 0, function () {
			$(this).slideUp(200);
		});
		return false;
	});
	
	//filter tracklist
	$('form#filter_tracklist').livequery('submit', function(){
		var form_number 	= $(this).find('#form_number').val();
		var status 			= $(this).find('#status').val();
		var formURI			= $(this).attr('action');
		var data 			= "form_number=" + form_number + "&status=" + status;		
		
		if (!$('#ajaxload').length){
			$('#container').append('<div id="ajaxload"><div>Loading Page..</div></div>');
		}
		$('#ajaxload').show();
	
		$('#filtered').html('');		
		$.ajax({
			type: "POST",
			url: formURI,
			data: data,
			success: function(msg){
				var content = $(msg).find( '#filtered' );
				$('#filtered').hide().html(content).fadeIn();
			}
		});
		
		$('#ajaxload').fadeOut();
		
		return false;
	});
	
	$('#pic1, #pic2').livequery('change',function(){
		var pic1	= $('#pic1').val();
		var pic2	= $('#pic2').val();
		
		if (pic1==pic2){
			console.log('disable');
			$('#notification').hide().html('<p class="notification error">PIC 1 and 2 must be different!</p>').fadeIn();
			$('#submit_kiosk_contract').attr('disabled','disabled');
		} else {
			console.log('enable');
			$('#notification').slideUp().html('');
			$('#submit_kiosk_contract').removeAttr('disabled');
		}
	});
		
});// end jquery	
