Date.firstDayOfWeek = 1;
Date.format = 'yyyy-mm-dd';

$(document).ready(function() {
	$('fieldset#english_text').show();
    //hide show translations in master
    $('ul#translations_navigation li a').click(function(){
        $(this).parents('ul').children('li').removeClass('current_item');
        $(this).parents('li').addClass('current_item');
        $('form#admin_add fieldset').hide();
        //show selected
        var show = $(this).parents('li').attr('id').replace('show_', '');
        $('fieldset#' + show + '_text').show();
        });
	
    //fadeout saved notice
    if ($('span#saved').length) {
        setTimeout('$("span#saved").fadeOut("slow")', 1000);
    }
    
    //delete site
    $('a#delete_site').click(function(){
        if (confirm('Really Delete?')) {
            $('form#delete_site_form').submit();
        }
        });
    
    //onchange form submit
    $('form#set_status').change(function(){
        $(this).submit();
        });
    
    //ajax form for auto_add
    if ($('form#auto_add').length) {
    	$('form#auto_add').ajaxForm(function(data) {
    		try {
    			var json = eval("(" + data + ")");
    			if ($('input[name=model_name]', document.forms['admin_add']).length)
    				$('input[name=model_name]', document.forms['admin_add']).val(json[0].post_title);
    			if ($('input[name=news_title]', document.forms['admin_add']).length)
    				$('input[name=news_title]', document.forms['admin_add']).val(json[0].post_title);
    			if ($('input[name=work_title]', document.forms['admin_add']).length)
    				$('input[name=work_title]', document.forms['admin_add']).val(json[0].post_title);
    			if ($('input[name=model_code]', document.forms['admin_add']).length)
    				$('input[name=model_code]', document.forms['admin_add']).val(json[0].post_name);
    			document.getElementById('last_update').innerHTML = "Last update: " + json[0].post_modified;
    			// Show Images
          $("img[@id=show_thumbnail]").attr("src", "/images/size/100x/uploads/"+json[0].postmeta.thumbnail);
				$("img[@id=show_compo]").attr("src", "/images/size/600x/uploads/"+json[0].postmeta.composite_card);
    			switch (json[0].post_status)    			
    			{
    			case "draft":
    				$('#radio_pending').attr("checked","checked");
    				break;
    			case "publish":
    				$('#radio_approved').attr("checked","checked");
    				break;
    			}

    			$('form#set_status input[name=id]').val(json[0].ID);
    			$.each(json[0].postmeta, function(i, n){
    				if (n != '') {    				
    					if ($('input:text[name=' + i + ']', document.forms['admin_add']).length) {				
    						$('input:text[name=' + i + ']', document.forms['admin_add']).val(n);
    						$('input:text[name=' + i + ']', document.forms['admin_add']).parents('div.form_section_container').removeClass('closed');
    					}
    					if ($('textarea[name=' + i + ']').length) {
    						$('textarea[name=' + i + ']', document.forms['admin_add']).val(n);
    						$('textarea[name=' + i + ']', document.forms['admin_add']).parents('div.form_section_container').removeClass('closed');
    					}
    					if (i == 'category') {
    						$('select[name=category]', document.forms['admin_add']).val(json[0].postmeta.category);
    					}
    					if (i == 'date_of_birth' || i == 'work_date' || i == 'news_date') {                    	
    						var birthdate =  new Date(n);                    	
    						$('select[name=year]', document.forms['admin_add']).val(birthdate.getFullYear());
    						$('select[name=month]', document.forms['admin_add']).val(birthdate.getMonth()+1);
    						$('select[name=day]', document.forms['admin_add']).val(birthdate.getDate());                   	
    					}
    				}
    			});
    		}
    		catch (e) {
    			alert (e);
    		}
    	}); 
    }
    
    //collapsible form items
    $('div.form_section_container label.title').click(function(){
        $(this).parents('div.form_section_container').toggleClass('closed');
        });
    
    $('input#input_color').click(function(){
        $('div#picker').slideDown('slow');
        });

    $('input#input_color').blur(function(){
        $('div#picker').slideUp('slow');
        });

    if ($('div#picker').length) {
        $('div#picker').farbtastic('input#input_color');
    }
    
    //ajax form for serach models
    if ($('form#search_models').length) {   
         $('form#search_models').ajaxForm(function(data) {        
             var json = eval("(" + data + ")");
     		sel=$('#model_list');
     		sel.empty();     		
     		$.each(json, function(i, n){
     			if (json[i].post_name.length > 0) {
         			$("<option></option>")
   					.attr("value", json[i].ID)
   					.html("[" + json[i].post_name + "] - " +json[i].post_title)
   					.appendTo(sel);  
     			}
     			else {
         			$("<option></option>")
   					.attr("value", json[i].ID)
   					.html(json[i].post_title)
   					.appendTo(sel);  
     			}					
     		});
         }); 
         }
    
    $('select#model_list[@class="model_page"]').change(function(){
    	if($('select#model_list').val() != null)
    		$('#admin_add').resetForm();
    		$('input[name=mode]').val("edit");
    		$('input[name=post_id]').val($('select#model_list').val());
			document.getElementById('select').click();
        });
        
    //onchange form submit
    $('form#set_status').change(function(){
        $(this).submit();
        });
        
    $('input#delete').click(function(){
    	if (confirm('Really Delete?')) {
    	var type = $('#type').val();
    	var id = $('#model_list').val();
    	var y=document.getElementById('model_list');
		$.post(
  		'/admin/delete',
  		{
    		type: type,
    		id: id
  		}//,
  		//onAjaxSuccessRep
		);		
		y.remove(y.selectedIndex);
		alert('Delete successful');
    	}
    });
	function onAjaxSuccessRep(data) {
	    //alert(data);
		if (data.length) {
			alert('');
		    var json = eval("(" + data + ")");
		    var message = "Attention! \n There are websites containing links to the selected item \n";
		    $.each(json[0], function(ID, title){
		        message += title + "\n";
		    });
		    alert (message);
		}
	}

  $('.date-pick').datePicker({ startDate: '1996-01-01' }).each(function() {
    if ($(this).hasClass('date-current') && !$(this).val()) {
      $(this).val(new Date().asString()).trigger('change');
    }
  });
});

