// JavaScript Document
//------------------------------------------------
var footer_current = 0;
var footer_link_count = 3;
var footer_timer;

$(document).ready(function() {
	footer_timer = setTimeout("footer_link_next()",5000);
	$('a#site-map-link').click(function() {
		$('#site-map').toggle();
	});
	if (document.getElementById('datepicker')) {
		$( "#datepicker" ).datepicker({
			changeMonth: true,
			changeYear: true
		});
	}
});

//--------------------------------------------------

function goTop(a, t) {
	a = a || 0.1;
	t = t || 16;

	var x1 = 0;
	var y1 = 0;
	var x2 = 0;
	var y2 = 0;
	var x3 = 0;
	var y3 = 0;

	if (document.documentElement) {
		x1 = document.documentElement.scrollLeft || 0;
		y1 = document.documentElement.scrollTop || 0;
	}
	if (document.body) {
		x2 = document.body.scrollLeft || 0;
		y2 = document.body.scrollTop || 0;
	}
	var x3 = window.scrollX || 0;
	var y3 = window.scrollY || 0;

	var x = Math.max(x1, Math.max(x2, x3));
	var y = Math.max(y1, Math.max(y2, y3));

	var speed = 1 + a;
	window.scrollTo(Math.floor(x / speed), Math.floor(y / speed));
	if(x > 0 || y > 0) {
		var f = "goTop(" + a + ", " + t + ")";
		window.setTimeout(f, t);
	}
}

//------------------------------------------------
//footer code

function footer_link_next() {
	//advance link in footer
	$('div#footer_content-' + footer_current).hide();
	if (footer_current < footer_link_count -1) {
		footer_current++;
		//alert('next footer');
	} else {
		footer_current = 0;		
		//alert('reset footer');
	}
	$('div#footer_content-' + footer_current).show();
	footer_timer = setTimeout("footer_link_next()",5000);
}

//-------------------------------------------------------
//code  for piwigo gallery & SlideShowPro  integration to set exit picture
  function thisMovie(movieName) {
         if (navigator.appName.indexOf("Microsoft") != -1) {
             tempName = movieName + '_ie';
             return document[tempName];
         } else {
                         return document[movieName];
         }
     }
     
  function sendToJavaScript(value) {
      //need to set this for server
  if (value != '') {
      current_url = window.location.href;
      str_pos = current_url.search('index.php');
      if (str_pos > 0) {
            window.location.reload(true);
      } else {
        str_pos = current_url.search('picture.php');
        temp_url_base = current_url.substr(0,str_pos + 11);
        temp_url_base2 = current_url.substr(str_pos+12);
        str_pos2 = temp_url_base2.search('/');
        if (str_pos > 0) { 
            str_len = current_url.length;
            str_pos = temp_url_base2.search('#pic');
            if (str_pos == -1) {
                temp_url_anchor = '#pic';
            } else {
                temp_url_anchor = '';
            }
            new_url  = temp_url_base +  '/' + value + temp_url_base2.substr(str_pos2)  + temp_url_anchor;
            //alert(new_url);
            window.location.href = new_url;
        }
      }
    }
  }

function confirm_upload_cat (cat) {
        var box = document.getElementById('upload_cat');
        var val = box.value;
        for (i=0; i < box.length;  i++) {
                //alert(box.children[i].label);
                if (val == box.children[i].value) {
                        //alert(box.children[i].label);
                      cat = box.children[i].label;
                }
        }
        var text = 'Please confirm the Upload Category as: \n\n' + cat.toUpperCase() + '\n\nOK will proceed with upload, cancel returns to form.';
    if (confirm(text)) {
        return true;
    } else {
        return false;
    }
}

function submit_upload() {
	//function called after  submit button pressed on upload page to clear any errors (id=upload_error) and show info box
	//alert('upload check');
	if (document.getElementById('upload_error')) {
		document.getElementById('upload_error').style.display = "none";
	}
	document.getElementById('upload_info').innerHTML = "Your upload is being checked for valid filename, size and format and if OK, your upload will begin automatically.<br />If there is a problem, you will be notified with an error message.  If you continue to have problems, please contact us.";
	document.getElementById('upload_info').style.display = "block";
}

function clear_datepicker() {
	document.getElementById('datepicker').value = '';
}
//-------------------------------------------------------------------------------------------------------
//combine image_box.js and kill-ie6.js to reduce requests
// JavaScript Document
var img_current = 0;				//current image number, starting at 0
//var img_array = new Array();
var img_playing = true;				//playing (true) or paused (false)
var img_timer_interval = 5000;		//image interval (msec)
//var img_timer_interval = 0;		//image interval (msec)
var img_timer;						//image timer
var img_loop = true;					//loop around images
//var img_stop = 200;					//max count to display (not used, max_img_number is used)
var img_count =0;					//count of next images 

//array set in home.php
$(document).ready(function() {
	//alert($('li.test div').hasClass('submenu')==true);
	//$('li.test div').removeClass('submenu');
	$('a#img_btn_prev').click(function(){
		//alert('prev btn');
		img_current = img_change('prev',img_current,img_max_number);
		if (img_playing == true) {
			clearTimeout(img_timer);
			img_timer = setTimeout("img_next()",img_timer_interval);
		}
    });	
	$('a#img_btn_next').click(function(){
		//alert('next btn');
		img_current = img_change('next',img_current,img_max_number);
		if (img_playing == true) {
			clearTimeout(img_timer);
			img_timer = setTimeout("img_next()",img_timer_interval);
		}
    });	
	$('a.img_btn_pp').click(function(){
		//alert('pp btn');
		switch (img_playing) {
			case true:
				img_playing=false;
				clearTimeout(img_timer);
				break;
			case false:
				img_playing=true;											//switch mode to playing (true)
				img_timer = setTimeout("img_next()",img_timer_interval);	//set timer for current image
				break;
		}
		img_btn_update(img_current, img_max_number)
    });	
	//put trigger in home_image_start function, hide  play button
	//call img_btn_update with 'loading' until trigger
	img_btn_update(img_current, '');
 });

function home_image_start() {
	if (img_playing == true) { img_timer = setTimeout("img_next()",img_timer_interval); }
	img_btn_update(img_current, img_max_number);
}

 
function img_next() {
	if (img_current < img_max_number - 1) {
		img_current = img_change('next',img_current,img_max_number);
		img_count++;
	}
	if ((img_loop == true) && (img_current == img_max_number -1)) {
		img_current = -1;
	}

	if ((img_current < img_max_number - 1) && (img_count < img_max_number)) {				//change from  img_count < img_stop
		img_timer = setTimeout("img_next()",img_timer_interval);
	} else {
		img_playing=false;
		clearTimeout(img_timer);
		img_btn_update(img_current, img_max_number)
		img_count = 0;									//reset image count
	}
}
 
function img_change(cmd, img_num, img_max) {
	//change image function - cmd is 'prev' or 'next',  img_num is image number, img_max is number of images
	switch (cmd) {
		case 'prev':
			if (img_num > 0) {
				img_num--;
			}
			break;
		case 'next':
			if (img_num < img_max - 1) {
				img_num++;
			} 
			img_preload(img_num, img_max);				//only need to preload next image
			break;
	}
	if ((img_num % 2) == 0) {
		//even is img_a
		//alert(img_array[img_num]);
		$('img#img_a').attr('src', img_array[img_num]);
		$('img#img_a').fadeIn('slow');
		$('img#img_b').fadeOut('slow');
	} else {
		//odd is img_b
		$('img#img_b').attr('src', img_array[img_num]);
		$('img#img_b').fadeIn('slow');
		$('img#img_a').fadeOut('slow');
	}
	img_btn_update(img_num, img_max);
	return img_num;
}
function img_preload(img_num,img_max) {
	//preload next image into dummy image object to preload for next img rotation
	
	var img_temp = new Image();
	if (img_num < img_max - 1) {
		//alert('preload');
		img_num++;
		img_temp.src = img_array[img_num];
		//$('div#preload_status').html('Image Preload: ' + (img_num + 1));
	}
}
function img_btn_update(img_num,img_max) {
	//update tool tip on play/pause button
	var temp;
	
	if (img_playing) {
		$('div#btn_play').hide();
		$('div#btn_pause').show();
		temp = " - playing";
	} else {
		$('div#btn_play').show();
		$('div#btn_pause').hide();
		temp = " - paused";
	}
	$('a.img_btn_pp').attr('title', (img_num +1) + ' of ' + img_max + temp);
}
//-------------------------------------------------------------------------------------------------------
//Kill ie6 by jonk - http://kill-ie6.pirateboy.net
if (document.all) {
	var ua = window.navigator.userAgent;
	var msie = ua.indexOf ( "MSIE " );
	var ieVer = parseInt ( ua.substring ( msie+5, ua.indexOf ( ".", msie ) ) );
	if (ieVer < 7) {
		if (typeof(killie6text)=='undefined') {
			killie6text = '<span class="kill-ie6_bold">Warning!</span>&nbsp;You\'re using Internet Explorer 6, this site may not display properly and IE6 is a <span class="kill-ie6_bold">security risk!</span> <a href="http://www.microsoft.com/ie" target="_blank">Upgrade</a> or try another browser: <a href="http://www.mozilla.com/firefox/" target="_blank">Firefox</a>, <a href="http://www.google.com/chrome/" target="_blank">Chrome</a> or <a href="http://www.apple.com/safari/" target="_blank">Safari</a>.';
		}
		document.write('<style type="text/css">');
		document.write('#kill-ie6_oldBrowserWarning * {');
			document.write('font-family:verdana,arial,sans-serif;font-size:10px;color:#000;border:0px;margin:0px;padding:0px;position:static;text-transform:none;');
		document.write('}');
		document.write('#kill-ie6_oldBrowserWarning {');
			document.write('display:block;width:100%;height:25px;line-height:25px;background-color:#F47777;border-bottom:1px solid #000;overflow:hidden;');
		document.write('}');
		document.write('#kill-ie6_oldBrowserWarning a {');
			document.write('text-decoration:underline;color:#0000ee;');
		document.write('}');
		document.write('#kill-ie6_oldBrowserWarning a#kill-ie6_close {');
			document.write('color:#000;text-decoration:none;font-weight:bold;float:right;margin-right:5px;height:23px;line-height:23px;');
		document.write('}');
		document.write('#kill-ie6_oldBrowserWarning #kill-ie6_text {');
			document.write('margin:0px 5px 0px 5px;text-align:left;');
		document.write('}');
		document.write('#kill-ie6_oldBrowserWarning #kill-ie6_text .kill-ie6_bold {');
			document.write('font-weight:bold;');
		document.write('}');
		document.write('</style>');
		document.write('<div id="kill-ie6_oldBrowserWarning">');
			document.write('<a id="kill-ie6_close" href="javascript:void(0);" onclick="javascript:document.getElementById(\'kill-ie6_oldBrowserWarning\').style.display=\'none\';">X</a>');
			document.write('<div id="kill-ie6_text">' + killie6text + '</div>');
		document.write('</div>');
	} 
}
