$(function() {
	jQuery.fn.outerHTML = function(s) {
		return (s)
		? this.before(s).remove()
		: jQuery("<p>").append(this.eq(0).clone()).html();
	}
});

/* function for generating not allowed document view */

function mark_not_allowed() {
    var $requestCall;
    $('.document-not-allowed * a.open-link').attr('href', 'javascript:void(0)')
    $('.document-not-allowed').live('click', function (e) {
        document_id = $(this).attr('rel')
        
        if ($requestCall != undefined)
            $requestCall.dialog('close')
            
        $requestCall = $(
        '<div>If you want to know more about getting access to this document call us 38 10 67 00 or '+
        'fill your number below and we\'ll call you.'+
        '<form action="/document/request_rights/" method="POST">'+
            '<input name="phone" type="text" /><input type="hidden" name="document" value="'+document_id+'"><input type="submit" value="Request call">'+
        '</form>'+
        '</div>')
		.dialog({
			title: 'No rights'
		})
        
        $requestCall.children('form').ajaxForm(function() { 
                alert("You will be contacted by our representative ASAP."); 
                $requestCall.dialog('close');
            });

        $requestCall.dialog('open');
                
        return false;
    });
}
// Cycle document colors the correct way ;)
function cycle_colors() {
    $('th.date_def').each(function(i, e) { 
        trs = $(e).parent('tr').nextUntil('tr:has(th)'); 
        $(trs).filter(':odd').addClass('dark') 
    });
}

function click_on_document_cells() {
    // complex document
    complex = $('div.complex-document-display.index table:eq(1) tbody tr').filter(':not(.document-not-allowed)').children(':not(td:nth-child(2), td:nth-child(4), th)');
    complex2 = $('div.complex-document-display.not-index table:eq(1) tbody tr').filter(':not(.document-not-allowed)').children(':not(td:nth-child(2), td:nth-child(8))');
    
    simple = $('div.simple-document-display.index table:eq(1) tbody tr').filter(':not(.document-not-allowed)').children(':not(td:nth-child(3), th)');
    simple2 = $('div.simple-document-display.not-index table:eq(1) tbody tr').filter(':not(.document-not-allowed)').children(':not(td:nth-child(3), td:nth-child(6))');
    
    fundamental = $('div.fundamental-document-display table:eq(1) tbody tr').filter(':not(.document-not-allowed)').children(':nth-child(3)');
    
    make_clickable(complex);
    make_clickable(complex2);
    make_clickable(simple);
    make_clickable(simple2);
    make_clickable(fundamental);
    
}

function make_clickable(objects) {
    objects.css('cursor', 'pointer');
    objects.click(function () {
        url = $(this).parent().find('* a.open-link').attr('href');
        if (url.length) {
            var newWindow = window.open(url, '_blank');
            newWindow.focus();
            $(this).parent().addClass('document-viewed');
        }
        
        return false;
    })
}



$(document).ready(function () {
    mark_not_allowed();
    cycle_colors();
    click_on_document_cells();
});

function ajax_page_loader(url, block, link) {
    $($(block).find('table')[1]).parent().html('Loading...');
    href = $(link).attr('href').split('?');
    url = url + '?' + href[1];
    $.get(url, function (ret) {
        $(block).html(ret);
        cycle_colors();
        click_on_document_cells();
        mark_not_allowed();
    });
}

$('.simple-document-display div.pagination a.button_paging').live('click', function() {
    ajax_page_loader('/document/simple/ajax', $(this).parents('.simple-document-display')[0], this);
    return false;
});
$('.complex-document-display div.pagination a.button_paging').live('click', function() {
    ajax_page_loader('/document/complex/ajax', $(this).parents('.complex-document-display')[0], this);
    return false;
});


function changeTab(link, index, company){
    tabs = $(link).parents('.tabs_1')
    tabs.children('li').each(function(i, e){
        $(e).removeClass('selected')
    })
    tabs.children('li:eq(' + index + ')').addClass('selected');
    
    $(link).parents('.company-contents').children('.tab_contents').hide();
    $(link).parents('.company-contents').children('.tab' + index).show();
    
    var content = $(link).parents('.company-contents').children('.tab' + index);
    
    if (index == 1 || index == 2) {
        content.html("Loading...");
        $.get('tab', {
            'index': index,
            'company': company
        }, function(r){
            content.html(r);
            click_on_document_cells();
            mark_not_allowed();
        });
    }
}
