$(document).ready(function(){

    $(".carousel").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        auto: 0,
        speed: 500,
        scroll: 1,
        visible: 3
    });
    (function autoAdvance(){
	        curr = $('.slider_container').find('.slider_item:visible');
            curr.fadeOut();
            if(curr.next().length)curr.next().fadeIn();
            else $('.slider_container').find('.slider_item:first').fadeIn();
	        // Устанавливаем таймер на 7 секунды.
	        timeOut = setTimeout(autoAdvance,7000);
    })();
    
    $('#left').click(
        function()
        {
            curr = $('.slider_container').find('.slider_item:visible');
            curr.fadeOut();
            if(curr.next().length)curr.next().fadeIn();
            else $('.slider_container').find('.slider_item:first').fadeIn();
        }
    );
    $('#right').click(
        function()
        {
            curr = $('.slider_container').find('.slider_item:visible');
            curr.fadeOut();
            if(curr.prev().length)curr.prev().fadeIn();
            else $('.slider_container').find('.slider_item:last').fadeIn();
        }
    );
    $('#lb a').lightBox();
});
function UnHide( eThis, realm ){
    if( document.getElementById(realm).className == 'cl' ){
        eThis.innerHTML = '<img src="/i/arrowdown.jpg"/>';
        document.getElementById(realm).className = '';
    }else{
        eThis.innerHTML = '<img src="/i/arrowleft.jpg"/>';
        document.getElementById(realm).className = 'cl';
    }
    return false;
}

function switchprev(e)
{
    $('#pre').find('.images-big-preview').hide();
    $('#'+e).show();
}

function SetCompare(e)
{
    cook = getCookie('compare');
    if(cook != '')
    {
        var compare = JSON.parse(cook);
        var newcompare = new Array();
        var flag = 0;
        if(is_array(compare))
        {
            for(var i=0; i < compare.length; i++)
            {
                if(compare[i] != e.id)newcompare.push(compare[i]);
                else flag = 1;
            }
            if(flag != 1)newcompare.push(e.id);
        }
        else newcompare.push(e.id);
        jn = array2json(newcompare);
        setCookie('compare', jn, '/', '.digis.smarthost.kz');
    }
    else
    {
        var compare = new Array();
        compare.push(e.id);
        jn = array2json(compare);
        setCookie('compare', jn, '/', '.digis.smarthost.kz');
    }

    
}

/**
 * Converts the given data structure to a JSON string.
 * Argument: arr - The data structure that must be converted to JSON
 * Example: var json_string = array2json(['e', {pluribus: 'unum'}]);
 * 			var json = array2json({"success":"Sweet","failure":false,"empty_array":[],"numbers":[1,2,3],"info":{"name":"Binny","site":"http:\/\/www.openjs.com\/"}});
 * [url]http://www.openjs.com/scripts/data/json_encode.php[/url]
 */
function array2json(arr) {
    var parts = [];
    var is_list = (Object.prototype.toString.apply(arr) === '[object Array]');

    for(var key in arr) {
    	var value = arr[key];
        if(typeof value == "object") { //Custom handling for arrays
            if(is_list) parts.push(array2json(value)); /* :RECURSION: */
            else parts[key] = array2json(value); /* :RECURSION: */
        } else {
            var str = "";
            if(!is_list) str = '"' + key + '":';

            //Custom handling for multiple data types
            if(typeof value == "number") str += value; //Numbers
            else if(value === false) str += 'false'; //The booleans
            else if(value === true) str += 'true';
            else str += '"' + value + '"'; //All other things
            // :TODO: Is there any more datatype we should be in the lookout for? (Functions?)

            parts.push(str);
        }
    }
    var json = parts.join(",");

    if(is_list) return '[' + json + ']';//Return numerical JSON
    return '{' + json + '}';//Return associative JSON
}

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function is_array(input)
{
    return typeof(input)=='object'&&(input instanceof Array);
}

