jq = $;

tErasePassword = '';

function fOnLoad() {

    fStartRequestChain();

    fRequestWeather();

    gsEditing = '';

    fCR();

    //fNotice('This is just a sample notice! Here\'s some text to make it longer');

}

function fStartRequestChain() {
    fSlides();
}

function fSlides(sSlides) {

    if (sSlides) {
	goSlides = sSlides;

	fCalendar();

	return;
    }

    jq.get('slides.php', function(sSlides) { fSlides(sSlides); }, 'json');

}

function fCalendar(sCalendar) {

    if (sCalendar) {
	goCalendar = sCalendar;
	
	fVendors();

	return;
    }

    jq.get('calendar.php', function(sCalendar) { fCalendar(sCalendar); }, 'json');

}

function fVendors(sVendors) {

    if (sVendors) {
	goVendors = sVendors;
	
	fPages();

	return;
    }

    jq.get('vendors.php', function(sVendors) { fVendors(sVendors); }, 'json');

}

function fPGPhotos(sPGPhotos) {

    if (sPGPhotos) {
	goPGPhotos = sPGPhotos;
	
	fPages();

	return;
    }

    jq.get('pg_photos.php', function(sPGPhotos) { fPGPhotos(sPGPhotos); }, 'json');

}

function fPages(sPages) {

    if (sPages) {

	if (sPages == 'home') {
	    fPreloadPG();
	}

	// set page structures/templates to a global object
	goPages = sPages;


	// fill in templates

	//vendors
        var sVendors = '';

	for (var sId in goVendors) {
	    
	    var oVendor = goVendors[sId];

	    /*
	    sVendors += goPages['vendors']
		.replace(/VENDOR_ID/g, oVendor['id'])
		.replace("VENDOR_NAME", oVendor['name'])
		.replace("VENDOR_DESC", oVendor['description'])
  		.replace("VENDOR_CONTACT", fObjToTable(oVendor['contact']))
 		.replace("VENDOR_PHOTOS", fBuildGallery(oVendor));
	    */
	    sVar = goPages['vendors'];
	    sVar = sVar.replace(/VENDOR_ID/g, oVendor['id']);
	    sVar = sVar.replace("VENDOR_NAME", oVendor['name']);
	    sVar = sVar.replace("VENDOR_DESC", oVendor['description']);
	    sVar = sVar.replace("VENDOR_CONTACT", fObjToTable(oVendor['contact']));
	    sVar = sVar.replace("VENDOR_PHOTOS", fBuildGallery(oVendor));

	    sVendors += sVar;

	}

	goPages['vendors'] = sVendors;


	// calendar
        var sCalendar = goPages['calendar'];
	    
	for (var sMonth in goCalendar) {
	    
	    sCalendar = sCalendar.replace("<!--CAL_"+sMonth.substr(0,3).toUpperCase()+'-->', fObjToTable(goCalendar[sMonth]));

	}

	goPages['calendar'] = sCalendar;

	fChangePage();
	return;
    }

    jq.get('pages.php', function(sPages) { fPages(sPages); }, 'json');

}

function fChangePage(sPage) {

    if (!sPage) {
	var sPage = (window.location.hash || 'home').replace('#','');
    }

    else {
	window.location.hash = sPage;
    }

    var sHtml = goPages[sPage] || "Sorry, the page you have requested does not exist.";

    jq('#content').html(sHtml);
    
    if (sPage == 'home') {
	
	var i = 1;
	jq.each(goSlides, function(title, text) {
	    var jSpan = jq('<span></span>').addClass('slide_box').appendTo('#slide_photos');
	    jq("<img/>").attr("src", 'images/slide_photos/slide_'+i+'.jpg').addClass('slide_photo').appendTo(jSpan);
	    var jDiv = jq('<div></div>').addClass('overlay').appendTo(jSpan);
	    jq('<span></span>').addClass('widget_title').text(title).appendTo(jDiv);
	    jq('<p></p>').text(text).appendTo(jDiv);
	    i++;
	    });

	setTimeout("fAdvanceSlideShow()",4000);
    }

    fCR('#content .cr, #content .title');

}

function fObjToTable(obj) {

    var sTable = '';

    for (var field in obj) {
        sTable += '<tr><td class="'+field+'"><b>'+field+'</b></td><td style="text-align:left">'+obj[field].replace('@', '<code>@</code>')+'</td></tr>';
    }

    return sTable
}

function fBuildGallery(obj) {

    var sPhotos ='';
    var i=1;

    for (i=1;i<=obj['photos'];i++) {
	sPhotos += '<img class="vendor_thumb" src="images/'+obj['id']+'_'+i+'_thumb.jpg"></img>';
    }

    return sPhotos;

}

function fCR(sSelector) {

    if (!sSelector) { var sSelector = '.cr'; }

    Cufon.replace(sSelector, {hover: true, hoverables: { li: true }});
}

function fNotice(sNotice) {
    if (!sNotice) {
	jq.get('notice.php', function(sNotice) { fNotice(sNotice); }, 'html');
	return;
    }

    jq('#notice').html(jq('#notice').html()+' '+sNotice);

    fCR('#notice');

}

function fGetSatDate() {

    var d = new Date();
    var sDay = d.getDay();
    var sDiff = 6 - sDay;
    
    var sDate = d.getDate();
    var sMonth = d.getMonth();
    var sYear = d.getFullYear();
    
    var sNewDate = new Date(sYear, sMonth, sDate+sDiff);
    
    var sDate = sNewDate.getDate();
    var sMonth = sNewDate.getMonth()+1;
    var sYear = sNewDate.getFullYear();
    
    if (sMonth < 10) { sMonth = '0'+sMonth; }

    return sYear+'-'+sMonth+'-'+sDate;  
} 

function fRequestWeather() {

    var sSatDate = fGetSatDate();

    jq.get('weather.php?date='+sSatDate,function(sResponse) { fUpdateWeather(sResponse); },'json');
    
}

function fUpdateWeather(sResponse) {
    
    goWeather = sResponse;//eval(sResponse);

    fToggleWeather();

}



function fToggleWeather() {

    if (jq('#weather').hasClass('sat')) {

	jq('#weather').removeClass('sat').addClass('now');

	var sCondition = goWeather.current.weather_text.replace(' skies','');

	jq('#weather_is').html('The current weather is:<br />');
	jq('#condition').html(sCondition + ', ' + goWeather.current.temp + '°');
        jq('#weather').css('background-image','url(images/'+ sCondition +'.png)');
    }

    else if (jq('#weather').hasClass('now')) {

	jq('#weather').removeClass('now').addClass('sat');

	var sCondition = goWeather.saturday.day[0].weather_text.replace(' skies','');

	jq('#weather_is').html('This saturday\'s forecast is:<br />');
	jq('#condition').html(sCondition + ', ' + goWeather.saturday.day_max_temp + '°/' + goWeather.saturday.night_min_temp + '°');
        jq('#weather').css('background-image','url(images/'+ sCondition +'.png)');

    }

    fCR('#weather');

}


function fAdvanceSlideShow() {

    jq('#slide_photos').children().first().animate({marginLeft: "-600px"}, 1000, function() { jq(this).detach().appendTo('#slide_photos').css('margin-left', '0px'); setTimeout("fAdvanceSlideShow()", 4000);});
}

function fShowPG() {

    document.body.style.overflow='hidden';

    var jPG = jq('<div></div>').attr('id','photo_gallery').appendTo(document.body);
    var jBox = jq('<div></div>').attr('id','pg_photo_box').appendTo(jPG);
    jq('<img />').attr('id','pg_photo').appendTo(jBox).load(function() { jq('.loading').remove();});
    jq('<div></div>').attr('id','pg_close').text('CLOSE').appendTo(jPG).click(fClosePG);
    jq('<div></div>').attr('id','pg_thumbs').appendTo(jPG);
    jq('<div></div>').appendTo('#pg_thumbs').addClass('v_fix').html('&nbsp;');

    jq('#shader').show();

    jq.get('countfiles.php?dir=images/photo_gallery/', function(sNum) { gsPGNum = parseInt(sNum,10);fLoadPGThumbs(); },'html');

}

function fClosePG() {
    document.body.style.overflow='auto';

    jq('#shader').hide();

    jq('#photo_gallery').remove();
}


function fLoadPGThumbs(i) {

    var sHeight = jq('#photo_gallery').outerHeight()-152;

    jq('#pg_photo').attr('src', 'load_image.php?path=images/photo_gallery/pg_1.jpg&max='+sHeight);

    var i = 1;
    while (i < gsPGNum) {
	jq('<img />').error(function() { jq(this).remove(); }).appendTo('#pg_thumbs').addClass('pg_thumb').attr('src', 'load_image.php?path=images/photo_gallery/pg_'+i+'.jpg&max=100')
	    .click(function() {
		    fScrollToMiddle(jq(this));
		    var sHeight = jq('#photo_gallery').outerHeight()-152;
		    var sSrc = (jq(this).attr('src')).replace('=100','='+sHeight);
		    jq('#pg_photo').attr('src', sSrc);
		    jq('#photo_gallery .selected').removeClass('selected');
		    jq(this).addClass('selected');
		    jq('<div></div>').addClass('loading').text('LOADING...').appendTo(jq('#photo_gallery'));
		});
	i++;
    }
    
    if (i == 1) {
	jq('.pg_thumb').first().addClass('selected');
	}
    
}

function fScrollToMiddle(jElement) {

    var jParent = jElement.parent();

    jParent.css('overflow','hidden');
    /*
    var sWid = eParent.style.height;
	
    var y = jq(eElement).offset().top;
		
    var sHeight = parseInt(jq(this).css('height'));
		
    var percent = y/sHeight;
    
    
    jq(this).stop().animate({scrollTop: jq(this)[0].scrollHeight*(percent)},'fast');
    */

 
    var sParWidth = jParent.outerWidth(true);
    
    var sElWidth = jElement.outerWidth(true);

    var x = jElement.position().left + jParent.scrollLeft();

    var sScrollTo = x - (sParWidth/2) + (sElWidth/2);
    
    
    jParent.animate({scrollLeft: sScrollTo},'fast');
}

function fPreloadPG() {
  
}


