﻿/* SEO AJAX Framework */
/* Preferences */

var loader = true; // false if no Loader
var http = false;

var parts = window.location.href.split("#");


if(navigator.appName == "Microsoft Internet Explorer") 
{
    try 
    { 
        http = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    catch (e) 
    { 
        try 
        { 
            http = new ActiveXObject("Msxml2.XMLHTTP"); 
        }
        catch (e) 
        {} 
    } 
} 
else 
{
        http = new XMLHttpRequest(); 
        if (http.overrideMimeType) 
        { 
            http.overrideMimeType('text/html'); 
        } 
}


if(parts.length > 1)
{
        location.href=parts[1];
}
function debug(msg, clear) {
        var output = 
                document.getElementById("output");
        if (clear == true)
          output.innerHTML = "<p>" + msg + "</p>";
        else {
          output.innerHTML += 
                            "<p>" + msg + "</p>";
        }
}


        
//Load function to add onclick function to menuitems
window.onload = load;
function load() {
Init();
}
function Init(){
	var part = window.location.href.split("#");
    if(part.length > 1)
	{
    }
    if(document.getElementsByTagName)
    {
        //Get all element with tag <a>
        var links = document.getElementsByTagName('a');
        var menuUrl = "#";
        
        //Browse arraylist Links
        for(var i = 0; i < links.length ; i++)
        {
            //If elmenent is a menuitem
            if(links[i].className.match('MenuItem'))
            {
                  //Add function to onclick
                  links[i].onclick = function(){
                   
                  var att = this.attributes;
                  menuUrl = att.getNamedItem("href").value;
                 
                  window.location.hash = menuUrl;
                  window.status = att.getNamedItem("title").value;
                  document.title = att.getNamedItem("title").value;
				  contdv = att.getNamedItem("class").value;
				  
				  var cont = contdv.split("#");
				  contdiv = cont[1];
				  jsload = cont[2];
                  getHTML(menuUrl, contdiv, jsload);
                  return false;
                };
            }
        }
    }
    
    return true;
}
function historyChange(newLocation, historyData) {
        if(newLocation == "")
        {
            getHTML("index.php", "content" );
        }
        else
        {
            getHTML(newLocation, contdiv, jsload);
        }
}

function getHTML(menuUrl, contdiv, jsload)
{
 
  //Open url given by href attribute
  http.open("GET",menuUrl, true);
  
  //If page is loaded
  http.onreadystatechange=function() {
    if(http.readyState == 3)
    {
        if(loader)
        {
        document.getElementById(contdiv).innerHTML = "<br/><br/><center><img src=\"http://blagavest.bg/images/slider/ajax_loader.gif\" title=\"Loading...\" /></center>";
        }
    }
    if(http.readyState == 4) {
        if(http.status == 200)
        {
        FadeIn( 0 );
document.getElementById(contdiv).style.display = "block";
for( var i = 0 ; i <= 100 ; i++ )
setTimeout( 'FadeIn(' + (i / 10) + ')' , 3 * i );
             div=document.createElement('div');  
             div.innerHTML = http.responseText;
             document.getElementById(contdiv).innerHTML = gethtmlDiv(div);
                         Init();
        var shortUrl = menuUrl.replace("http://blagavest.bg","");
	_gaq.push(['_trackPageview', shortUrl]);

	
				  		 if (jsload == 'video'){
							  jwplayer('mediaPlayer').setup({
							   'skin': 'http://blagavest.bg/jwplayer/glow/glow.zip',
						 	   'id': 'mediaPlayer',
						 	   'width': '640',
						 	   'height': '360',
						 	   'file': document.getElementById('pFile').value,
						 	   'image': document.getElementById('pImage').value,
						 	   'plugins': {
						       		'gapro-2': {}
							    },
						 	   'modes': [
						 	   		{type: 'html5'},
						 	   		{type: 'flash', src: 'http://blagavest.bg/jwplayer/player.swf'},
						 	   		{type: 'download'}
						 	   		]
						 	   	});				  		 
						}
				  		 
                          $(document).ready(function() {
        					        $(".login").fancybox({
										helpers: {
											title : {
											type : 'outside'
											},
											overlay : {
											speedIn : 500,
											opacity : 0.95
											}
										}
									});
   						 });
   						if(jsload == 'gallery') {
   						 jQuery(document).ready(function($) {
				// We only want these styles applied when javascript is enabled
				$('div.content-gal').css('display', 'block');

				// Initially set opacity on thumbs and add
				// additional styling for hover effect on thumbs
				var onMouseOutOpacity = 0.67;
				$('#thumbs ul.thumbs li, div.navigation a.pageLink').opacityrollover({
					mouseOutOpacity:   onMouseOutOpacity,
					mouseOverOpacity:  1.0,
					fadeSpeed:         'fast',
					exemptionSelector: '.selected'
				});
				
				// Initialize Advanced Galleriffic Gallery
				var gallery = $('#thumbs').galleriffic({
					delay:                     2500,
					numThumbs:                 6,
					preloadAhead:              3,
					enableTopPager:            false,
					enableBottomPager:         false,
					imageContainerSel:         '#slideshow-gal',
					controlsContainerSel:      '#controls',
					captionContainerSel:       '#caption',
					loadingContainerSel:       '#loading',
					renderSSControls:          true,
					renderNavControls:         true,
					playLinkText:              'Пусни Слайдшоу',
					pauseLinkText:             'Спри Слайдшоу',
					prevLinkText:              '&lsaquo; Предишна Снимка',
					nextLinkText:              'Следваща Снимка &rsaquo;',
					nextPageLinkText:          'Следваща &rsaquo;',
					prevPageLinkText:          '&lsaquo; Предишна',
					enableHistory:             false,
					autoStart:                 false,
					syncTransitions:           true,
					defaultTransitionDuration: 900,
					onSlideChange:             function(prevIndex, nextIndex) {
						// 'this' refers to the gallery, which is an extension of $('#thumbs')
						this.find('ul.thumbs').children()
							.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
							.eq(nextIndex).fadeTo('fast', 1.0);

						// Update the photo index display
						this.$captionContainer.find('div.photo-index')
							.html((nextIndex+1) +' от '+ this.data.length);
					},
					onPageTransitionOut:       function(callback) {
						this.fadeTo('fast', 0.0, callback);
					},
					onPageTransitionIn:        function() {
						var prevPageLink = this.find('a.prev').css('visibility', 'hidden');
						var nextPageLink = this.find('a.next').css('visibility', 'hidden');
						
						// Show appropriate next / prev page links
						if (this.displayedPage > 0)
							prevPageLink.css('visibility', 'visible');

						var lastPage = this.getNumPages() - 1;
						if (this.displayedPage < lastPage)
							nextPageLink.css('visibility', 'visible');

						this.fadeTo('fast', 1.0);
					}
				});

				/**************** Event handlers for custom next / prev page links **********************/

				gallery.find('a.prev').click(function(e) {
					gallery.previousPage();
					e.preventDefault();
				});

				gallery.find('a.next').click(function(e) {
					gallery.nextPage();
					e.preventDefault();
				});

			});
						 }
						 
						if(jsload == 'blagovestnik') {
   						 jQuery(document).ready(function($) {
				// We only want these styles applied when javascript is enabled
				$('div.content-gal').css('display', 'block');

				// Initially set opacity on thumbs and add
				// additional styling for hover effect on thumbs
				var onMouseOutOpacity = 0.67;
				$('#thumbs ul.thumbs li, div.navigation a.pageLink').opacityrollover({
					mouseOutOpacity:   onMouseOutOpacity,
					mouseOverOpacity:  1.0,
					fadeSpeed:         'fast',
					exemptionSelector: '.selected'
				});
				
				// Initialize Advanced Galleriffic Gallery
				var gallery = $('#thumbs').galleriffic({
					delay:                     3500,
					numThumbs:                 7,
					preloadAhead:              3,
					enableTopPager:            false,
					enableBottomPager:         false,
					imageContainerSel:         '#slideshow-gal1',
					controlsContainerSel:      '#controls',
					captionContainerSel:       '#caption',
					loadingContainerSel:       '#loading',
					renderSSControls:          true,
					renderNavControls:         true,
					playLinkText:              'Пусни Слайдшоу',
					pauseLinkText:             'Спри Слайдшоу',
					prevLinkText:              '&lsaquo; Предишна страница',
					nextLinkText:              'Следваща страница &rsaquo;',
					nextPageLinkText:          'Следваща &rsaquo;',
					prevPageLinkText:          '&lsaquo; Предишна',
					enableHistory:             false,
					autoStart:                 true,
					syncTransitions:           true,
					defaultTransitionDuration: 900,
					onSlideChange:             function(prevIndex, nextIndex) {
						// 'this' refers to the gallery, which is an extension of $('#thumbs')
						this.find('ul.thumbs').children()
							.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
							.eq(nextIndex).fadeTo('fast', 1.0);

						// Update the photo index display
						this.$captionContainer.find('div.photo-index')
							.html((nextIndex+1) +' от '+ this.data.length);
					},
					onPageTransitionOut:       function(callback) {
						this.fadeTo('fast', 0.0, callback);
					},
					onPageTransitionIn:        function() {
						var prevPageLink = this.find('a.prev').css('visibility', 'hidden');
						var nextPageLink = this.find('a.next').css('visibility', 'hidden');
						
						// Show appropriate next / prev page links
						if (this.displayedPage > 0)
							prevPageLink.css('visibility', 'visible');

						var lastPage = this.getNumPages() - 1;
						if (this.displayedPage < lastPage)
							nextPageLink.css('visibility', 'visible');

						this.fadeTo('fast', 1.0);
					}
				});

				/**************** Event handlers for custom next / prev page links **********************/

				gallery.find('a.prev').click(function(e) {
					gallery.previousPage();
					e.preventDefault();
				});

				gallery.find('a.next').click(function(e) {
					gallery.nextPage();
					e.preventDefault();
				});

			});
						 }
						 
						 if(jsload == 'liveaudio'){
							 jwplayer('live').setup({
 							    'skin': 'http://blagavest.bg/jwplayer/glow/glow.zip',
 							    'controlbar': 'over',
 							    'id': 'Live',
 							    'width': '640',
 							    'height': '360',
 							    'file': 'http://blagavest.bg:8000/;',
 							    'image': 'http://blagavest.bg/images/live_preview.jpg',
 							    'type': 'sound',
 							    'plugins': {
							       'gapro-2': {}
							    },
 							    'modes': [
							        {type: 'flash', src: 'http://blagavest.bg/jwplayer/player.swf'},
							        {type: 'html5'},
							    ]
							  });
						 }
						 if(jsload == 'livevideo'){
  							jwplayer('live').setup({
 							    'skin': 'http://blagavest.bg/jwplayer/glow/glow.zip',
							    'id': 'playerID',
							    'width': '640',
							    'height': '360',
							    'provider': 'rtmp',
							    'streamer': 'rtmp://blagavest.bg/video',
							    'file': 'live',
							    'plugins': {
							       'gapro-2': {}
							    },
							    'image': 'http://blagavest.bg/images/live_preview.jpg',
							    'modes': [
							        {
 							         type: 'html5',
 							         config: {
 							           'file': 'http://blagavest.bg:1935/video/live/playlist.m3u8',
							           'provider': 'http'
							         	}
  							        },
							        {type: 'flash', src: 'http://blagavest.bg/jwplayer/player.swf'}
 								 ]
							  });

                        	$(document).ready(function() {
								$("#responsecontainer").load("http://blagavest.bg/live.php");
								var refreshId = setInterval(function() {
								$("#responsecontainer").load('http://blagavest.bg/live.php');
								}, 3000);
								$.ajaxSetup({ cache: false });
							});
							$(document).ready(function() {
								//Show link
								$(".showlink").click(function() {
									$("#responsecontainer").css('height','337px');
									$(".showlink").hide();
									$(".hidelink").show();
								});
								//Hide link 
								$(".hidelink").click(function() {
									$("#responsecontainer").css('height','150px');
									$(".hidelink").hide();
									$(".showlink").show();
								});
								//ajax
								$(".button").click(function() {

								var uid = $("#uid").val();
								var comment = $("#comment").val();
								var dataString = 'uid='+ uid + '&comment=' + comment;
								if(uid=='')
								{
									alert('Моля влезте във вашият профил!');
								}
								else if(comment=='')
								{
									alert('Моля въведете текст!');
								}
								else
								{
									$.ajax({
										type: "POST",
										url: "/addComment.php",
										data: dataString,
										cache: false,
										success: function(html){
											$("ol#com_messages").before(html);
											document.getElementById('comment').value='';
										}
									});
								}
								return false;
								});

							});
						 }

                         if(jsload == 'home'){
       						 $('#slider').nivoSlider({
       						 effect:"random",
       						 slices:15,
       						 boxCols:8,
       						 boxRows:4,
       						 animSpeed:500,
       						 pauseTime:3000,
       						 startSlide:0,
       						 directionNav:false,
       						 directionNavHide:true,
       						 controlNav:true,
       						 controlNavThumbs:false,
       						 controlNavThumbsFromRel:true,
       						 keyboardNav:true,
       						 pauseOnHover:true,
       						 manualAdvance:false
       						 });
       						 $('#counter').countdown();
       					}
}
        else
        {
             document.getElementById(contdiv).innerHTML = "<center>... Could not open page ...</center>";
        }
    }
  }
  
  http.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
  http.send(null);
  return true;
}
function FadeIn(value) {
document.getElementById(contdiv).style.opacity = value / 10;
document.getElementById(contdiv).style.filter = 'alpha(opacity=' + value * 10 + ')';
}
function gethtmlDiv(dv)
{
    var html;
    var boolfound = false;
    
    for(var j=0,d;d=dv.childNodes[j];j++)
    {

         if(d.nodeName == 'DIV')
         {
           if(d.id)
           {
               if(d.id.match(contdiv))
               {
                     html = d.innerHTML;
                     boolfound = true;
               }
               else
               {
                    html = gethtmlDiv(d);
               }
            }
            else
            {
                    html = gethtmlDiv(d);
            }
        }
        else
        {
                html = gethtmlDiv(d);
        }
        
        if(html)
        {
            break;
        }
        if(boolfound)
        {
            break;
        }
    }
    return html;
}
