//jQuery Plugin: Drop Shadow Text
// call like this: $(element).textDropShadow();
(function($) {
 $.fn.textDropShadow = function(){
	 $(this).html('<span class="jq-shadow">'+$(this).html()+'</span><span>'+$(this).html()+'</span>');
	 return $(this);
 }
 
 
 $.fn.divScroller = function(options) {
    var defaults = {
      upElementId : 'up',
      dnElementId : 'dn',
      step : 50,
      x : 0
    }
    var opts = $.extend(defaults, options);
    var element = this;
    
    $(opts.upElementId).click(function(){
      if (opts.x>0){
        opts.x = opts.x - opts.step;
        $(element).css("top", "-" + opts.x + "px");
      }
    });
    
    $(opts.dnElementId).click(function(){
      opts.x = opts.x + opts.step;
      $(element).css("top", "-" + opts.x + "px");
    });
    
    return $(this);
 }
 
})(jQuery);


jQuery(document).ready(function($) {
    //forum 
    initForumFunctions();

    $("#submenu #nav A").each(function() { $(this).textDropShadow() });

    $("#mi88").click(function() {
        openKontakti();
        return false;
    });
    $("#mi146").click(function() {
        openMelodije();
        return false;
    });
    $("#mi103").click(function() {
        openPipsOnWeb();
        return false;
    });
    $("#diskografija-list .inner").divScroller({
        upElementId: "#diskografija-list-up",
        dnElementId: "#diskografija-list-dn"
    });


    var $wh = $(window).height();
    if ($wh > 670) {
        $(".home #center").css("height", "665px").css("margin-top", ($wh - 665) / 2);
        $(".home").css("background-position", "50% " + ($wh - 665) / 2 + "px");
    }


    $(window).resize(function() {
        var $wh = $(this).height();

        if ($wh > 670) {
            $(".home #center").css("margin-top", ($(this).height() - 665) / 2);
            $(".home").css("background-position", "50% " + ($(this).height() - 665) / 2 + "px");
        }
    });
});

function openKontakti() {
	openCenteredPopup("/hr/band/kontakti/", "Kontakti", "346", "560", "toolbar=no, resizable=no, scrollbars=no, status=yes"); 
}

function openMelodije() {
	openCenteredPopup("/hr/biznis/melodije-linkovi/", "Melodije", "346", "560", "toolbar=no, resizable=no, scrollbars=no, status=yes"); 
}

function openPipsOnWeb() {
	openCenteredPopup("/hr/band/pips-on-web/", "PipsOnWeb", "462", "480", "toolbar=no, resizable=no, scrollbars=no, status=yes"); 
}

 function openCenteredPopup(mypage,myname,w,h,features) {
    if (features=='default')
      features = 'resizable=yes,scrollbars=yes';
    var winl = (screen.width-w)/2;
    var wint = (screen.height-h)/2;
    if (winl < 0) winl = 0;
    if (wint < 0) wint = 0;
    var settings = 'height=' + h + ',';
    settings += 'width=' + w + ',';
    settings += 'top=' + wint + ',';
    settings += 'left=' + winl + ',';
    settings += features;
    
    try {
	this.win = window.open(mypage,myname,settings);
    	this.win.window.focus();
    } catch(err) {
    	return false;
    }
 }
  
  function initForumFunctions() {
    $(".profil-autora").click(function() { openCenteredPopup($(this).attr("href"), "profilAutora", 280, 208, "resizable=no,scrollbars=no"); return false; });
}


pcvc = function() {

      this.getTextSelection = function() {
      var textarea = $('#message-text').get(0);
        if (window.getSelection) {
            var len = textarea.value.length;
            var start = textarea.selectionStart;
            var end = textarea.selectionEnd;
            var sel = textarea.value.substring(start, end);
            var result = { "textarea": textarea, "start": start, "end": end, "len": len, "sel": sel };
            return result;
        }
        else if (document.selection) {
            /* Damn you IE, why must you torture me so? */
            var range = document.selection.createRange();
            var stored_range = range.duplicate();
            stored_range.moveToElementText(textarea);
            stored_range.setEndPoint('EndToEnd', range);
            textarea.selectionStart = stored_range.text.length - range.text.length;
            textarea.selectionEnd = textarea.selectionStart + range.text.length;
            var result = { "textarea": textarea, "start": textarea.selectionStart, "end": textarea.selectionEnd, "len": textarea.value.length, "sel": range.text };
            return result;
        }
    }

    this.replaceText = function(text) {
        var inputID = '#message-text';
        var replace = $(inputID).attr('value').substring(0, text.start) + text.replace + text.textarea.value.substring(text.end, text.len);
        $(inputID).attr('value', replace).focus();
    }

    this.simpleReplace = function(wrapper) {
        var text = this.getTextSelection();
        text.replace = '[' + wrapper + ']' + text.sel + '[/' + wrapper + ']';
        this.replaceText(text);
    }

    this.bold = function() {
        this.simpleReplace('b');
    }

    this.italic = function() {
        this.simpleReplace('i');
    }

    this.underline = function() {
        this.simpleReplace('u');
    }

    this.link = function() {
        this.simpleReplace('url');
    }
}

var PCVC = new pcvc();

