var millenia3;

millenia3 = {
    show_messages: function() {
        var messages = $('div.message');
        messages.each(function() {
            $(this).fadeIn(500);
        });
        if (messages) {
            window.setTimeout('millenia3.hide_messages();', 4000);
        }
    },

    hide_messages: function() {
        $('div.message').each(function() {
           $(this).fadeOut(1000);
        });
    },
	
	forms: {
		
        init: function() {
            millenia3.forms.highlight_form_field();
        },
		
	    highlight_form_field: function(){
			$("input").focus(function(){
				$(this).parent().addClass("curFocus")
			});
			$("input").blur(function(){
				$(this).parent().removeClass("curFocus")
			});
		}
	},

    tablesorter: function() {
        $('table.tablesorter').each(function() {
           $(this).tablesorter();
        });
    },

    tooltips: function() {
        $('div.tooltip').each(function() {
            $('div.tooltip-link').click(function() {
                $(this).addClass('tooltip-link-active');
                var tcontent = $('div.tooltip-content', $(this).parent());
                tcontent.position({my: 'top left', at: 'bottom left', of: $(tcontent).parent(), collision: 'fit'});
                tcontent.fadeIn();
            })
            $('div.tooltip-content').each(function() {
                $(this).draggable();
                $(this).click(function() {
                    var tlink = $('div.tooltip-link', $(this).parent());
                    tlink.removeClass('tooltip-link-active');
                    $(this).fadeOut();
                });
            });
        })
    },

    long_requests: function() {
        $('a.long-request, input.long-request').click(function() {
            $('div#loading-screen').fadeIn();
        })
    },

    confirm_action: function() {
        $('.confirm-action').live('click', function() {
            if (!confirm("Are you sure?")) {
                return false;
            }
        });
    },

    redirect_buttons: function() {
        $('button.redirect-button').live('click', function() {
            window.location.replace($(this).attr('href'));
            return false;
        });
    },

    colorbox: function() {
        $('.colorbox').colorbox();
        $('.colorbox-iframe').colorbox({'iframe': true, 'width': '70%', 'height': '80%'});
    },

    colorbox_close: function() {
        if (parent.$("iframe").length > 0) {
            parent.$("iframe").colorbox.close();
            parent.location.reload();
        }
    },

    select_all_none: function() {
        $('a.select-all').live('click', function() {
            $('input[type=checkbox]').not('input.no-group').attr('checked', 'checked');
            return false;
        });
        $('a.select-none').live('click', function() {
            $('input[type=checkbox]').not('input.no-group').removeAttr('checked');
            return false;
        })
    },

    reports: {

        init: function() {
            millenia3.reports.report_edit();
            millenia3.reports.report_merge();
            millenia3.reports.new_reports();
        },

        report_merge: function() {
            $('#merge-button').click(function() {
                $('input.merge-mark').each(function() {
                    if ($(this).attr('checked')) {
                        var reports_merge = eval($(this).attr('reports'));
                        for (var i=0; i<reports_merge.length; i++) {
                            $('<input></input>')
                                .attr('name', 'report_id')
                                .attr('type', 'hidden')
                                .attr('value', reports_merge[i])
                                .appendTo('#merge-form');
                        }
                        $('#merge-form');
                    }
                });
            });

            $('input.merge-mark').live('click', function() {
                $('#merge-button').attr('disabled', 'disabled');
				$('#merge-button').removeClass('active-merge');
                $('input.merge-mark').each(function() {
                    if ($(this).attr('checked')) {
                        $('#merge-button').removeAttr('disabled');
                        // IE fix
                        $('#merge-button').css({"text-align" : "center"});
						$('#merge-button').addClass('active-merge');
                    }
                });
            });
        },

        report_edit: function() {

            $('#editable-title').each(function() {
                if (!$(this).hasClass('no-editor')) {
                    $(this).editableText({newlinesEnabled: false});
                }
            });

            $('#editable-title').live('change', function() {
                if ($(this).hasClass('no-editor')) {
                    return;
                }
                $(this).addClass('edit-loader');

                var report_id = $(this).attr('report');
                var value = $(this).text();

                Dajaxice.airplay.report_title_edit(Dajax.process, {'report_id': report_id, 'value': value});
            });

            $('table.report-edit td').each(function() {
                if (!$(this).hasClass('no-editor')) {
                    $('div.cell-edit', this).editableText({newlinesEnabled: false});
                }
            });

			$('table.report-edit td').live('click', function() {
				if (!$(this).hasClass('no-editor')) {
					/* $('div.cell-edit', $(this)).click();*/
				}
			});

            $('div.cell-edit').live('change', function() {
                if ($(this).hasClass('no-editor')) {
                    return;
                }
                $(this).parent('td').addClass('edit-loader');

                var record_id = $(this).parents('tr').attr('record');
                var field = $(this).attr('field');
                var value = $(this).text();

                Dajaxice.airplay.report_edit(Dajax.process, {'record_id': record_id, 'field': field, 'value': value});

                $(this).data('input', true);
            });

            $('select.conflict-box').each(function() {
                $(this).combobox();
            });

            $('td.conflict-field input').live('keydown', function(e, ui) {
                if (e.keyCode == 9) {
                    var options = {
                        record_id: $(this).parent('td').attr('record'),
                        field: $(this).parent('td').attr('field'),
                        value: $(this).val()
                    }
                    Dajaxice.airplay.report_conflict(Dajax.process, options);
                    var next = $('tr.conflict-record input').not($(this));
                    if (next) {
                        $(next[0]).focus()
                    }
                    return false;
                }
            });

            $('td.conflict-field input').live('keypress', function(e, ui) {
				if (e.keyCode == 13) {
					return false;
				}
            });

            $('td.conflict-field input').live('keyup', function(e, ui) {
                if (e.keyCode == 13) {
                    var options = {
                        record_id: $(this).parent('td').attr('record'),
                        field: $(this).parent('td').attr('field'),
                        value: $(this).val()
                    }
                    Dajaxice.airplay.report_conflict(Dajax.process, options);
                    var next = $('tr.conflict-record input').not($(this));
                    if (next) {
                        $(next[0]).focus()
                    }
                    return false;
                }
            });

            $('a.conflicts-resolve-button').live('click', function() {
                $('td.conflict-field input', $(this).parents('tr')).each(function() {
                    var options = {
                        record_id: $(this).parent('td').attr('record'),
                        field: $(this).parent('td').attr('field'),
                        value: $(this).val()
                    }
                    Dajaxice.airplay.report_conflict(Dajax.process, options);
                    var next = $('tr.conflict-record input').not($(this));
                    if (next) {
                        $(next[0]).focus()
                    }
                });
                return false;
            });

            $('a.publish-toggle-button').live('click', function() {
                var options = {
                    record_id: $(this).parents('tr').attr('record')
                }
                Dajaxice.airplay.record_toggle_publish(Dajax.process, options);
                return false;
            });

            $('button.next-conflict').live('click', function() {
                var next = $('tr.conflict-record input').not(':focus');
                if (next.length > 0) {
                    $(next[0]).focus()
                }
            });

            $('button.reports-conflicts-clear').live('click', function() {
                $(this).hide();
                $('#reports-conflicts-clear-form').show();
            });

            $('button.reports-conflicts-clear-submit').live('click', function() {
                if (confirm("Are you sure you want to resolve all conflicts automatically?")) {
                    var conflict_source = $('#conflicts-clear-source').val();
                    var options = {'report_id': $(this).attr('report'), 'source': conflict_source};
                    Dajaxice.airplay.report_conflicts_clear(Dajax.process, options);
                }
                return false;
            });
        },

        new_reports: function() {
			if ($("tr.report-line-new").length > 0) {
            	$('html, body').animate({scrollTop: $("tr.report-line-new").offset().top-100}, 1000);
			}
            $('tr.report-line-new').each(function() {
                $('td', this).css({'background-color': '#fef3bb'});
                $(this).effect("pulsate", { times:2 }, 1000);
            });
        },

        colors: {
            current: null,
            current_value: null,
            is_mousedown: null,
            mousedown_value: null,

            init: function() {
                $('a.reports-colors-button').live('click', function() {
                    millenia3.reports.colors.current = $(this).attr('name');
                    millenia3.reports.colors.current_value = $(this).attr('value');
                    $('a.reports-colors-button').removeClass('reports-colors-button-active');
                    $('a.reports-colors-button').addClass('reports-colors-button-inactive');
                    $(this).removeClass('reports-colors-button-inactive');
                    $(this).addClass('reports-colors-button-active');
                    return false;
                });

                $('tr.reports-colored td').each(function() {
                    if (!$(this).hasClass('no-color')) {
                        $(this).css('cursor', 'pointer');
                    }
                });

                $('tr.reports-colored td').live('mousedown', function() {
                    millenia3.reports.is_mousedown = true;
                    if ($(this).hasClass('no-color'))
                        return;
                    if (millenia3.reports.colors.current && $(this).attr('color') == null) {
                        millenia3.reports.colors.mousedown_value = millenia3.reports.colors.current_value;
                        $(this).css('background-color', '#'+millenia3.reports.colors.current_value);
                        $(this).attr('color', millenia3.reports.colors.current);
                        var options = {
                            'record_id': $(this).parent('tr').attr('record'),
                            'field': $(this).attr('name'),
                            'color': millenia3.reports.colors.current_value
                        };
                        Dajaxice.airplay.color(Dajax.process, options);
                    } else {
                        millenia3.reports.colors.mousedown_value = null;
                        $(this).css('background-color', '#ffffff');
                        $(this).attr('color', null);

                        var options = {
                            'record_id': $(this).parent('tr').attr('record'),
                            'field': $(this).attr('name')
                        };
                        Dajaxice.airplay.color(Dajax.process, options);
                    }
                    return false;
                }).live('mouseover', function() {
                    if (millenia3.reports.is_mousedown) {
                        if ($(this).hasClass('no-color'))
                            return;
                        if (millenia3.reports.colors.mousedown_value) {
                            $(this).css('background-color', '#'+millenia3.reports.colors.mousedown_value);
                            $(this).attr('color', millenia3.reports.colors.current);
                            var options = {
                                'record_id': $(this).parent('tr').attr('record'),
                                'field': $(this).attr('name'),
                                'color': millenia3.reports.colors.mousedown_value
                            };
                            Dajaxice.airplay.color(Dajax.process, options);
                        } else {
                            $(this).css('background-color', '#ffffff');
                            $(this).attr('color', null);

                            var options = {
                                'record_id': $(this).parent('tr').attr('record'),
                                'field': $(this).attr('name')
                            };
                            Dajaxice.airplay.color(Dajax.process, options);
                        }
                    }
                }).live('mouseup', function() {
                    millenia3.reports.is_mousedown = false;
                });

                $('button.reports-colors-clear').live('click', function() {
                    if (confirm("Are you sure you want to remove all colors and notes?")) {
                        Dajaxice.airplay.color_clear(Dajax.process, {'report_id': $(this).attr('report')});
                        $('tr.reports-colored td').each(function() {
                            $(this).css('background-color', '#ffffff');
                        });
                        $('tr.reports-colored td.record-notes').each(function() {
                            $(this).html('');
                        });
                    }
                    return false;
                });
            }
        }
    }
}


$(function() {
    Dajaxice.setup({'default_exception_callback': function(){ return; }});
    millenia3.show_messages();
	millenia3.forms.init();
    millenia3.select_all_none();
    millenia3.tablesorter();
    millenia3.tooltips();
    millenia3.long_requests();
    millenia3.confirm_action();
    millenia3.redirect_buttons();
    millenia3.colorbox();
    millenia3.reports.init();
})

