/* Configuration variables */

var conf_reflection_p = 0;         // Sets the height of the reflection in % of the source image 

var conf_focus = 4;                  // Sets the numbers of images on each side of the focussed one

var conf_slider_width = 48;          // Sets the px width of the slider div

var conf_images_cursor = 'pointer';  // Sets the cursor type for all images default is 'default'

var conf_slider_cursor = 'default';  // Sets the slider cursor type: try "e-resize" default is 'default'



/* Id names used in the HTML */

var conf_imageflow = 'imageflow';    // Default is 'imageflow'

var conf_loading = 'loading';        // Default is 'loading'

var conf_images = 'images';          // Default is 'images'

var conf_captions = 'captions';      // Default is 'captions'

var conf_scrollbar = 'scrollbar';    // Default is 'scrollbar'

var conf_slider = 'slider';          // Default is 'slider'



/* Define global variables */

var caption_id = 0;

var new_caption_id = 0;

var current = 750;

var target = 0;

var mem_target = 0;

var timer = 0;

var array_images = new Array();

var new_slider_pos = 0;

var dragging = false;

var dragobject = null;

var dragx = 0;

var posx = 0;

var new_posx = 0;

var xstep = 140;

var htmlContent = "";
var adUnitNamesArray = ["Roller","Mobile Flash","HTML5 Video","HTML5 Rich Media","Video Overpass","Rotator","Rotator + Slider"];

function setOpacity(image, value)

{

	if (image.filters && image.filters[0])

	{

	    image.filters[0].opacity = value * 100;
	    //console.log("If Opacity: " + value * 100);

	}

	else

	{
	    image.style.opacity = value;
		//console.log("Else Opacity: " + value);

	}

}



function step()

{

	switch (target < current-1 || target > current+1) 

	{

	    case true:

	        moveTo(current + (target - current) / 3);	      
	        window.setTimeout(step, 120); //changed to 70, initially it was 50

	        timer = 1;

	        break;



	    default:

	        timer = 0;	       
	        break;

	}

}


function glideTo(x, new_caption_id)

{	

	/* Animate gliding to new x position */

	target = x;

	mem_target = x;

	if (timer == 0)

	{

		window.setTimeout(step, 120);

		timer = 1;

	}

	

	/* Display new caption */

	caption_id = new_caption_id;

	caption = img_div.childNodes.item(array_images[caption_id]).getAttribute('alt');

	if (caption == '') caption = '&nbsp;';

	caption_div.innerHTML = caption;



	/* Set scrollbar slider to new position */

	if (dragging == false)

	{

		new_slider_pos = (scrollbar_width * (-(x*100/((max-1)*xstep))) / 100) - new_posx;

		slider_div.style.marginLeft = (new_slider_pos - conf_slider_width) + 'px';

	}

}



function moveTo(x) {    
	current = x;

	var zIndex = max;	

	/* Main loop */
    leftArr = [];
	for (var index = 0; index < max; index++) {
	   
		var image = img_div.childNodes.item(array_images[index]);

		var current_image = index * -xstep;
		
		/* Don't display images that are not conf_focussed */

//		if ((x < -(xstep*5.5)) || (x > (xstep*5.5)))

//		{

//			image.style.visibility = 'hidden';

//			image.style.display = 'none';

//		}
		if ((current_image + max_conf_focus) < mem_target || (current_image - max_conf_focus) > mem_target) {
		    image.style.visibility = 'hidden';
		    image.style.display = 'none';		   		    
		}
		else 

		{

			var z = Math.sqrt(10000 + x * x) + 100;

			var xs = x / z * size + size;

			/* Still hide images until they are processed, but set display style to block */

			image.style.display = 'block';
			image.style.visibility = 'visible';
		
			/* Process new image height and image width */
              
			var new_img_h = (image.h / image.w * image.pc) / z * size;
			var check = 0;
			switch ( new_img_h > max_height )

			{

			    case false:

			        var new_img_w = image.pc / z * size;
			        if (image.i == '3') {//for the ipad
			            //console.log("If New Image Height: " + new_img_h + " Image: " + image.i + " Max height: " + max_height);
			            new_img_w = image.pc / z * size + 30;
			            check = 0;
			        }
			        break;



			    default:

			        new_img_h = max_height;
			        //console.log("Else New Image Height: " + new_img_h + " Image: " + image.i + " Max height: " + max_height);
			        var new_img_w = image.w * new_img_h / image.h + 20;
			        if (image.i == '3') { //for the ipad
			            //console.log("If New Image Height: " + new_img_h + " Image: " + image.i + " Max height: " + max_height);
			            new_img_w = image.pc / z * size + 65;
			            check = 1;
			        }
			        break;

			}

			var new_img_top = (images_width * 0.34 - new_img_h) * 0.5 + images_top;

			/* Set new image properties */
			if (check == 1 && image.i == '3') {
			    image.style.left = xs - (image.pc / 2) / z * size + images_left - 30 + 'px';
			}
			else {
			    image.style.left = xs - (image.pc / 2) / z * size + images_left + 'px';
			}
			
			if(new_img_w && new_img_h)

			{

			    image.style.height = new_img_h + 'px'; 				

				image.style.width = new_img_w + 'px';

				image.style.top = new_img_top + 'px';				

			}

			

			/* Set the image opacity */

			if ((x < -(xstep*4.5)) || (x > (xstep*4.5)))

			{

				if (x > 0)

					setOpacity(image, (xstep*5.5-x)/xstep);

				else

					setOpacity(image, (xstep*5.5+x)/xstep);

			}

			else

			{

				setOpacity(image, 1);

			}			

			/* Set image layer through zIndex */

			switch ( x < 0 )

			{

				case true:

					zIndex++;

					break;



				default:

					zIndex = zIndex - 1;

					break;

			}

			

			/* Change zIndex and onclick function of the focussed image */

			switch ( image.i == caption_id )

			{

			    case false:

			        image.onclick = function() { glideTo(this.x_pos, this.i);  }

			        break;



			    default:

			        zIndex = zIndex + 1;

			        image.onclick = function() {
			            /*document.location = this.url;*/
			            //console.log(this.i);
			        //here we have to display the video			           
			            var videoTest = checkVideo(); //returns 0-no video, 1-video supported
			            //alert(videoTest);

			            if (videoTest == 0) {
			                //play flas
			                htmlContent = '<div style="width:520px; height:500px; text-align:center;">' +
			                                '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="510" height="490" id="videoPlayer"> <param name="movie" value="videoPlayer.swf" /><param name="flashvars" value="videoURL=videos/video' + this.i + '.flv" /> <param name="quality" value="high" /> <param name="bgcolor" value="#ffffff" /> <param name="allowScriptAccess" value="sameDomain" /> <param name="allowFullScreen" value="true" /> <!--[if !IE]>--> <object type="application/x-shockwave-flash" data="videoPlayer.swf" width="510" height="490"><param name="flashvars" value="videoURL=videos/video' + this.i + '.flv" /> <param name="quality" value="high" /> <param name="bgcolor" value="#ffffff" /> <param name="allowScriptAccess" value="sameDomain" /> <param name="allowFullScreen" value="true" /> <!--<![endif]--> <!--[if gte IE 6]>--> <p> Either scripts and active content are not permitted to run or Adobe Flash Player version 10.0.0 or greater is not installed. </p> <!--<![endif]--> <a href="http://www.adobe.com/go/getflashplayer"> <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash Player" /> </a> <!--[if !IE]>--> </object> <!--<![endif]--> </object>' +
			                              '</div>';			          }
			            else {
			                //play html5
			                htmlContent = '<div style="width:520px; height:500px; text-align:center;">' +
			                '<video id="videoPlayer" width="500" height="480" preload="auto" controls="controls">' +
			                    '<source src="videos/video' + this.i + '.mp4" type="video/mp4" codecs="avc1.42E01E, mp4a.40.2"/>' +
			                    '<source src="videos/video' + this.i + '.ogg" type="video/ogg" codecs="theora, vorbis"/>' +
			                '</video>' +
			                '</div>';
			            }


			            $.colorbox({ transition: 'none', speed: 100, title: adUnitNamesArray[this.i], initialWidth: 150, initialHeight: 330, html: htmlContent });

			            $("#panelContainer").trigger("pause"); //stop the carousel auto slideshow

			            //console.log("########## " + document.getElementById('videoPlayer'));
			            if (videoTest == 1)
			                document.getElementById('videoPlayer').play();
			        }

			        break;

			}

			image.style.zIndex = zIndex;
			
		}
       
		x += xstep;

}
}


function checkVideo() {
    if (document.createElement('video').canPlayType) {
        var vidTest = document.createElement("video");
        oggTest = vidTest.canPlayType('video/ogg; codecs="theora, vorbis"');
        if (!oggTest) {
            h264Test = vidTest.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
            if (!h264Test) {
                //alert("Sorry. No video support.");
                return 0;
            }
            else {
                if (h264Test == "probably") {
                    //alert("Yeah! Full support!");
                    return 1;
                }
                else {
                    //alert("Meh. Some support.");
                    return 0;
                }
            }
        }
        else {
            if (oggTest == "probably") {
                //alert("Yeah! Full support!");
                return 1;
            }
            else {
                //alert("Meh. Some support.");
                return 0;
            }
        }
    }
    else {
        //alert("Sorry. No video support.");
        return 0;
    }
}


/* Main function */

function refresh(onload)

{

	/* Cache document objects in global variables */

	imageflow_div = document.getElementById(conf_imageflow);

	img_div = document.getElementById(conf_images);

	scrollbar_div = document.getElementById(conf_scrollbar);

	slider_div = document.getElementById(conf_slider);

	caption_div = document.getElementById(conf_captions);



	/* Cache global variables, that only change on refresh */

	images_width = img_div.offsetWidth - 160;
	//console.log(images_width);

	images_top = imageflow_div.offsetTop;

	images_left = imageflow_div.offsetLeft + 100;

	max_conf_focus = conf_focus * xstep;

	size = images_width * 0.5;

	scrollbar_width = images_width * 0.6;

	conf_slider_width = conf_slider_width * 0.5;

	max_height = images_width * 0.51;



	/* Change imageflow div properties */

	imageflow_div.style.height = max_height + 'px';



	/* Change images div properties */

	img_div.style.height = images_width * 0.338 + 'px';



	/* Change captions div properties */

	caption_div.style.width = images_width + 'px';

	caption_div.style.marginTop = images_width * 0.03 + 'px';



	/* Change scrollbar div properties */

	scrollbar_div.style.marginTop = images_width * 0.02 + 'px';

	scrollbar_div.style.marginLeft = images_width * 0.2 + 'px';

	scrollbar_div.style.width = scrollbar_width + 'px';

	

	/* Set slider attributes */

	slider_div.onmousedown = function () { dragstart(this); };

	slider_div.style.cursor = conf_slider_cursor;



	/* Cache EVERYTHING! */

	max = img_div.childNodes.length;
	//console.log("Length: " + max);

	var i = 0;

	for (var index = 0; index < max; index++)

	{ 

		var image = img_div.childNodes.item(index);

		if (image.nodeType == 1)

		{

			array_images[i] = index;

			

			/* Set image onclick by adding i and x_pos as attributes! */

			image.onclick = function() { glideTo(this.x_pos, this.i); }

			image.x_pos = (-i * xstep);

			image.i = i;

			

			/* Add width and height as attributes ONLY once onload */

			if(onload == true)

			{

				image.w = image.getAttribute('w');

				image.h = image.getAttribute('h');

			}



			/* Check source image format. Get image height minus reflection height! */

			switch ((image.w + 1) > (image.h / (conf_reflection_p + 1))) 

			{

				/* Landscape format */

				case true:

					image.pc = 118;

					break;



				/* Portrait and square format */

				default:

					image.pc = 100;

					break;

			}



			/* Set ondblclick event */

			image.url = image.getAttribute('longdesc');

			//image.ondblclick = function() { /*document.location = this.url;*/ }



			/* Set image cursor type */

			image.style.cursor = conf_images_cursor;



			i++;

		}

	}

	max = array_images.length;



	/* Display images in current order */

	moveTo(current);

	glideTo(current, caption_id);

}



/* Show/hide element functions */

function show(id)

{

	var element = document.getElementById(id);

	element.style.visibility = 'visible';

}

function hide(id)

{

	var element = document.getElementById(id);

	element.style.visibility = 'hidden';

	element.style.display = 'none';

}
function init()
{
	if(document.getElementById(conf_imageflow))

	{

		refresh(true);
		glideTo(-430, 0);
		//glideTo(0, 0); //initially	
	}

}


