﻿
String.format = function( text ) {
    
    //check if there are two arguments in the arguments list
    if ( arguments.length <= 1 ) {
        //if there are not 2 or more arguments there’s nothing to replace
        //just return the original text
        return text;
    }

    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for( var token = 0; token <= tokenCount; token++ ) {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace( new RegExp( "\\{" + token + "\\}", "gi" ), arguments[ token + 1 ]);
    }

    return text;
};

var persianNumbers = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
function convertToPersianNumbers(str) {	            
	if (persianNumbers) {
		for (var i = 0; i < persianNumbers.length; i++) {
			str = str.replace(new RegExp(i, 'g'), persianNumbers[i]);
		}
	}
	return str;
}

Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

if (typeof String.prototype.startsWith != 'function') {
    String.prototype.startsWith = function (str) {
        return this.indexOf(str) == 0;
    };
}

$(function () {

    // forces colorbox to stay in the middle when scrolling
    $(document).bind('cbox_open', function () {
        //$('html').css({ 'overflow': 'hidden' });
        $(window).bind('scroll', function () {
            var cb = $('#colorbox');
            var top = 0;
            var cbHeight = cb.outerHeight();
            var winHeight = $(this).height();
            if (winHeight > cbHeight)
                top = (winHeight - cbHeight) / 2 + $(this).scrollTop();

            //cb.css('top', top + "px");
            cb.animate({
                top: top
            }, 40);
        });

    }).bind('cbox_closed', function () {
        //$('html').css({ 'overflow': 'auto' });
        $(window).unbind('scroll');
    });

    $(".contact-button").colorbox({
        initialWidth: 400,
        initialHeight: 300,
        width: 620,
        height: 590,
        iframe: true
    });

    $("a.badge").colorbox({
        initialWidth: 400,
        initialHeight: 300,
        width: 628,
        height: 500,
        iframe: true
    });

    $("a.stat-button").colorbox({
        initialWidth: 400,
        initialHeight: 300,
        width: 850,
        height: 600,
        iframe: true
    });

    /*
    *
    * Switch Box with multiple tabs and content scrolling
    *
    */
    $('.switch-box ul.tabs li').hover(function () {
        $('.switch-box ul.tabs li').removeClass('active').eq($(this).index()).addClass('active');
        $('.switch-box .content').removeClass('content-active').eq($(this).index()).addClass('content-active');
    });

    var switchBoxScroll = function () {
        var elems = $('.switch-box .content-active ul');
        var active = elems.filter('.active');
        var newIndex = elems.index(active) + 1;
        newIndex = (newIndex == elems.length) ? 0 : newIndex;
        active.removeClass('active');
        elems.eq(newIndex).addClass('active');
    };

    var switchBoxScrollTimer = null;
    var startSwitchBoxScroll = function () {
        switchBoxScrollTimer = setInterval(function () {
            switchBoxScroll();
        }, 5000);
    };

    $('.switch-box .content').hover(function () {
        if (switchBoxScrollTimer != null)
            clearInterval(switchBoxScrollTimer);
    }, function () {
        startSwitchBoxScroll();
    });

    if ($('.switch-box').length > 0)
        startSwitchBoxScroll();

    /*
    *
    * Intro Banner at home page
    *
    */
    var switchBannerTab = function (index) {
        var panes = $('.banner li');
        var tabs = $('.banner div');
        var activePane = panes.filter('.active');
        var activeTab = tabs.filter('.active');
        if (index == null) {
            index = activePane.index() + 1;
            index = (index == panes.length) ? 0 : index;
        }
        activePane.removeClass('active');
        panes.eq(index).addClass('active');
        tabs.eq(index).fadeIn('slow', function () { $(this).addClass('active'); });
        activeTab.fadeOut('slow', function () { $(this).removeClass('active'); });
        //        $('.banner').animate({
        //            opacity: 0.7
        //        }, 200, function() {
        //            $(this).removeClass('banner-slide' + active.index()).addClass('banner-slide' + index).animate({
        //                opacity: 1
        //            });
        //        });
    };

    var bannerSwitchTimer = null;
    var startBannerSwitch = function () {
        bannerSwitchTimer = setInterval(function () {
            switchBannerTab();
        }, 6000);
    };

    $('.banner li').hover(function () {
        if (bannerSwitchTimer != null)
            clearInterval(bannerSwitchTimer);
        switchBannerTab($(this).index());
    }, function () {
        startBannerSwitch();
    });

    startBannerSwitch();

    // $(".frm input[title != '']").tooltip({
    $(".showtip[title != '']").tooltip({
        // place tooltip on the right edge
        position: "top right",

        // a little tweaking of the position
        offset: [5, -150],

        // use the built-in fadeIn/fadeOut effect
        effect: "slide",

        // custom opacity setting
        opacity: 0.8
    });

    $(".showbigtip[title != '']").tooltip({
        position: "bottom left",
        offset: [-22, 2],
        effect: "fade",
        tipClass: 'bigtip'
    });

    $(".showtip-simple[title != '']").tooltip({
        position: "top center",
        offset: [0, 0],
        tipClass: 'simpletip'

    });

    $('.home-menu li li:last-child').addClass('home-menu-last');

    $('.slide-message').delay(10000).slideUp();

    $('.popup-link').click(function () {
        var wh = this.hash.substring(1).split(',');
        var rp = $(this).hasClass('reload-parent');
        $.colorbox({
            iframe: true,
            href: $(this).attr('href'),
            initialWidth: 200,
            initialHeight: 100,
            width: wh[0],
            height: wh[1],
            onClosed: function () { if (rp) window.location.reload(); }
        });
        return false;
    });

    $('.popup-link-ajax').click(function () {
        var wh = this.hash.substring(1).split(',');
        $.colorbox({
            href: $(this).attr('href'),
            initialWidth: 200,
            initialHeight: 100,
            width: wh[0],
            height: wh[1],
            onComplete: function () {
                $('#cboxLoadedContent').wrapInner('<div class="popup-frame" />');
            }
        });
        return false;
    });

    $('.popup-close').click(function () {
        $.colorbox.close();
    });

});

function closeColorBox() {
    $.fn.colorbox.close();
}

function openPopup(url, width, height) {
    $.fn.colorbox({
        href: url,
        initialWidth: 400,
        initialHeight: 300,
        width: width,
        height: height,
        iframe: true
    });
}

function gotoPage(url, total) {
    var pg = $('.pager-direct input').val();
    if (isNaN(pg) || pg > total || pg < 1) {
        alert('لطفا عدد صحیح وارد نمایید');
        return;
    }
    window.location = String.format(url, pg);
}

function showProgress() {
    $('#ajaxloader').show();
}

function hideProgress() {
    $('#ajaxloader').hide();
}

function autocompleteSearch(source) {
	$(".search-fields input").autocomplete({
		source: source,
        select: function(event, ui) { 
            $('.search-form form').submit();
        }
	}).data( "autocomplete" )._renderItem = function( ul, item ) {
		return $( "<li></li>" )
			.data( "item.autocomplete", item )
			.append( "<a>" + item.desc + "</a>" )
			.appendTo( ul );
	};
}

function IeRtlFix() {
    if ($.browser.msie && parseInt($.browser.version) == 9 && $(window).width() % 2 != 0) { $('#header, #content').width(961); } 
}
