Date.prototype.time = function() {
    var t = this.getTime();
    return parseInt(t / 1000);
}
 //window.onload = spanCount();
 
  		
						
 
window.onload = function() {
	                   
                       /*  var deviceIphone = "iphone";
                         var deviceplam = "plam";
                         var deviceS60 = "series60";
                         var deviceSymbian = "symbian";
                        // var engineWebKit = "webkit";
                         var deviceAndroid = "android";
                          var deviceWinMob = "windows ce";
                         var deviceBB = "blackberry";

                    //Initialize our user agent string to lower case.
					var uagent = navigator.userAgent.toLowerCase();
					
					 if(uagent.match(deviceIphone) == 'iphone' || uagent.match(deviceplam) == 'plam'   || uagent.match(deviceS60) == 'series60' || uagent.match(deviceSymbian) == 'symbian' || uagent.match(deviceAndroid) == 'android' || uagent.match(deviceWinMob) == 'windows ce' || uagent.match(deviceBB) == 'blackberry' ){					 
						
						var url = window.location.href;
						var urlparts = url.split('?');
						 if(urlparts[1] == 'moFull'){						 
							window.location('http://www.claycountyfair.org/main-stage.htm'); 			
						 }
						 else{
							location.replace('http://claycountyfair.org/mobile/');	
						 }
					 }
						 else{
							window.location('http://www.claycountyfair.org/main-stage.htm'); 
					 }
*/
			
		 
		 
		 	$('#footer').hide();
	   setTimeout(function() {
         
		 
		var $content = $('#content');
        var $contentSub = $content.find('#content-sub');
        var $contentMain = $content.find('#content-main');
        
		
        var offsetHeight = 90;
        var heightSub = ($contentSub.height()+50);
        var heightMain = $contentMain.height() + parseInt($contentMain.css('top')) - offsetHeight;
		heightMain =heightMain+50;
		/*alert(heightSub);
		alert(heightMain);*/
		
		var h = heightMain > heightSub? heightMain : heightSub;
		 
		$content.height(h);	
		$('#footer').show();	

    }, 3000);
  
}
function spanCount(){
	
  //alert("asdasd");	
   // setTimeout(function() {
//         
//		 
//		var $content = $('#content');
//        var $contentSub = $content.find('#content-sub');
//        var $contentMain = $content.find('#content-main');
//        var offsetHeight = 90;
//        var heightSub = $contentSub.height();
//        var heightMain = $contentMain.height() + parseInt($contentMain.css('top')) - offsetHeight;
//		
//		$content.height(heightMain > heightSub? heightMain : heightSub);	
//		$('#footer').show();	
//
//    }, 4000);
}
$(document).ready(function() {
    var delay = 8000;
    var $imgs = $('#content-main .main-image > img');
    var item = 0;
    var length = $imgs.length;
    setInterval(function() {
        $imgs.hide();
        item++;
        item = item > length-1 ? 0 : item;
        $imgs.eq(item).fadeIn('fast');
    }, delay);
});

$(document).ready(function() {
    $('.photo-list li div').mouseover(function() {
        var $this =  $(this).parent('li').find('img');
        var src = $this.attr('src');
        var index = src.lastIndexOf('.');
        var filename = src.substring(0,index);
        var extend = src.substring(index);
        $this.attr('src', filename + '_hover' + extend);
        return false;
    }).mouseout(function() {
        var $this = $(this).parent('li').find('img');
        $this.attr('src', $this.attr('src').replace('_hover', ''));
        return false
    });
});

$(document).ready(function() {
    var $days = $('#countdown .days .time');
    var $hours = $('#countdown .hours .time');
    var $minutes = $('#countdown .minutes .time');
    var $seconds = $('#countdown .seconds .time');
    var offsetCache = {'days':0, 'hours':0, 'minutes':0, 'seconds':0};
    countdown();

    function countdown() {
        var deadline = new Date('2012/3/29 12:0:0').time();
        var now = new Date().time();
        //timezong
        var timezone = (new Date().getTimezoneOffset()) / 60 * -1;
        var est = -5;
        var timezoneOffset = timezone - est;
        deadline += timezoneOffset * 60 * 60; //EST -5

        var timeOffset = deadline - now;
        var offset = {
            'days': 0,
            'hours': 0,
            'minutes': 0,
            'seconds': 0
        }
        if (timeOffset >= 0) {
            offset.days = parseInt(timeOffset / (60*60*24)) //days
            offset.days = offset.days > 100? offset.days : offset.days < 10 ? '00'+offset.days : '0'+offset.days;
            timeOffset = timeOffset % (60*60*24);

            offset.hours =  parseInt(timeOffset  / (60*60));  //hours
            offset.hours = offset.hours < 10 ? '0'+offset.hours : offset.hours;
            timeOffset = timeOffset % (60*60);

            offset.minutes =  parseInt(timeOffset / 60) ;      //minutes
            offset.minutes = offset.minutes < 10 ? '0'+offset.minutes : offset.minutes;

            offset.seconds = timeOffset % 60;                    //seconds
            offset.seconds = offset.seconds < 10 ? '0'+offset.seconds : offset.seconds;

            var i;
            var $tmp;
            var current = '';
            var digitValue;
            var newPosition;

            //days
            if (!offsetCache.days || offset.days != offsetCache.days) {
                var dayDigit = offset.days.toString().length;
                for (i=0; i<dayDigit; i++) {
                    digitValue = offset.days.toString().substring(i,i+1);
                    current += '<span style="background-position:' + '-' + parseInt(digitValue,10)*20+'px 0' + ';">' + digitValue + '</span>';
                }
                $days.get(0).innerHTML = current;
                current = '';
            }
            

            //hours
            if (!offsetCache.hours || offset.hours != offsetCache.hours) {
                var hourDigit = offset.hours.toString().length;
                for (i=0; i<hourDigit; i++) {
                    digitValue = offset.hours.toString().substring(i,i+1);
                    current += '<span style="background-position:' + '-' + parseInt(digitValue,10)*20+'px 0' + ';">' + digitValue + '</span>';
                }
                $hours.get(0).innerHTML = current;
                current = '';
            }
            

            //minutes
            if (!offsetCache.minutes || offset.minutes != offsetCache.minutes) {
                var minuteDigit = offset.minutes.toString().length;
                for (i=0; i<minuteDigit; i++) {
                    digitValue = offset.minutes.toString().substring(i,i+1);
                    current += '<span style="background-position:' + '-' + parseInt(digitValue,10)*20+'px 0' + ';">' + digitValue + '</span>';
                }
                $minutes.get(0).innerHTML = current;
                current = '';
            }
            

            //seconds
            if (!offsetCache.seconds || offset.seconds != offsetCache.seconds) {
                var secondDigit = offset.seconds.toString().length;
                for (i=0; i<secondDigit; i++) {
                    digitValue = offset.seconds.toString().substring(i,i+1);
                    current += '<span style="background-position:' + '-' + parseInt(digitValue,10)*20+'px 0' + ';">' + digitValue + '</span>';
                }
                $seconds.get(0).innerHTML = current;
                current = '';
            }

            offsetCache = offset;
        }

        setTimeout(countdown, 1000);
    }
});

//initialize: .family-nav #content
$(document).ready(function() {
 						   
	 var height = $('#content-main').height();
   // alert(height);	
   if(height > 600)
     $('#footer').hide();	
   
 	
    $('#content-sub .family-nav li').each(function() {
        $(this).removeAttr('style').removeAttr('onmouseover').removeAttr('onmouseout').removeAttr('class').css('cursor','pointer');
    })
    .find('a').each(function() {
        var $this = $(this);
        $this.removeAttr('onclick');
        if ($.trim($this.attr('href')) == '#') {
           $this.click(function() {return false;});
        }
    });

    //var html = $('#content-sub .family-nav ul[id^=nav_] > li > ul[id^=navsub_]').html();
    $('#content-sub .family-nav ul[id^=nav_] > li > ul[id^=navsub_]').wrap('<div class="sub-nav level2" ></div>').after('<div class="bottom" ></div>')
    .find('ul[id^=navsub_]').wrap('<div class="sub-nav level3" ></div>').before('<div class="top" ></div>');
		 
});

 

//sub-nav
$(document).ready(function() {
   var $subnav = $('#content-sub .family-nav .sub-nav');
    $subnav.parent('li').hover(function() {
        var $this = $(this);
        
        switch (true) {
            case $this.children('.sub-nav').hasClass('level3'):
                $this.css({
                    'text-align':'right'
                }).children('a').css({
                    'color':'#6a0c0c',
                    'text-decoration':'none'
                });
                break;
            case $this.children('.sub-nav').hasClass('level2'):
                $this.css({
                    'text-align':'right',
                    'background-position':'-9999px -9999px'
                }).children('a').css({
                    'color':'#ffcc66',
                    'text-decoration':'none'
                })
                break;
        }
        
        $this.children('.sub-nav').show();
    }, function() {
        var $this = $(this);
        switch (true) {
            case $this.children('.sub-nav').hasClass('level3'):
                $this.css({
                    'text-align':'left'
                }).children('a').css({
                    'color':'#1032a9'
                });
                break;
            case $this.children('.sub-nav').hasClass('level2'):
                $this.css({
                    'text-align':'left',
                    'background-position':'0 50%'
                }).children('a').css({
                    'color':'#fff'
                });
                break;
        }
        
        $this.children('.sub-nav').hide();
    });
});
