var IN = {

    mainDiv: 'leftframe',

    userDiv: 'user_box',

    pollDiv: 'poll_content',

    loginInputCheck: function(obj, name){

        if (obj.value == name) {
            obj.value = '';
        }
        else
            if (obj.value == '') {
                obj.value = name;
            }

    },

    callObject: function(url, data, target){

        $.post(url, data, function(html){
            $("#" + target).html(html);
			IN.standartListRefresh();
            return false;
        });


    },

    shoutBox : {

    	timerId : 0,

    	checkTime : 4500,

    	url : 'shoutbox',

    	postUrl : 'shoutbox/post',

    	archiveUrl : 'shoutbox/archive',

		refreshShoutBox : function(){

			if (this.timerId)
                window.clearTimeout(this.timerId);
            this.timerId = setTimeout("IN.shoutBox.callIt()", IN.shoutBox.checkTime);

	    },

	    callIt : function(){

	    	data = {};

			$.post(siteurl + '' + this.url, data, function(html){
				if(html!=$('#shoutbox').html()){
					$('#shoutbox').html(html);
				}
	            return false;
	        });

	    	this.refreshShoutBox();

	    },

	    archive : function(){

	    	window.clearTimeout(this.timerId);

	    	data = {};

			$.post(siteurl + '' + this.archiveUrl, data, function(html){
				$('#shoutbox').html(html);
	            return false;
	        });

	    },

	    post : function(){

	    	data = {};
	    	data.text = $('#new_shout').val();

	    	$.post(siteurl + '' + this.postUrl, data, function(html){
				$('#new_shout').val('');
				IN.shoutBox.callIt();
	            return false;
	        });

	    }

    },

    showLoader: function(target){

        $('#' + target).html('<img style="margin:auto;" width=20 height=20 src="' + ui + 'img/loader.gif" />');

    },

    openRegistration: function(){

        this.callObject(siteurl + 'authorization/register', null, this.mainDiv);
        this.showLoader(this.mainDiv);

    },

    openPasswordRecover: function(){

        this.callObject(siteurl + 'authorization/recover', null, this.mainDiv);
        this.showLoader(this.mainDiv);

    },

    makeLogin: function(){

        var data = {};
        data.login = 1;
        data.username = $('#username').val();
        data.password = $('#password').val();

        this.callObject(siteurl + 'authorization/login', data, this.userDiv);

        this.showLoader(this.userDiv);

    },

    makeLogout: function(){

        var data = {};
        data.login = 1;

        this.callObject(siteurl + 'authorization/logout', data, this.userDiv);

        this.showLoader(this.userDiv);

    },

    checkFields: function(list){

        $('.er1').remove();

        var status = true;

        for (var n = 0; n < list.length; n++) {

            if (list[n] != undefined) {

                if (!$('#' + list[n]).val() || $('#' + list[n]).val() == 0) {

                    $('#' + list[n]).after('<span class="er1">Neįvestas</span>');

                    status = false;

                }
                else
                    if ($('#' + list[n]).attr('minlength') > 0 && $('#' + list[n]).val().length < $('#' + list[n]).attr('minlength')) {

                        $('#' + list[n]).after('<span class="er1">Per trumpas</span>');

                        status = false;

                    }

            }

        }

        return status;

    },

    collectFormAndSend: function(url, fields, div){

        var data = {};

        for (var n = 0; n < fields.length; n++) {
            data[fields[n]] = $('#' + fields[n]).val();
            if ($('#' + fields[n]).attr('type') == 'checkbox') {
                data[fields[n]] = ($('#' + fields[n]).attr('checked') == true) ? 1 : 0;
            }
        }

        this.callObject(siteurl + '' + url, data, div);
        this.showLoader(div);

    },

    changeCaptcha: function(div){

        $('#' + div).attr('src', siteurl + 'in/libs/securimage/securimage_show.php?sid=' + Math.random());

    },

    makeRegistration: function(){

        var status = this.checkFields(['f_name', 'f_pass', 'f_pass2', 'f_email', 'f_title', 'f_code', 'f_address', 'f_agree']);

        //var status = true;

        if (status == true && $('#f_name').attr('ok') == 'true') {

            if ($('#f_pass').val() != $('#f_pass2').val()) {
                $('#f_pass').after('<span class="er1">Nesutampa</span>');
                $('#f_pass2').after('<span class="er1">Nesutampa</span>');
            }
            else {
                this.collectFormAndSend('authorization/register/submit', ['f_name', 'f_pass', 'f_pass2', 'f_email', 'f_title', 'f_code', 'f_vat', 'f_address', 'f_country', 'f_phone', 'f_fax', 'f_captcha'], this.mainDiv);
            }

        }

    },

    makeRegistration2: function(){

        this.collectFormAndSend('authorization/register/submit', ['f_second', 'f_captcha'], this.mainDiv);

    },

    makeProfileEdit: function(){

        var status = this.checkFields(['f_email', 'f_title', 'f_phone', 'f_contact_person', 'f_code', 'f_address']);

        if (status == true) {

            this.collectFormAndSend('profile/edit/submit', ['f_profile_update', 'f_email', 'f_icq', 'f_www', 'f_title', 'f_code', 'f_vat', 'f_address', 'f_country', 'f_new_pass1','f_new_pass2','f_phone', 'f_mobile_phone', 'f_fax', 'f_contact_person', 'f_company_type', 'f_carriage_by_road', 'f_air_carriage', 'f_rail_carriage', 'f_sea_shipping', 'f_container_transportation', 'f_by_country', 'f_international', 'f_about'], this.mainDiv);

        }

    },

    objClassified : {

	    getModelsByBrand : function(id,select){

			IN.callObject(siteurl + 'classified/edit/model/get', {brand:id,select:select}, 'f_model_div');

        	IN.showLoader('f_model_div');

	    },

	    openViewPopup : function(id){

	    	var newwindow;

			newwindow=window.open(siteurl + 'classified/view/' + id , 'classified' , 'height=600,width=810,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,top=0,location=yes');
			if (window.focus) {newwindow.focus()}

	    }

    },

    makeClassifiedEdit: function(){

        var status = this.checkFields(['f_title', 'f_model_title' ,'f_brand', 'f_type', 'f_wheels', 'f_fuel', 'f_made', 'f_run', 'f_country' , 'f_price']);

        if (status == true) {

            this.collectFormAndSend('classified/edit/submit', ['f_classified_update', 'f_id', 'f_title', 'f_descr' , 'f_model', 'f_country' , 'f_run', 'f_model_title' , 'f_brand', 'f_type', 'f_wheels', 'f_power', 'f_power_type', 'f_neto', 'f_bruto', 'f_fuel', 'f_euro', 'f_made', 'f_price', 'f_with_vat', 'f_curency' , 'f_anon_cookie'], this.mainDiv);

        }

    },

    checkFreename: function(){

        var username = $('#f_name').val();

        if (username.length > 2) {

            $.post(siteurl + 'authorization/register/freename', {
                username: username
            }, function(html){
                $('.ok1').remove();
                $('.er1').remove();
                if (html == "true") {
                    $('#f_name').after('<span class="er1">Vardas užimtas</span>');
                    $('#f_name').attr('ok', 'false');
                }
                else {
                    $('#f_name').after('<span class="ok1">Vardas laisvas</span>');
                    $('#f_name').attr('ok', 'true');
                }
                return false;
            });

        }

    },

    submitPollVote: function(){

        var data = {};
        data.poll = $('#poll_id').val();
        data.vote = $("input[@name=poll_vote]:checked").val();

        this.callObject(siteurl + 'polls', data, this.pollDiv);

        this.showLoader(this.pollDiv);

    },

    callMainObject: function(url, data){

        this.callObject(siteurl + '' + url, data, this.mainDiv);

        this.showLoader(this.mainDiv);


    },

    openRootUrl: function(){

        location.href = siteurl;

    },

    openUrl: function(url){

        location.href = url;

    },

    recoverPassword: function(){

        var data = {};
        data.recover = 1;
        data.username = $('#f_name').val();
        data.email = $('#f_email').val();

        this.callObject(siteurl + 'authorization/recover/submit', data, this.mainDiv);

        this.showLoader(this.mainDiv);

    },

    onResize: function(){
        var width = $(window).width();
        $('#content').css('width', (width - 250));
        $('.centerBlock').css('width', (width - 270));
        $('.centerBlockHeader').css('width', (width - 278));
        $('#status').val(($('#status').val() * 1) + 1);
    },

	objFilter : {

		onOpenClose : function(){
			$('.filter .inputs').toggle();
			$('.filter .closer').toggle();
			$('.filter .opener').toggle();
		}

	},

	objFavorites : {

		addItem : function(type,id){

			var data = {};

			data.type = type;

			data.id = id;

			$.post(siteurl + 'favorites/add', data, function(html){
				$("#add_favorites_block").html(html);
				return false;
	        });

		}

	},

    objLoad: {

        cargoPointCount: 0,

		filterByCountryAndType : function(country,type){

			var data = {};

			data.load_cargo_point_0_country = country;

			data.load_cargo_point_0_type 	= type;
			data.load_cargo_point_count 	= 1;

			$.post(siteurl + 'load', data, function(html){
	            //$("#f_cargo_point_" + targetID + "_city").html(html);
	            //return false;
				location.href = siteurl + 'load';
	        });

		},

        addCargoPoint: function(){

            this.cargoPointCount++;

            var temp = $('#load_cargo_points_template').html();

            temp = temp.replace(/ID1/g, this.cargoPointCount);

            temp = temp.replace(/ID2/g, (this.cargoPointCount + 1));

            $('#load_cargo_points').append(temp);

            $('#f_load_cargo_point_count').val(this.cargoPointCount + 1);

            delete (temp);

        },

		removeCargoPoints : function(){

			this.cargoPointCount = -1;

			 $('#f_load_cargo_point_count').val(0);

			$('#load_cargo_points').html('');

		},

        submitEdit: function(){

            var status = IN.checkFields([
				'f_cargo_point_0_country',
				'f_cargo_point_1_country',
				'f_denomination',
				'f_date',
				'f_load_status',
				'f_payment_mode'
				]);

            if (status == true) {

                var submit = [
					'f_update_id',
					'f_denomination',
					'f_run_circle',
					'f_service_custom_clearing_needed',
					'f_volume',
					'f_weight',
					'f_length',
					'f_number_carriers',
					'f_carrier_type',
					'f_carrier_period',
					'f_additional_info',
					'f_load_cargo_point_count',
					'f_price',
					'f_curency',
					'f_date',
					'f_europallets',
					'f_hoist',
					'f_manipulator',
					'f_load_status',
					'f_adr',
					'f_cmr',
					'f_tir',
					'f_emkt',
					'f_t1',
					'f_t2',
					'f_payment_mode',
					'f_tr1',
					'f_tr2_a',
					'f_tr2_r',
					'f_tr2_s',
					'f_tr2_i',
					'f_tr3'
					];

                for (var i = 0; i <= this.cargoPointCount; i++) {
                    submit.push('f_cargo_point_' + i + '_country', 'f_cargo_point_' + i + '_detail', 'f_cargo_point_' + i + '_city', 'f_cargo_point_' + i + '_type');
                }

                IN.collectFormAndSend('load/manage/edit/submit', submit, IN.mainDiv);

            }

        }

    },

	objBlackwhite : {

		findUser : function(){

			var data = {};
	        data.username = $('#user_find').val();

	        IN.callObject(siteurl + 'blackwhite/comment/finduser', data, 'step_1_results');

	        IN.showLoader('step_1_results');

		}

	},

	 objTransport: {

        cargoPointCount: 0,

		filterByCountryAndType : function(country,type){

			var data = {};

			data.transport_cargo_point_0_country = country;

			data.transport_cargo_point_0_type 	= type;
			data.transport_cargo_point_count 	= 1;

			$.post(siteurl + 'transport', data, function(html){
	            //$("#f_cargo_point_" + targetID + "_city").html(html);
	            //return false;
				location.href = siteurl + 'transport';
	        });

		},

        addCargoPoint: function(){

            this.cargoPointCount++;

            var temp = $('#transport_cargo_points_template').html();

            temp = temp.replace(/ID1/g, this.cargoPointCount);

            temp = temp.replace(/ID2/g, (this.cargoPointCount + 1));

            $('#transport_cargo_points').append(temp);

            $('#f_transport_cargo_point_count').val(this.cargoPointCount + 1);

            delete (temp);

        },

		removeCargoPoints : function(){

			this.cargoPointCount = -1;

			 $('#f_transport_cargo_point_count').val(0);

			$('#transport_cargo_points').html('');

		},

        submitEdit: function(){

            var status = IN.checkFields([
				'f_cargo_point_0_country',
				'f_cargo_point_1_country',
				'f_date',
				'f_load_status',
				'f_payment_mode'
				]);

            if (status == true) {

                var submit = [
					'f_update_id',
					'f_denomination',
					'f_run_circle',
					'f_service_custom_clearing_needed',
					'f_volume',
					'f_weight',
					'f_length',
					'f_number_carriers',
					'f_carrier_type',
					'f_carrier_period',
					'f_additional_info',
					'f_transport_cargo_point_count',
					'f_price',
					'f_curency',
					'f_date',
					'f_europallets',
					'f_hoist',
					'f_manipulator',
					'f_load_status',
					'f_adr',
					'f_cmr',
					'f_tir',
					'f_emkt',
					'f_t1',
					'f_t2',
					'f_payment_mode',
					'f_tr1',
					'f_tr2_a',
					'f_tr2_r',
					'f_tr2_s',
					'f_tr2_i',
					'f_tr3'
					];

                for (var i = 0; i <= this.cargoPointCount; i++) {
                    submit.push('f_cargo_point_' + i + '_country', 'f_cargo_point_' + i + '_detail', 'f_cargo_point_' + i + '_city', 'f_cargo_point_' + i + '_type');
                }

                IN.collectFormAndSend('transport/manage/edit/submit', submit, IN.mainDiv);

            }

        }

    },

    dfxWinXY: function(w){
        var b, d, x, y, sx, sy, v;
        x = y = sx = sy = 0;
        if (w.innerWidth && w.innerHeight) {
            x = w.innerWidth;
            v = w.document.body.offsetWidth;
            if (v && (1 < v) && !(x < v)) // scrollbar width problem
                x = v - 1;
            y = w.innerHeight;
            sx = w.pageXOffset || 0;
            sy = w.pageYOffset || 0;
        }
        else {
            d = w.document;
            if (d.body) {
                b = d.documentElement.clientWidth ? d.documentElement : d.body; // IE 6 strict dtd
                x = b.clientWidth || 0;
                y = b.clientHeight || 0;
                sx = b.scrollLeft || 0;
                sy = b.scrollTop || 0;
            }
        }
        return {
            x: x,
            y: y,
            sx: sx,
            sy: sy
        };
    },

    oResize: {
        checkTime: 500,
        oldXY: null,
        timerId: 0,
        check1: function(){
            IN.oResize.check2()
        },
        check2: function() // call as method of oResize
        {
            if (IN.oResize.timerId)
                window.clearTimeout(IN.oResize.timerId);
            IN.oResize.timerId = setTimeout("IN.oResize.check3()", IN.oResize.checkTime);

        },
        check3: function(){
            var newXY = IN.dfxWinXY(window);
            IN.oResize.timerId = 0;
            if ((newXY.x != IN.oResize.oldXY.x) ||
            (newXY.y != IN.oResize.oldXY.y)) {
                IN.oResize.oldXY = newXY;
                IN.oResize.onafterresize();
            }
        },
        onafterresize: function(){
            IN.onResize();
        }
    },

	standartListRefresh : function(){
		 $('.r1').mouseover(function(){
	        $(this).attr('class', 'r1o');
	    });
	    $('.r1').mouseout(function(){
	        $(this).attr('class', 'r1');
	    });
	    $('.r2').mouseover(function(){
	        $(this).attr('class', 'r2o');
	    });
	    $('.r2').mouseout(function(){
	        $(this).attr('class', 'r2');
	    });


		 $('.r1b').mouseover(function(){
	        $(this).attr('class', 'r1o');
	    });
	    $('.r1b').mouseout(function(){
	        $(this).attr('class', 'r1b');
	    });
	    $('.r2b').mouseover(function(){
	        $(this).attr('class', 'r2o');
	    });
	    $('.r2b').mouseout(function(){
	        $(this).attr('class', 'r2b');
	    });

		tooltip();

	},

	showBigImage : function(id){

		$('.big_img').hide();

		$('#big_img_'+id).show();

	},

	getCities : function(object,targetID){

		var data = {};

		data.country = $(object).val();

		$.post(siteurl + 'load/manage/edit/country/city/get', data, function(html){
            $("#f_cargo_point_" + targetID + "_city").html(html);
            return false;
        });

	}

}
