// JavaScript Document
var isGallery = false;
$(document).ready(function(){

    function changeBackground() {
        var images = jQuery.parseJSON(HEADER_IMAGES);
        console.log(images);
        jQuery.each(images).each(function (i, data) {
            console.log(SITE_URL+'/wp-content/header-images/'+data);
            if ($('#content').css('display') != 'block') {
                $('#backgrounder').animate({opacity:0},3000,function () {
                    $('#backgrounder').attr('src', SITE_URL+'/wp-content/header-images/'+data);
                    setTimeout(changeBackground, 9000);
                }).animate({opacity:1},4000);
            } else {
                setTimeout(changeBackground, 9000);
            }
        });
    }

    SLIDES = jQuery.parseJSON(HEADER_IMAGES);
    SLIDE_I = 0;
    SLIDE_N = SLIDES.length-2;
    SLIDE = SLIDES[SLIDE_I];

    function slider() {
        if ($('#content').css('display') != 'block') {
            $('#backgrounder').animate({opacity:0},0,function () {
                $('#backgrounder').attr('src', SITE_URL+'/wp-content/header-images/'+SLIDE);
            }).animate({opacity:1},3000);
        }

            SLIDE_I++;

            if (SLIDE_I == SLIDE_N) {
                SLIDE_I = 0;
            }

            SLIDE = SLIDES[SLIDE_I];
    }

    //setInterval(slider, 5000);
    var images_objs = [];

    $.each(SLIDES, function (i,v) {
        if (v != '..' && v != '.') {
            images_objs.push(
                { src: WP_CONTENT_URL+'/header-images/'+v }
            );
        }
    });

    function stripos (f_haystack, f_needle, f_offset) {
        // Finds position of first occurrence of a string within another, case insensitive
        //
        // version: 1109.2015
        // discuss at: http://phpjs.org/functions/stripos
        // +     original by: Martijn Wieringa
        // +      revised by: Onno Marsman
        // *         example 1: stripos('ABC', 'a');
        // *         returns 1: 0
        var haystack = (f_haystack + '').toLowerCase();
        var needle = (f_needle + '').toLowerCase();
        var index = 0;

        if ((index = haystack.indexOf(needle, f_offset)) !== -1) {
            return index;
        }
        return false;
    }
if(kera_kha==1){
        $('#place-bg').crossSlide({
            sleep: 3,
            fade: 1
        }, images_objs);
}
    function stopSlider() {

       // if (stripos($('#content').attr('style'), 'block') !== false || stripos($('#navigation').attr('style'),'block') !== false) {
		    if (stripos($('#content').attr('style'), 'block') !== false) {
            $('#place-bg').crossSlidePause();

            //$('#place').css('background', 'url(/wp-content/header-images/'+SLIDES[0]+')');
        }
    }

    setInterval(stopSlider, 1000);


	$(window).bind('resize', function() { my_slide() });
	my_slide();
	
	if($.browser.msie && $.browser.version < 7){
		$('body').html('<div class="ieerrore">Sorry, your browser version is outdated. You could update it here <a href="http://windows.microsoft.com/en-US/internet-explorer/downloads/ie">microsoft.com</a></div>');
	}
	//////////////
	
	$('#navigation ul li.current_page_item').addClass('currentrmnu');

	$('#navigation ul li a').click(function(){

        if ($(this).hasClass('xcp')) {
            $('#header .title').remove();
        }

        $('#navigation ul li a').each(function () {
            if (!$(this).hasClass('xcp')) {
                $(this).addClass('xcp');
            }
        });

		/////////////////////////////////////////////////////////
		// if(!$(this).parent('li').find('li.current-menu-item:first').is('li')) {	
		
			//$(this).parent('li').children('ul:first').find('a').each(function(){
			//if(!$(this).parent('li').children('ul').is('ul')){
				//window.location = $(this).attr('href');
				//return false;
			//}
		 // });
			//return true;
		//}
		//////////////////////////////////////
		
		$('#content').fadeOut('fast');

/*        $.ajax('/ajax', {
            type: 'POST',
            data: {
                'act': 'show_media_header'
            },
            success: function (data) {
                $('#backgrounder').attr('src', data);
            }
        });*/


		if($(this).parent('li').children('ul:first').is('ul')) {
			$('#left-nav').html( '<ul id="lmnu" style="color:#ffffff; font-family:Arial, Helvetica, sans-serif; font-size:12px;">' + $(this).parent('li').children('ul:first').html() + '</ul>' ) ;
		} else {

            // Get slug from url
            var href = $(this).attr('href').split('/');
			
            var slug = href[href.length-1];

            if (slug == "") {
                slug = href[href.length-2];
            }
            $.ajax(SITE_URL+'/ajax', {
                'data': {
                    'act': 'postimage',
                    'page': slug
                },
                'dataType': 'json',
                'async': false,
                'type': 'POST',
                'success': function (data) {
					if (data) {

                        galleryListVal(data.join(''));
                        
                        return false;
                    } else {
                        return true;
                    }
                },
                'error': function () {
					return true;
                }
            });
		}

		$('#navigation ul li.currentrmnu').removeClass('currentrmnu');
		$(this).parent('li').addClass('currentrmnu');
		$('#lt .title').text($(this).text());
		
		$('#left-nav ul li a').unbind('click').bind('click', function() {
            
            if ($(this).hasClass('event-link')) return true;
			if($(this).parent('li').children('ul').is('ul')){
				$(this).parent('li').parent('ul').children('li').children('ul').hide('normal');
				$(this).parent('li').children('ul').toggle('normal');
				return false;
			}
			if($(this).parent('li').is('.current-menu-item')){
				$('#content:hidden').fadeIn('fast');
				if(isGallery) {
					galleryList();
				}

				return false;
			}		
		});

		
		$('#place #place-lt:hidden, #place #place-rt:hidden, #place #place-lb:hidden, #footer:hidden').fadeIn('fast');

		return false;
	});
	
	
	$('#navigation ul li.current-menu-ancestor a:first').click();
	$('#left-nav ul li.current-menu-ancestor a:first').click();
	
	$('#lt .title').text($('#navigation ul li.currentrmnu').children('a').text());
	//$('#place-bg').click(function(){ if($('#navigation').css('display') == 'none') });
	$('#nav-btn,#place-bg').click(function(){
		if($('#navigation').css('display') == 'none')
		{
			$('#navigation').stop(true, true).fadeIn('fast', function(){
				$('#content').removeClass('fullsize');
				
			});
		}
		else
		{		
			$('#content').addClass('fullsize');
			$('#navigation').stop(true, true).fadeOut('fast', function(){
				
			});
		}
		
	});
	
	$('#content #close').click(function(){
		  $('#content').fadeOut('fast');
	});

    if ($('body').hasClass('events-single')) {
        $.ajax(SITE_URL+'/ajax', {
            'data': {
                'act': 'postimage',
                'page': 'events'
            },
            'dataType': 'json',
            'async': false,
            'type': 'POST',
            'success': function (data) {
                if (data) {
                    galleryListVal(data.join(''));
                    
                    return false;
                } else {
                    return true;
                }
            },
            'error': function () {
                
                return true;
            }
        });
    }

			
});

	/// start edit by shtrih		 // Background			   
  function my_slide()
  {
	var window_dimensions = {width: $(window).width(), height: $(window).height()};
	var window_ratio = window_dimensions.width / window_dimensions.height;
	$('#background-slideshow').css({
            width: window_dimensions.width + 'px',
            height: window_dimensions.height + 'px'
        }); 
	
	var my_image = $('#background-slideshow img');

	//Start spike
	var image_dimensions = {width: my_image.width(), height: my_image.height()};
    //End spile
	
	var image_ratio = image_dimensions.width / image_dimensions.height;
	
	
		if (image_ratio == window_ratio) {
            my_image.css({
                width: window_dimensions.width + 'px',
                height: window_dimensions.height + 'px'
            });
        } else if (image_ratio > window_ratio) {
            my_image.css({
                width: (window_dimensions.width * image_ratio / window_ratio) + 'px',
                height: window_dimensions.height + 'px'
            });
        } else if (image_ratio < window_ratio) {
            my_image.css({
                width: window_dimensions.width + 'px',
                height: (window_dimensions.height * window_ratio / image_ratio) + 'px'
            });
        }	
		var mtop = (window_dimensions.height - $('#main').height() ) / 2;
		if(mtop < 0) {mtop = 0;}
		$('#main').css('top', mtop + 'px');
		
		var mlft = (my_image.width() - window_dimensions.width) / 2;
		if(mlft < 0) {mlft = 0;}
		$('#background-slideshow img').css('marginLeft', '-' + mlft + 'px');
  	}  
	
function indexphp() {
	$('#place #place-lt:hidden, #place #place-rt:hidden, #place #place-lb:hidden, #footer:hidden, #navigation:hidden, #content:hidden').fadeIn('fast');
	
	if($('#left-nav ul li.current-menu-ancestor:first ul li.current-menu-item:first').is('li'))
	{
		$('#header .title').text($('#left-nav ul li.current-menu-ancestor:first a:first').text() + ' / "' + $('#left-nav ul li.current-menu-ancestor:first ul li.current-menu-item:first a:first').text() + '"' );
	}
	else 
	{
		if($('#left-nav ul li.current-menu-item:first').is('li'))
		{
			$('#header .title').text( $('#left-nav ul li.current-menu-item:first a:first').text() );
		}
	}	
}


function galleryListVal(li) {
    $('#left-nav').html('<div id="arr-top"></div><div id="gallery-scroll"><ol id="thumbnail-gallery">' + li +  '</ol></div><div id="arr-bottom"></div>') ;
    $('#left-nav #thumbnail-gallery li a').click(function(){
        
        if ($(this).hasClass('event-link')) return true;
        $('#content .full-content').html($(this).children('.image').html());
        $('#content #desc .cont').html('<div class="title">' + $(this).attr('title') + '</div>' + $(this).children('.description').html() );
        $('#content:hidden').fadeIn('fast');
        $('#thumbnail-gallery li.curr').removeClass('curr').stop(true, true).fadeTo('normal', 0.5);
        $(this).parent('li').addClass('curr').stop(true, true).fadeTo('normal', 1.0);

        return false;
    });

    $('#left-nav #thumbnail-gallery li, #arr-bottom, #arr-top').fadeTo(0, 0.5);
    $('#left-nav #thumbnail-gallery li, #arr-bottom, #arr-top').hover(function() {
        $(this).not('.curr').stop(true, true).fadeTo('normal', 1.0);
    },function(){
        $(this).not('.curr').stop(true, true).fadeTo('normal', 0.5);
    });

    $('#arr-top').click(function() {
        var newTop = parseInt($('#thumbnail-gallery').css('top')) + 365;
        
		if(newTop <= 0) {
            $('#thumbnail-gallery').stop(true, true).animate({top:'+=365px'}, 365);
        }

    });
    $('#arr-bottom').click(function() {
        var newTop = parseInt($('#thumbnail-gallery').css('top')) - 365;
       // if(newTop >=  -(parseInt($('#thumbnail-gallery').css('height')) - 365)    ) {
		 if(newTop >=  -(parseInt($('#thumbnail-gallery').css('height')) )    ) {
            $('#thumbnail-gallery').stop(true, true).animate({top:'-=365px'}, 365);
        }


    });
}

