var curIndex=0;
var arr=new Array();
var imgTexts=new Array();


function showImage(caller_)
{     
//alert( 'Show Images');
	var focus_title = '';

	var img_text = imgTexts[curIndex];
	var img_source = imgSources[curIndex];

	main_caller='';

	if ( caller_ == 'on_focus' ) {
		main_caller = 'on_focus';
		focus_title = document.getElementById('on_focus_title').innerHTML;
		img_text = imgTexts[curIndex];
		img_source = imgSources[curIndex];
	} 
	caller_ = null;

	if(arr[curIndex].match(/\/media\/pics/))  // if pic
        {       
		
				  var inner_html = '<table width=764 height=714 border=0 cellpadding=10><tr><td><h3>' + focus_title + '</h3></td></tr><tr><td valign=middle align=center width="100%" ><img src="'+arr[curIndex]+'" alt="'+img_text+'"  class="" />';
				  if ( img_source && img_source.length > 1 ) {
				  
					  inner_html = inner_html + '<br/><p><strong style="float:left;">Източник:</strong><span style="float:left;">&nbsp;'+img_source+'</span></p>';
				  }
				  inner_html = inner_html + '</td></tr><tr><td><p>'+img_text+'</p></td></tr></table>';

				  document.getElementById("popImage").innerHTML = '';
				  document.getElementById("popImage").innerHTML = inner_html ;
        } else if (arr[curIndex].match(/\/media\/videos/)) {      // if video

				document.getElementById("popImage").innerHTML = '<div id="popImage2"></div>';
				var flashParams = {allowScriptAccess: 'always', allowFullScreen : 'true', wmode: 'transparent', bgcolor: '#e0d9d5', align: 'middle'};
				var flashVars = {flv: arr[curIndex] ,img: img_text };
				swfobject.embedSWF("/flash/player.swf", "popImage2", "500", "421", "9.0.0", "expressInstall.swf", flashVars, flashParams);

		} else {                           // else embed video
				document.getElementById("popImage").innerHTML = '';
                document.getElementById("popImage").innerHTML=arr[curIndex];
        }       
        positionPicFrame();      
         $("#grey_transp").fadeIn(200);
		 $('#grey_transp').css("height",document.body.offsetHeight+"px");
 }

 
 function showNext()
 {
        if(curIndex<arr.length-1){curIndex++;}
        else{curIndex=0};
		showImage(main_caller);
 }
 
 // caller -> Flash
 function initImage(imgArray, imgIndex, texts)
 {      arr=imgArray;
		imgTexts=texts;
        curIndex=imgIndex;
        showImage();
 }
 
 function showPrevious()
 {
        if(curIndex>0){curIndex--;}
        else{curIndex=arr.length-1};
        showImage(main_caller);
 }
 
  $(document).ready(function(){
    $("div.gal_img").click(function () {

    });
        
        $("a.close_link").click(function () {
      $("#grey_transp").fadeOut(200);
    });

        $("a.gal_prev").click(function () {
    showPrevious()
    });
                
        $("a.gal_next").click(function () {
     showNext()
    });
  });
  
  function positionPicFrame() {
        picContainer = document.getElementById('popCont');
        windowSize = getWindowSize();
        scrollOffset = getScrollOffset();
        if (windowSize[1] > 720) {
                picContainerTopOffset = ((windowSize[1] - 650) / 2 )- 53;
        } else {
                picContainerTopOffset = 0;
        }
        picContainer.style.top = (scrollOffset[1]+picContainerTopOffset)+'px';
}

function getWindowSize() {
        var myWidth = 0, myHeight = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
                //Non-IE
                myWidth = window.innerWidth;
                myHeight = window.innerHeight;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
                //IE 6+ in 'standards compliant mode'
                myWidth = document.documentElement.clientWidth;
                myHeight = document.documentElement.clientHeight;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
                //IE 4 compatible
                myWidth = document.body.clientWidth;
                myHeight = document.body.clientHeight;
        }
        return [ myWidth, myHeight ];
}

function getScrollOffset() {
        var scrOfX = 0, scrOfY = 0;
        if( typeof( window.pageYOffset ) == 'number' ) {
                //Netscape compliant
                scrOfY = window.pageYOffset;
                scrOfX = window.pageXOffset;
        } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
                //DOM compliant
                scrOfY = document.body.scrollTop;
                scrOfX = document.body.scrollLeft;
        } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
                //IE6 standards compliant mode
                scrOfY = document.documentElement.scrollTop;
                scrOfX = document.documentElement.scrollLeft;
        }
        return [scrOfX,scrOfY ];
}


