﻿
//--------------------script for handling request parameters from query string-------------------------------
window.location.querystring = (function() {

    // by Chris O'Brien, prettycode.org

    var collection = {};

    // Gets the query string, starts with '?'

    var querystring = window.location.search;

    // Empty if no query string

    if (!querystring) {
        return { toString: function() { return ""; } };
    }

    // Decode query string and remove '?'

    querystring = decodeURI(querystring.substring(1));

    // Load the key/values of the return collection

    var pairs = querystring.split("&");

    for (var i = 0; i < pairs.length; i++) {

        // Empty pair (e.g. ?key=val&&key2=val2)

        if (!pairs[i]) {
            continue;
        }

        // Don't use split("=") in case value has "=" in it

        var seperatorPosition = pairs[i].indexOf("=");

        if (seperatorPosition == -1) {
            collection[pairs[i]] = "";
        }
        else {
            collection[pairs[i].substring(0, seperatorPosition)]
                = pairs[i].substr(seperatorPosition + 1);
        }
    }

    // toString() returns the key/value pairs concatenated

    collection.toString = function() {
        return "?" + querystring;
    };

    return collection;
})();
//-------------------------------------------------------------------------------------------------------

var timer; //Variable for storing data of setTimeout() function. If registration was successful panel with thankyou massage will be displayed for 5 seconds
//If user exit message panel with close button, setTimeout() need to be canceled with clearTimeout() function.





//----------------------------changing panels for registration, loging and error messages-------------------------
var showLogBlock = false;
var showRegBlock = false;
var showSuccBlock = false;

$(document).ready(function() {
    $(".top-err-msg").hide();
    if (!showRegBlock) { $('.top-reg').hide(); }
    if (!showLogBlock) { $('.top-login').hide(); }

    $('.ico-reg').click(function() {
        $('div[id$=summLoging]').hide();
        $('div[id$=summRegistration]').hide();
        //  $('div[id=top-addon]').css('display','block');
        if ($('.top-err-msg').is(':visible')) {
            $('.top-err-msg').slideToggle("slow", function() { $('.top-reg').slideToggle("slow", function() { $('input[id$=regUsername]').focus(); }) });
            $('.top-reg input, span.logMsg, span.regMsg, .top-login input').val('').html('');
        }
        else if ($('.top-login').is(':visible')) {
            $('.top-login').slideToggle("slow", function() { $('.top-reg').slideToggle("slow", function() { $('input[id$=regUsername]').focus(); }) });
            $('.top-reg input, span.logMsg, span.regMsg, .top-login input').val('').html('');
        }
        else {
            $('.top-reg').slideToggle("slow", function() { $('input[id$=regUsername]').focus(); });
            $('.top-reg input, span.logMsg, span.regMsg, .top-login input').val('').html('');
        }
        return false;
    });
    $('input.top-reg-button').click(function() {
        $('span.regMsg').val('').html('');
        $('div[id$=summRegistration').show();
    });
});

$(document).ready(function() {
    if (!showLogBlock) {
        $('.top-login').hide();
    }
    else {
        $('.top-login').css('display', 'block');
    }

    $('.ico-login').click(function() {
        $('div[id$=summLoging]').hide();
        $('div[id$=summRegistration]').hide();
        if ($('.top-err-msg').is(':visible')) {
            $('.top-err-msg').slideToggle("slow", function() { $('.top-login').slideToggle("slow", function() { $('input[id$=logUsername]').focus(); }) });
            $('.top-reg input, span.logMsg, span.regMsg, .top-login input').val('').html('');
        }
        else if ($('.top-reg').is(':visible')) {
            $('.top-reg').slideToggle("slow", function() { $('.top-login').slideToggle("slow", function() { $('input[id$=logUsername]').focus(); }) });
            $('.top-reg input, span.logMsg, span.regMsg, .top-login input').val('').html('');
        }
        else {
            $('.top-login').slideToggle("slow", function() { $('input[id$=logUsername]').focus(); });
            $('.top-reg input, span.logMsg, span.regMsg, .top-login input').val('').html('');
        }

        return false;
    });
    $('input.top-login-button').click(function() {
        $('span.logMsg').val('').html('');
        $('div[id$=summLoging]').show();
    });
});

$(document).ready(function() {
    if (window.location.querystring["login"] == "true") {
        $('.top-login').show();
    }

    $('a.top-close').click(function() {
        $(this).parent().slideToggle("slow");
        if ($(this).parent().attr('class') == "top-msg" || $(this).parent().attr('class') == "top-err-msg") {
            clearTimeout(timer);
        }
    });
});

$(document).ready(function() {
    if (isAuthenticated == "True") {
        $('.register-login').attr('style', 'display:none');
    }
});

//--------------on submit button cliclk check if page is valid. if isn't then show tor-err-msg panel with appropriate message--	
function delayedCheckForErrors() {
    setTimeout("delayedShowErrorsIfAny()", 100);
}
function delayedShowErrorsIfAny() {
    if (Page_IsValid != null && Page_IsValid == false) {
        $('label[id$=msgTitle]').html($('input[id$=errMsgTitle]').val());
        var topAddon = false;
        $('#top-addon > div').each(function() {
            if ($(this).is(':visible')) { topAddon = true; $(this).slideToggle("slow", function() { $('.top-err-msg').slideToggle("slow"); }); }
        });
        if (!topAddon && !($('.top-err-msg').is(':visible'))) {
            $('.top-err-msg').slideToggle("slow");
        }
    }
    else if (Page_IsValid != null && Page_IsValid == true) {
        if ($('input[id$=confirm]').val() == "True") {
            $('label[id$=msgTitle]').html($('input[id$=confirmationMsgTitle]').val());
            var topAddon = false;
            $('#top-addon > div').each(function() {
                if ($(this).is(':visible')) { topAddon = true; $(this).slideToggle("slow", function() { $('.top-err-msg').slideToggle("slow"); }); }
            });
            if (!topAddon && !($('.top-err-msg').is(':visible'))) {

                $('.top-err-msg').slideToggle("slow");
            }
        }
    }
}
//--------------------------------end script-----------------------------------------------------------------------------------

/************************************************************************************************************************************/
/************************************************************************************************************************************/
/************************************************************************************************************************************/



// cufon (all pages)
Cufon.replace('.section-title strong, .section-title.kateg, .feat-title, .more, .from-blog-title, h6, #my-suggestion label, #suggestion-preview label, .top-addon-title', { hover: true });


// search.html
$(document).ready(function() {
    $('div.najg-sidebar ul.sect-slide li').click(function() {
        $('div.najg-sidebar ul.sect-slide li').removeClass('ss-curr');
        $(this).addClass('ss-curr');
        return false;
    });
    $('div.najtisi-sidebar ul.sect-slide li').click(function() {
        $('div.najtisi-sidebar ul.sect-slide li').removeClass('ss-curr');
        $(this).addClass('ss-curr');
        return false;
    });
});


//--------------------------suggestionInputNew.html	-----------------------------------

function isValidYoutubeLink(youtubeLink) {
    var pattern = new RegExp(/http\:\/\/www\.youtube\.com\/watch\?v=([a-z0-9\-]+)\&?/i);
    return pattern.test(youtubeLink);
}

function bindChangePictureUpload() {
    var filePicID = $(this).attr('id');
    $("div.add-media.picture div#media-" + filePicID + " span").html($(this).val());
}
$(document).ready(function() {
    // Adding fields for images
    $('div.add-media.picture div.media-plus a').click(function() {
        var newDate = new Date;
        var id = newDate.getTime();
        $('<div><div class="media-input" id="media-file-pic-' + id + '"><span></span><input type="file" class="file-pic" id="file-pic-' + id + '" name="file-pic-' + id + '" /></div></div>').appendTo('div.add-media.picture div.media-fields');
        $('input:file.file-pic').change(bindChangePictureUpload);
        return false;
    });

    $('input:file.file-pic').change(bindChangePictureUpload);

    // Adding fields for videos (ekstern)
    $('div.add-media.video div.media-extern div.media-plus a').click(function() {
        var count = $('div.media-extern > div').length;
        $('<div><div class="media-input"><input type="text" class="file-ext" id="media_ext_' + (count + 1) + '"/></div></div>').appendTo('div.add-media.video div.media-extern');
        return false;
    });
    // Get all new external URL
    $(".ms-pregled").click(function(event) {
        var links = '';
        var valid = true;
        $('input.file-ext').each(function() {
            if ($(this).val() != "") {
                valid &= isValidYoutubeLink($(this).val());
            }
        });
        if (valid) {
            $('span[id$=labErrMsg]').html('');
            $('input.file-ext').each(function() {
                links += $(this).attr('value') + ',';
            });
            $('input[id$=externPlus]').val(links.substring(0, links.length - 1));
        }
        else {
            agent = jQuery.browser;
            if (agent.msie) {
                event.cancelBubble = true;
            }
            else {
                event.stopPropagation();
            }
            showHeaderMessage(errTitle, errYouTubeText);
            return false;
        }
    });
});

$(document).ready(function() {
    // textarea focus
    $('#my-suggestion #tekst-prew .ms-desc textarea').focus(function() {
        $('#my-suggestion #tekst-prew .ms-desc').addClass('ms-desc-focus');
    });
    $('#my-suggestion #tekst-prew .ms-desc textarea').blur(function() {
        $('#my-suggestion #tekst-prew .ms-desc').removeClass('ms-desc-focus');
    });
    // extern video focus
    $('#my-suggestion .add-media .media-extern .media-input input').focus(function() {
        $(this).parent().addClass('media-input-focus');
    });
    $('#my-suggestion .add-media .media-extern .media-input input').blur(function() {
        $(this).parent().removeClass('media-input-focus');
    });
});


//-----------------------------------------------------------------------------------
/*
// thecomplicated.html
$(document).ready(function () {
$('div.najg ul.sect-slide li').click(function () {
$('div.najg ul.sect-slide li').removeClass('ss-curr');
$(this).addClass('ss-curr');
return false;
});
});
$(document).ready(function () {
$('div.najtisi ul.sect-slide li').click(function () {
$('div.najtisi ul.sect-slide li').removeClass('ss-curr');
$(this).addClass('ss-curr');
return false;
});
});
$(document).ready(function () {
$('div.komment ul.sect-slide li').click(function () {
$('div.komment ul.sect-slide li').removeClass('ss-curr');
$(this).addClass('ss-curr');
return false;
});
});

*/
// admin.html
$(document).ready(function() {
    $('#changePassLnk').live('click', function() {
        $('#passwordChange').show();
    });
});

$(document).ready(function() {
    $('#changeEmailLnk').live('click', function() {
        $('#emailChange').show();
    });
});

$(function() {
    $('div.radio-wrap label').live('click', function() {
        $('div.radio-wrap label').removeClass('checked');
        $(this).addClass('checked');
        $('div.radio-wrap input:radio').attr('checked', false);
        $('div.radio-wrap input:radio[id$=_' + $(this).attr('for') + ']').attr('checked', true);
    });
});

function initGenderRadioButtons() {
    if ($("input[@name='pol']:checked").val() == 'man') {
        $('div.radio-wrap label.man').addClass('checked');
    }
    else if ($("input[@name='pol']:checked").val() == 'woman') {
        $('div.radio-wrap label.woman').addClass('checked');

    }
};
$(function() {
    $('a.changeProfileImg').live('click', function() {
        $('div.userImgLnk').addClass('profil-hide');
        $('div.upload-img').removeClass('profil-hide');
        return false;
    });
});
// file input mask

$(document).ready(function() {
    $('input:file').change(function() {
        $("div#media-file-pic-1 em").html($(this).val());
    });

    $('input[id$=DeleteImg]').live('click', function() {
        if (!confirm('Da li ste sigurni da želite da obrišete sliku?!')) {
            return false;
        } else {
            return true;
        }
    });
});

//checkboxes on profile page

$(function() {
    $('div.checkbox-wrap label').live('click', function() {

        if ($(this).hasClass('checked')) {
            $('div.checkbox-wrap label').removeClass('checked');
            $('div.checkbox-wrap input:checkbox[id$=' + $(this).attr('for') + ']').attr('checked', false);
        }
        else {
            $(this).addClass('checked');
            $('div.checkbox-wrap input:checkbox[id$=' + $(this).attr('for') + ']').attr('checked', true);
        }

    });
});

//// page.html   -  This is not needed
//	$(document).ready(function () {
//		$('ul.pi-glasaj li').click(function () {
//			$('ul.pi-glasaj li').removeClass('pi-slider');
//			$(this).addClass('pi-slider');
//			return false;
//		});
//	});


function showChangePasswordError(messageTitle, messageBody) {
    $('a#changePassLnk').click();

    showHeaderMessage(messageTitle, messageBody);
}

function showHeaderMessage(messageTitle, messageBody) {
    $('label[id$=msgTitle]').html(messageTitle);
    $('span[id$=labErrMsg]').html(messageBody);

    scrollToTop();

    if (!($('.top-err-msg').is(':visible'))) {
        $('.top-err-msg').slideToggle("slow")
    };
}

function submitSucces(txt) {
    $('label[id$=msgTitle]').html($('input[id$=confirmationMsgTitle]').val());
    if ($('span[id$=labErrMsg]').length == 0) {
        $('.top-err-msg .msg-col label').html(txt);
    } else {
        $('span[id$=labErrMsg]').html(txt);
    }

    var slideToggleTimeoutMilliseconds = 3000;
    if (!($('.top-err-msg').is(':visible'))) {
        $('.top-err-msg').slideToggle('slow');
        timer = setTimeout("$('.top-err-msg').slideToggle('slow')", slideToggleTimeoutMilliseconds);
    }
    else {
        timer = setTimeout("$('.top-err-msg').slideToggle('slow')", slideToggleTimeoutMilliseconds);
    }
};

//suggestion-item

$(document).ready(function() {
    if ($('#dialog').length > 0) {
        $('#dialog').jqm();
        $('#dialog').jqmAddTrigger('.add-comment');
        $('#dialog .jqmclose').bind('click', function() {
            $('span[id$=lblerrormessage]').css('display', 'none');
            $('input:text, textarea, input:submit', '#dialog').attr('style', '').val('');
        });
    }

    if ($('#changeDialog').length > 0) {
        $('#changeDialog').jqm();
        $('#changeDialog').jqmAddTrigger('.lnkChangeSuggestion');
        $('#changeDialog .jqmclose').bind('click', function() {
            $('input:textarea, input:submit', '#changeDialog').attr('style', '').val('');
        });
    }
});

$(document).ready(function() {
    $('.video:odd').addClass('alt');
    $('.ocupation').each(function() {
        if ($(this).text() === ' - ') {
            $(this).attr('style', 'display:none');
        }
    });


    $('textarea[id$=suggestionText]').keyup(function() {
        var maxLength = 300;
        var charLength = $(this).val().length;

        $('span[id$=NoOfCharsLeft]').text(maxLength - charLength);

        // auto truncate the text to maxLength's setting
        if (charLength > maxLength) {
            this.value = this.value.substring(0, maxLength);
            $('span[id$=NoOfCharsLeft]').text(0);
            return false;
        }
    }); //keyup

});

//---------------- focus for IE7
$(document).ready(function() {
    //main-search
    $('#navbar #search input').focus(function() { $(this).css('background-position', '0 -27px'); });
    $('#navbar #search input').blur(function() { $(this).css('background-position', '0 0'); });
    //suggestion title
    $('#my-suggestion #title-prew .sb-input').focus(function() { $(this).css('background-position', '0 -49px'); });
    $('#my-suggestion #title-prew .sb-input').blur(function() { $(this).css('background-position', '0 0'); });
    //news - subscribe
    $('#my-suggestion .article-info input').focus(function() { $(this).css('background-position', '0 -27px'); });
    $('#my-suggestion .article-info input').blur(function() { $(this).css('background-position', '0 0'); });
    //enter name - suggestion page
    $('#my-suggestion .add-name input').focus(function() { $(this).css('background-position', '0 -49px'); });
    $('#my-suggestion .add-name input').blur(function() { $(this).css('background-position', '0 0'); });
    //input fields - contact page
    $('.contact-data input').focus(function() { $(this).css('background-position', '0 -27px'); });
    $('.contact-data input').blur(function() { $(this).css('background-position', '0 0'); });
    //text-area - contact page
    $('.contact-data textarea').focus(function() { $(this).css('background-position', '0 -110px'); });
    $('.contact-data textarea').blur(function() { $(this).css('background-position', '0 0'); });
    //buka textarea - homepage
    $('#post-msg textarea').focus(function() { $(this).css('background-position', '0 -120px'); });
    $('#post-msg textarea').blur(function() { $(this).css('background-position', '0 0'); });
    //search all suggestions
    $('#search-bar .sb-input').focus(function() { $(this).css('background-position', '0 -49px'); });
    $('#search-bar .sb-input').blur(function() { $(this).css('background-position', '0 0'); });
    //top registration and login
    $('.tl-col input[type="password"]').focus(function() { $(this).css('background-position', '0 -27px'); });
    $('.tl-col input[type="password"]').blur(function() { $(this).css('background-position', '0 0'); });
    //top registration and login
    $('.tl-col input[type="text"]').focus(function() { $(this).css('background-position', '0 -27px'); });
    $('.tl-col input[type="text"]').blur(function() { $(this).css('background-position', '0 0'); });
    //suggestion change textarea
    $('.change-data textarea').focus(function() { $(this).css('background-position', '0 -128px'); });
    $('.change-data textarea').blur(function() { $(this).css('background-position', '0 0'); });
    //suggestion change textarea
    $('.profil-data input[type=text] ').focus(function() { $(this).css('background-position', '0 -27px'); });
    $('.profil-data input[type=text] ').blur(function() { $(this).css('background-position', '0 0'); });

});
//------------------ navigation

//$(document).ready(function () {
//	$('#navbar a').click(function () {
//		$('#navbar a').each(function()
//		    {
//		         $(this).removeClass('current');
//		});			
//		    $(this).addClass('current');
//	});
//});

//-------------------- script for go to top page on footer

$(document).ready(function() {
    $('#footer .go-top-page').click(function() {
        scrollToTop();
        return false;
    });
});

function scrollToTop() {
    $('html, body').animate({ scrollTop: '0px' }, 300);
}
//-------------image resize

$(document).ready(function() {

    if ($('.tooltip-close-big textarea').is(':visible')) {
        var parentPosition = $('.tooltip-close-big textarea').offset();
        var topPosition = parentPosition.top + 50;
        var leftPosition = 100;
        $('.tooltip-wrap').css('top', topPosition + 'px').css('left', leftPosition + 'px');
    }


    $('.jcarousel-skin-tango .jcarousel-container img').each(function() {
        $(this).removeAttr('height', '');
        $(this).removeAttr('width', '');
        var maxWidth = 108; // Max width for the image
        var maxHeight = 108;    // Max height for the image
        var ratio = 0;  // Used for aspect ratio
        var width = $(this).width();    // Current image width
        var height = $(this).height();  // Current image height
        $(this).css('position', 'relative');

        // Check if we were able to read width and height; if not then use default values
        if (width == 0 || height == 0 || width == undefined || height == undefined) {
            $(this).css("width", maxWidth); // Set max width
            $(this).css("height", maxHeight);  // Set max height
        }

        // Check if the current width is larger than the max
        if (width > maxWidth) {
            ratio = maxWidth / width;   // get ratio for scaling image
            $(this).css("width", maxWidth); // Set new width
            $(this).css("height", height * ratio);  // Scale height based on ratio
            height = height * ratio;    // Reset height to match scaled image
            width = width * ratio;    // Reset width to match scaled image
            var left = (maxWidth - width) / 2;
            var top = (maxHeight - height) / 2;
            $(this).css('left', left + 'px');
            if (top > 0)
                $(this).css('top', top + 'px');
        }

        // Check if current height is larger than max
        if (height > maxHeight) {
            ratio = maxHeight / height; // get ratio for scaling image
            $(this).css("height", maxHeight);   // Set new height
            $(this).css("width", width * ratio);    // Scale width based on ratio
            width = width * ratio;    // Reset width to match scaled image
            var left = (maxWidth - width) / 2;
            var top = (maxHeight - height) / 2;
            $(this).css('left', left + 'px');
            if (top > 0)
                $(this).css('top', top + 'px');
        }
    });
});


