
var
speed = 700,
bg_cur = 1, bg_total = 7, bg_timer = 5000, timer_on = 1, timer, t, bg, bg_img, bg_img_new,
nav, navLi,
nav_sub, nav_subLi,
content,
ajx_url, ajx_context, ajx_start, ajx_success, ajx_complete;

$(document).ready(function(e) {
    ini();
});

function ini(){
    //disable_contextMenu();
    ini_globals();
    ini_nav();
    ini_controls();
   ini_menusLinks();
    //timer();
}

function ini_globals(){
    
    bg = $('#bg');
    bg_img = bg.find('img');
	
    nav = $('#nav');
    navLi = nav.find('li');
    nav_sub = navLi.find('ul');
    nav_subLi = nav_sub.find('li');
    
    ajx_start = function(){
    //display indicator
    }
    
    ajx_success = function(){
    //hide indicator
    }
    
    timer = function(){
        timer_on == 1 ? t = setInterval(change_bg, bg_timer) : clearInterval(t);
    }
}

function ini_nav(){
    navLi.hover(function(){
        $(this).find('.sn').stop().fadeTo(speed, 1);
    }
    ,function(){
        $(this).find('.sn').stop().fadeTo(speed, 0, function(){
            $(this).css('display', 'none');
        });
    });
    
    $('.sn li').hover(function(){
        $(this).find('.sn2').stop().fadeTo(speed, 1);
    }
    ,function(){
        $(this).find('.sn2').stop().fadeTo(speed, 0, function(){
            $(this).css('display', 'none');
        });
    });
    
    $('.sn2 li').hover(function(){
        $(this).find('ul').stop().fadeTo(speed, 1);
    }
    ,function(){
        $(this).find('ul').stop().fadeTo(speed, 0, function(){
            $(this).css('display', 'none');
        });
    });
}

function ini_menusLinks(){
    nav.find('a').click(function(e){
        e.preventDefault();
        var parent = $(this).parent();
        var index = parent.index();
//        var link = String($(this).text());
        var link = $(this).attr('href');
//        link = link.toLowerCase();
//        link = link.replace(/ /gi, '-');
//        link = '/content/'+link;
		  link = link.replace('&','and');
		  link = link.replace('content/','');
		  if(link.indexOf('.pdf') > 0)
		  {
		  	ajx_url = '/pages/pdf/'+link;
		  	window.open(ajx_url);
		  }
		  else
		  {
        	if(link != '')
        	{
        	ajx_url = '/pages/content/'+link+'.html';
        	window.location.replace(ajx_url);        	
        	}
          }
        
        //alert(ajx_url);
        //change_content();
    });
}

function ini_controls(){
    $('.slider-contorls img').click(function(){
        bg_cur = $(this).index();
        timer_on = 0;
        change_bg();
    });
    
    $('.play-button').click(function(){
        play_vid(); 
    });
	
	$('.close').click(function(){
		play_vid();
		});
}

function disable_contextMenu(){
    $(document).bind("contextmenu",function(e){
        return false;
    });
}


function change_bg(){
    ajx_complete = function(){
        ajx_success;
        var bg_clone = bg_img.clone();
        bg_clone.prependTo(bg).addClass('remove');
        bg.find('.none').fadeIn(speed + 700, function(){
            $(this).removeAttr('class');
            bg.find('.remove').remove();
            bg_img = $(this);
            var pre_bg = bg_cur-1;
            pre_bg == 0 ? pre_bg = 1 : null;
            $('.slider-contorls img').attr('src', 'img/slider-button.png');
            $('.slider-contorls img:nth-child('+ bg_cur +')').attr('src', 'img/slider-button1.png');
        });
    }
    bg_cur++;
    bg_cur > bg_total ? bg_cur = 1 : null;
    ajx_context = bg;
    ajx_url = '?bg=' + bg_cur;
    ajx();
}

function change_content(){    
    ajx_context = $('.top-part');
    ajx_complete = function(){
        ajx_success;
        ajx_context.find('.none').removeClass('none');
        ajx_context.fadeIn();
    }
    ajx();
}

function ajx(){
    ajx_start;
    ajx_context.load(ajx_url, ajx_complete);
}

function play_vid(){
    $('#video').toggle(speed);
}
/**/
