$(document).ready(function() {

    $("#morePhones").live("click", function(){
	$(this).slideUp("fast", function(){
	    $(this).next().slideDown("fast");
	});
		
    });
	
    if($("#CompanyFormSimple_tel2").val() || $("#CompanyFormSimple_tel3").val() ){
	$("#morePhones").slideUp("fast", function(){
	    $("#morePhones").next().slideDown("fast");
	});
    }
		
	
    $("#CompanyFormSimple_region_id").live("change", function(){
	if($(this).val()>0){
	    $("#CompanyFormSimple_cityName").removeClass("disabled");
	}
	else{
	    $("#CompanyFormSimple_cityName").addClass("disabled").val("").closest('.formRow').next().find('input').addClass("disabled").val("");
	}
	return false;
    });

    /******TOOLTIP*************/
    $(".newTooltipCity").tooltip({
	relative:true,
	events: {
	    def:      "click,mouseout"
	},
	onBeforeShow:function(){
	    if(!$(".newTooltipCity :input").hasClass("disabled"))
		return false;
	}

    });
    $(".newTooltipStreet").tooltip({
	relative:true,
	events: {
	    def:      "click,mouseout"
	},
	onBeforeShow:function(){
	    if(!$(".newTooltipStreet :input").hasClass("disabled"))
		return false;
	}

    });

    /*************************/

    /*OFFICE*/
    $("#officeForm :disabled").each(function(){
	if($(this).val())
	    $(this).removeAttr("disabled");
    });
    if($("#OfficeForm_cityName").val()){
	$("#OfficeForm_streetName").removeAttr("disabled");
	$("#OfficeForm_ulica_nr").removeAttr("disabled");
	$("#OfficeForm_lokal_nr").removeAttr("disabled");
	$("#OfficeForm_kod").removeAttr("disabled");
    }
	
	
    $("#OfficeForm_region_id").live("change", function(){
	if($(this).val()>0)
	    $("#OfficeForm_cityName").removeAttr("disabled");
	else
	    $("#OfficeForm_cityName").attr("disabled", "disabled");
		
	return false;
				
    });

    $("#OfficeForm_cityName").keyup(function(){
	$("#OfficeForm_streetName").removeAttr("disabled");
    });
    $("#OfficeForm_streetName").keyup(function(){
	$("#OfficeForm_ulica_nr").removeAttr("disabled");
	$("#OfficeForm_lokal_nr").removeAttr("disabled");
	$("#OfficeForm_kod").removeAttr("disabled");
    });
	
    $("#officesList a.edit").click(function(){

	var id = $(this).closest("li").attr("id");
	$.ajax({
	    type: "POST",
	    dataType: "json",
	    url: "/Companies/AOffices",
	    data: ({
		id:id
	    }),
	    success: function(result){
		$("#OfficeForm_region_id").val(result.region_id);
		$("#OfficeForm_cityName").val(result.city);
		$("#OfficeForm_city_id").val(result.city_id);
		$("#OfficeForm_streetName").val(result.adres);
		$("#OfficeForm_street_id").val(result.street_id);
		$("#OfficeForm_ulica_nr").val(result.ulica_nr);
		$("#OfficeForm_lokal_nr").val(result.lokal_nr);
		$("#OfficeForm_kod").val(result.kod);
		$("#OfficeForm_url").val(result.url);
		$("#OfficeForm_email").val(result.email);
		$("#OfficeForm_fax").val(result.fax);
		$("#OfficeForm_tel1").val(result.tel1);
		$("#OfficeForm_tel2").val(result.tel2);
		$("#OfficeForm_tel3").val(result.tel3);
		$("#office_id").val(id);
					
		$("#officeForm form *").removeAttr("disabled");
		if( result.tel2 || result.tel3 ){
		    $("#morePhones").slideUp("fast", function(){
			$("#morePhones").next().slideDown("fast");
		    });
		}
		else{
		    $("#morePhones").next().slideUp("fast", function(){
			$("#morePhones").slideDown("fast");
		    });
		}
	    }
	});
		
    });
	
	
	
    $("#officesList a.delete").click(function(){
	var li = $(this).closest("li");
	var company_id = li.attr("rel");
	var id = li.attr("id");
	$.ajax({
	    type: "POST",
	    url: "/Companies/deleteOffice",
	    data: ({
		id:id,
		company_id:company_id
	    }),
	    success: function(result){
		$(li).html(result);
						
		$(li).fadeTo("slow",0, function(){
		    $(this).slideUp("fast");
		});
						
	    }
	});
    });
		

	
	
    /***********/
	
    /*DISABLE*/

    $(".newTooltipStreet :input").focus(function(){
	if(	$(this).hasClass("disabled") )
	    $(this).blur();

    });

    $("#CompanyFormSimple_cityName").focus(function(){
	if(	$("#CompanyFormSimple_region_id").val()<1){
	    $(this).blur();
	}
    });

    $("#CompanyFormSimple_cityName").keyup(function(){
	$("#CompanyFormSimple_streetName").removeClass("disabled");
    });
    $("#CompanyFormSimple_streetName").keyup(function(){
	$("#CompanyFormSimple_ulica_nr").removeClass("disabled");
	$("#CompanyFormSimple_lokal_nr").removeClass("disabled");
	$("#CompanyFormSimple_kod").removeClass("disabled");;
    });

    if($("#CompanyFormSimple_region_id").val()>0)
	$("#CompanyFormSimple_cityName").removeClass("disabled");

    if($("#CompanyFormSimple_cityName").val()){
	$("#CompanyFormSimple_cityName").removeClass("disabled");
	$("#CompanyFormSimple_streetName").removeClass("disabled");
	$("#CompanyFormSimple_ulica_nr").removeClass("disabled");
	$("#CompanyFormSimple_lokal_nr").removeClass("disabled");
	$("#CompanyFormSimple_kod").removeClass("disabled");
    }

    if($("#CompanyFormSimple_streetName").val()){
	$("#CompanyFormSimple_ulica_nr").removeClass("disabled");
	$("#CompanyFormSimple_lokal_nr").removeClass("disabled");
	$("#CompanyFormSimple_kod").removeClass("disabled");
    }






	
    /*remove brands*/
    $(".removeBrand").live("click", function(){
	$(this).prev(".brands").html('');
	$(this).next(".brandId").val(null);
	$(this).hide();
		
    }).css("margin-left", "20px");
	
    $(".brands").live("change", function(){
	$(this).next().show();
			
    });
	
    $(".removeBrand").queue(function(){
	if($(this).prev(".brands").html() == '') $(this).hide();
    });
	
    /*upload*/
    if($("#uploadify").length)
    {
		
	$("#uploadify").uploadify({
	    'uploader'      : 	'/images/uploadify.swf',
	    'script'        : 	'/Companies/uploadLogo',
	    'scriptData'	:	{
		'company_id':$("#companyId").html()
		},
	    'cancelImg'     : 	'/images/cancel.png',
	    'folder'        : 	'uploads',
	    'queueID'       : 	'fileQueue',
	    'auto'          : 	true,
	    'multi'         : 	false,
	    'buttonText'	: 	'Dodaj logo',
	    'fileDataName'	:	'uploadedLogo',
	    'sizeLimit'		:	51200,
	    //		'onSelect'		:	function(event,queueID,fileObj) {
	    //								if(fileObj.size>100000){
	    //									alert('Za duży');
	    //									uploadifyCancel(queueID);
	    //
	    //								}
	    //							},
	    'onComplete'	:	function(event,queueID,fileObj,response,data) {
		$('#logo').html('<img src="/images/logo/' + response + '" />');
		$("#CompanyFormExtended_logo").val(response);
	    }
	});
    }
	
	
	
	
}); 


function setBrand(brandId, brandName){
	
    var i = 0;
    for(i=0;i<3;i++){
	if( !$(".brandId:eq("+ i +")").val()){
	    $(".brandId:eq("+ i +")").val(brandId);
	    $(".brands:eq("+ i +")").html(brandName);
	    $(".brands:eq("+ i +")").next().show();
	    break;
	}
    }
		
}


function pause(millis)
{
    var date = new Date();
    var curDate = null;
	
    do {
	curDate = new Date();
    }
    while(curDate-date < millis);
}

