var TimerIDArray = new Array();
var TimerIDCount = 0;

function clearAlts () {
	$(".slideShowWrapper img[alt='undentified ']").attr('alt', "");
}

$(function() {
	clearAlts();
	/* Firefox scroll fix */
	$('html').css('overflow-y', 'scroll');

	/* IE 5.5+ Pngfix */
	if ($.browser.msie && $.browser.version >= 5.5 && $.browser.version <= 6.0) {
		$(document).pngFix();
	}

	/* Textarea vertical resize */
	$('textarea').css('resize', 'vertical');

	/* Form elements focus highlight disable (Chrome, Safari) */
	$('input, select, textarea').focus(function() {
		$(this).css('outline', 0);
	});


	$('img[alt="undentified"]').each(function() {
		$(this).attr('alt', '');
	});

	/* Flow player setup */
	var counter = 0;
	$('object').each(function() {
		if ($('body').attr('id') != 'home') {
			$(this).hide();
			var src		= $(this).attr('rel');
			var width	= $(this).attr('width');
			var height	= $(this).attr('height');
			
			if ($(this).children('param[name="play"]').length) {
				var autoplay = $(this).children('param[name="play"]').attr('value');
			} else {
				var autoplay = false;
			}

			if ($(this).children('param[name="loop"]').length) {
				var loopq = $(this).children('param[name="loop"]').attr('value');
			} else {
				var loopq = false;
			}

			if (autoplay == false) {
				autoplay = true;
			} else {
				autoplay = false;
			}
			if ($(this).parent('a').attr('class') != 'galleryPlayer') {
				autoplay = false;
			}
			counter++;
			$(this).after('<a id="player' + counter + '" style="width: ' + width + 'px; height:' + height + 'px;display:block" href="' + src + '"></a>');
			$(this).remove();
			
			//$("a#player" + counter).append('<img src="http://www.pmtv.com/assets/images/projects/press_conferences.jpg" /img');
			if (loopq == false) {
				flowplayer("player" + counter, "/assets/templates/pmtv/flowplayer/flowplayer-3.1.5.swf", { 
					clip: {autoPlay: autoplay, autoBuffering: true, onBeforeFinish: function() {return false;}
					},
					plugins: {
						controls: {fullscreen: false}
					}
				});
			} else {
				flowplayer("player" + counter, "/assets/templates/pmtv/flowplayer/flowplayer-3.1.5.swf", { 
					clip: {autoPlay: autoplay, autoBuffering: true},
					plugins: {
						controls: {fullscreen: false}
					}
				});
			}
		}
	});

	if ($('.slideShowWrapper').length) {
		/* Getting timeout */
		if ($('.slideShowWrapper').length && parseInt($('.slideShowWrapper').attr('class').substr(23,1)) == $('.slideShowWrapper').attr('class').substr(23,1)) {
			timeout = parseInt($('.slideShowWrapper').attr('class').substr(23,1)) * 1000;
		} else {
			timeout = 3000;
		}
	
		/* Getting visibility of slides titles */
		if ($('.slideShowWrapper').length && parseInt($('.slideShowWrapper').attr('class').substr(35,1)) == $('.slideShowWrapper').attr('class').substr(35,1)) {
			showTitle = parseInt($('.slideShowWrapper').attr('class').substr(35,1));
		} else {
			showTitle = 1;
		}
	
		/* Init */
		$('.slideShowWrap img').addClass('phidden');
		var count = $('.slideShowWrap img').length;
		var rel = $('.slideShowWrap img:first').attr('rel');
		$('.slideShowWrap img:first').attr('src', rel).removeAttr('rel');
		$('.slideShowWrap img:first').removeClass('phidden').addClass('showen').attr('src', rel).removeAttr('rel').show();
		if (count > 1) {
			rel = $('.slideShowWrap img:first').next().attr('rel');
			$('.slideShowWrap img:first').next().attr('src', rel).removeAttr('rel');
		}
	
		/*creating the container for slides navigation list*/
		$('.slideShowWrapper').append('<div id="preview_b"></div><div id="preview"></div>');
	
		//generating slides navigation list
		var i = 0;
		$('.slideShowWrap img').each(function() {
			if (++i <= 5) {
				$('div#preview').append('<p>' + i + '</p>');
			} else {
				$('div#preview').append('<p class="phidden">' + i + '</p>');
			}
		});
		$('.slideShowWrapper').append('<div id="imageTitle_b"></div>');
		$('.slideShowWrapper').append('<p id="imageTitle"></p>');
		$('.slideShowWrapper').append('<a id="prev" href="#"></a>');
		$('.slideShowWrapper').append('<a id="next" href="#"></a>');
		$('a.#next, a#prev').removeAttr('href');
	
		//
		if (showTitle == 1) {
			$('#imageTitle').text(getCurentSlideTitle());
		}
		//adding to first preview border (current slide)
		$('#preview p:first').addClass('border');
	
		//getting the cout of all slideshow slides
		count = $('#preview p').length;
		if (count > 1) {
			//each timeout the current image will be replaced by next one
			setTimeout("slideIt(" + count + ", false, 0)", timeout);
	
			$("#preview p").click(function(){
				current = $("#preview p").index($(this));
				currentSlide(current, count);
			});
	
			$("#next").click(function(){
				nextSlide(count);
			});
	
			$("#prev").click(function(){
				prevSlide(count);
			});
		} else {
			if (showTitle == 1) {
				$('#imageTitle').text($(".slideShowWrap img:first").attr('alt'));
			}
		}
		if (showTitle == 0) {
			$('#imageTitle_b').hide();
		}
	}
});

// return the alt of current slide
function getCurentSlideTitle() {
	return $(".slideShowWrap img.showen").attr('alt');
}

//add border to the current preview image, hide all images in slideshow and show the current image 
function toggleSlide (current) {
	$(".slideShowWrap img").fadeOut();
	
	if ($(".slideShowWrap img").eq(current).attr('rel')) {
		rel = $(".slideShowWrap img").eq(current).attr('rel');
		$(".slideShowWrap img").eq(current).attr('src', rel).removeAttr('rel');
	}
	count = $('.slideShowWrap img').length;
	if (($(".slideShowWrap img").eq(current) != (count - 1)) && $(".slideShowWrap img").eq(current).next().attr('rel')) {
		rel = $(".slideShowWrap img").eq(current).next().attr('rel');
		$(".slideShowWrap img").eq(current).next().attr('src', rel).removeAttr('rel');
	}
	$(".slideShowWrap img").eq(current).fadeIn();
	$("#preview p").removeClass('border');
	$("#preview p").eq(current).addClass('border');
	slowSlide(current);
}

function slowSlide (current) {
	$(".slideShowWrap img").removeClass('showen').addClass('hidden');
	if ($(".slideShowWrap img").eq(current).attr('rel')) {
		rel = $(".slideShowWrap img").eq(current).attr('rel');
		$(".slideShowWrap img").eq(current).attr('src', rel).removeAttr('rel');
	}
	count = $('.slideShowWrap img').length;
	if (($(".slideShowWrap img").eq(current) != (count - 1)) && $(".slideShowWrap img").eq(current).next().attr('rel')) {
		rel = $(".slideShowWrap img").eq(current).next().attr('rel');
		$(".slideShowWrap img").eq(current).next().attr('src', rel).removeAttr('rel');
	}
	$(".slideShowWrap img").eq(current).removeClass('hidden').addClass('showen');
}

//show the group of preview images (ex: preview images from 1 to 8...)
function tooglePreviews (showGroup, totalGroups) {
	if(showGroup == totalGroups) {
		$("#preview p").removeClass('pshowen').addClass('phidden');
		$("#preview p").eq(count - 5).removeClass('phidden').addClass('pshowen');
		$("#preview p").eq(count - 4).removeClass('phidden').addClass('pshowen');
		$("#preview p").eq(count - 3).removeClass('phidden').addClass('pshowen');
		$("#preview p").eq(count - 2).removeClass('phidden').addClass('pshowen');
		$("#preview p").eq(count - 1).removeClass('phidden').addClass('pshowen');
	} else {
		var from = (showGroup - 1)*5;
		$("#preview p").removeClass('pshowen').addClass('phidden');
		$("#preview p").eq(from).removeClass('phidden').addClass('pshowen');
		$("#preview p").eq(from + 1).removeClass('phidden').addClass('pshowen');
		$("#preview p").eq(from + 2).removeClass('phidden').addClass('pshowen');
		$("#preview p").eq(from + 3).removeClass('phidden').addClass('pshowen');
		$("#preview p").eq(from + 4).removeClass('phidden').addClass('pshowen');
	}
}

//display the current visible group of preview images (is using by slideIt, nextSlide, currentSlide)
function previewGroupsVisibility(count) {
	var totalGroups;
	if (count % 5 == 0) {
		totalGroups =  count / 5;
	} else {
		totalGroups = (count - (count % 5)) / 5 + 1;
	}

	var showGroup;
	showen = $(".slideShowWrap img").index($(".showen"));
	if (showen + 1 != count) {
		showGroup =  Math.floor((showen + 1) / 5) + 1;
	} else {
		showGroup =  1;
	}
	tooglePreviews(showGroup, totalGroups);
}

//display the current  group of preview images (is using by slprevSlide)
function previewGroupsVisibilityPrev(count) {
	var totalGroups;
	if (count % 5 == 0) {
		totalGroups =  count / 5;
	} else {
		totalGroups = (count - (count % 5)) / 5 + 1;
	}
	var showGroup;
	showen = $(".slideShowWrap img").index($(".showen"));
	if (showen != 0) {
		showGroup =  Math.floor((showen) / 5) + 1;
	} else {
		showGroup =  1;
	}

	tooglePreviews (showGroup, totalGroups);
}

//slides permanently the images
function slideIt(count) {
		previewGroupsVisibility(count);
		var show = count - ($(".slideShowWrap img").index($(".showen")) % 5);
		if (count == $(".slideShowWrap img").index($(".showen")) + 1) {
			current = 0;
		} else {
			current = $(".slideShowWrap img").index($(".showen")) + 1;
		}
		toggleSlide(current);
		for (i = 0 ; i < TimerIDCount ; i++) {
			window.clearTimeout(TimerIDArray[i]);
		}
		if (showTitle == 1) {
			$('#imageTitle').text(getCurentSlideTitle());
		}
		TimerIDArray[TimerIDCount++] = setTimeout("slideIt(" + count + ")", timeout);
}

//changing to the next slide
function nextSlide(count) {
		previewGroupsVisibility(count);
		for (i = 0 ; i < TimerIDCount ; i++) {
			window.clearTimeout(TimerIDArray[i]);
		}
		if (count == $(".slideShowWrap img").index($(".showen")) + 1) {
			current = 0;
		} else {
			current = $(".slideShowWrap img").index($(".showen")) + 1;
		}
		toggleSlide(current);

		if (showTitle == 1) {
			$('#imageTitle').text(getCurentSlideTitle());
		}

		TimerIDArray[TimerIDCount++] = setTimeout("slideIt(" + count + ")", timeout);		
}

//changing to the prev slide
function prevSlide(count) {

		for (i = 0 ; i < TimerIDCount ; i++) {
			window.clearTimeout(TimerIDArray[i]);
		}
		if ($(".slideShowWrap img").index($(".showen")) == 0) {
			current = count - 1;
		} else {
			current = $(".slideShowWrap img").index($(".showen")) - 1;
		}

		toggleSlide(current);

		previewGroupsVisibilityPrev(count);

		if (showTitle == 1) {
			$('#imageTitle').text(getCurentSlideTitle());
		}

		TimerIDArray[TimerIDCount++] = setTimeout("slideIt(" + count + ")", timeout);	
}

//changing to the current slide (when press on the previe image)
function currentSlide (current, count) {
		if (current != $(".slideShowWrap img").index($(".showen"))) {
			toggleSlide(current);
			if (showTitle == 1) {
				$('#imageTitle').text(getCurentSlideTitle());
			}
		}
		for (i = 0 ; i < TimerIDCount ; i++) {
			window.clearTimeout(TimerIDArray[i]);
		}

		TimerIDArray[TimerIDCount++] = setTimeout("slideIt(" + count + ")", timeout);
}
/* </Slideshow> */
/* SMS Status */
$(function() {
	if ($('.smsSendStatus').length) {
		var smsCurrentUsed = parseInt($('.SmsStatus .smsUsed').text());
		var smsCurrentTotal = parseInt($('.SmsStatus .smsTotal').text());
		var smsSendStatus = $('.SmsStatus .smsError').text();
		$('.smsCurrentUsed').text(smsCurrentUsed);
		$('.smsCurrentTotal').text(smsCurrentTotal);
		$('.smsSendStatus').text(smsSendStatus);
	}
});