$(document).ready(function() {

    new CommentResponse();
    // gallery init
	colorboxOptions = {
		current: 	"zdjęcie {current} z {total}",
		previous:	"previous",
		next:		"nastepne",
		close:		"Zamknij",
		transition:	"elastic"
	}
	galleryCounter = 1;
	$(".class-gallery").each(function() {
		$(this).find('.content-view-children a').attr('rel', 'gallery'+galleryCounter);
		$("a[rel='gallery"+galleryCounter+"']").colorbox(colorboxOptions);

		galleryCounter++;
	})

    // Colorbox - lightbox
	$(".colorboxShow").find('a').attr('rel', 'colorbox');
	$("a[rel='colorbox']").colorbox(colorboxOptions);
	$("a[class='embed_image_link']").colorbox({transition:"elastic"});	

    // RDS refresh
    rdsRefresh();
	
	// text resizer init
	var textSizeCookie = readCookie("textSizeCookie");
	var textSize = textSizeCookie ? textSizeCookie : "normal";
	changeTextSize(textSize);
	$('li.'+textSize).addClass('selected');
	$('.size-controll li').click(function() {
		$(this).siblings().removeClass('selected');
		changeTextSize($(this).attr('class'));
		$(this).addClass('selected');
	});

})

// RDS init
function rdsRefresh() {
    $.ajax({
        type: "GET",
        timeout: 2000,
        url: '/externals/rds_cache',
        success: function(data) {
            $('#rds').html(data);
            data=null;
        }
    });
	setTimeout('rdsRefresh();', 30*1000);
}



// Response to comment
function CommentResponse() {
    var self = this;

    self.commentsDivId          = 'comments';
    self.basicCommentFormId     = 'commentForm';
    self.subCommentFormClass    = 'subCommentForm';

    /**
     * Initialize object
     */
    self.construct = function() {
        self.findItems();
    };

    /**
     * Find comment response buttons
     */
    self.findItems = function() {
        $('#' + self.commentsDivId).find("input[class*='new_subcomment']").click( function() {
            self.showResponseForm(this);
            return false;
        })
    }

    self.showResponseForm = function(inputButton) {
        parentElement = $(inputButton).parent();
        currentDisplayState = parentElement.next().css('display');
        currentNodeId = $(inputButton).siblings("[name='NodeID']").val();

        $('.' + self.subCommentFormClass).hide();

        if(parentElement.next().hasClass('subCommentForm')) {
           if(currentDisplayState == 'none') {
                parentElement.next().slideDown("slow");
           } else {
               parentElement.next().show();
           }

        } else {
            self.createCommentForm(parentElement, currentNodeId);
        }

    }

    self.createCommentForm = function(parent, nodeId) {
        newForm = $("#" + self.basicCommentFormId).clone();
        newForm.find("input[name='NodeID']").val(nodeId);
        newForm.addClass(self.subCommentFormClass);
        newForm.css('display', 'none');
        newForm.insertAfter(parent).removeAttr('id');
		newForm.slideDown('slow');
    }

    self.construct();
}

// Text resizer
//config
var textSizeConfig = {small: 12, normal: 14, big: 16}
// function
function changeTextSize(textSize){
	if(textSizeConfig[textSize] > 0) {
		createCookie("textSizeCookie", textSize, 365);
		$('div.attribute-long p').css('font-size', textSizeConfig[textSize] + 'px');
		$('div.attribute-body p').css('font-size', textSizeConfig[textSize] + 'px');
	}
}

function openPopup(url) {
	playerWindow = window.open(url, "popup_id", "scrollbars=0,resizable,width=438,height=670"); 
	playerWindow.focus();
    return false;
}
