function flashWrite(flashfile,x,y,flashvar) {
	document.write("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='"+ x +"' height='"+ y +"'>");
	document.write("<param name='movie' value='"+ flashfile +"' />");
	document.write("<param name='quality' value='high' />");
	document.write("<param name='flashvars' value='"+ flashvar +"' />");
	document.write("<param name='wmode' value='transparent' />");
	document.write("<embed src='"+ flashfile +"' wmode='transparent' ");
	document.write("flashvars='"+ flashvar +"'  ");
	document.write("quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='"+ x +"' height='"+ y +"'></embed>");
	document.write("</object>");
}

function printYear() {
	var today = new Date();
	document.write(today.getFullYear());
}

function writeEmail(email) {
	document.write('<a href="mailto:'+email+'">'+email+'</a>');
}

function changeTextSize(textSize) {
	document.body.className = 'site ' + textSize;
	return false;
}

function printPage() {
	window.print();
	return false;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function showPopup(el) {
    var overlay = $('<div class="overlay"></div>');
    overlay.hide().appendTo('form').fadeIn();
    el.fadeIn();
    overlay.click(function(e) {
        e.preventDefault();
        hidePopup(el);
    });
}
function hidePopup(el) {
    el.fadeOut();
    $('div.overlay').unbind().fadeOut(function() {
        $(this).remove();
    });
}

var overlay2 = $('<div class="overlay2"></div>');
function showPopup2(el) {
    overlay2.attr('rel', el);
    overlay2.fadeIn();
    $(el).fadeIn();
    var h = $(el).height();
    var mt = 0 - (h / 2);
    // $(el).css({ 'margin-top': mt });
}
function hidePopup2() {
    var el = overlay2.attr('rel');
    overlay2.fadeOut().attr('rel', '');
    $(el).fadeOut();
}

$(document).ready(function() {
    $.fn.corners.defaults.detectCSS = false;

    if ($('div.lightbox').length > 0) {
        $('div.lightbox a').lightBox({
            imageLoading: '/system/assets/images/lightbox/lightbox-ico-loading.gif',
            imageBtnClose: '/system/assets/images/lightbox/lightbox-btn-close.gif',
            imageBtnPrev: '/system/assets/images/lightbox/lightbox-btn-prev.gif',
            imageBtnNext: '/system/assets/images/lightbox/lightbox-btn-next.gif'
        });
    }
    overlay2.appendTo('form').click(function() {
        hidePopup2();
    }).hide();

    var date = new Date(2011, 11, 12, 0, 0, 0, 0);
    $('.timer').countdown({ until: date });

    $('div.quoteBox ul').adidoCarousel({ animDelay: 200, dynamicResize: true, mode: 'fade', resizeMask: false });
    $('div.boxPhotoCarousel ul').adidoCarousel({ buttonPrev: 'div.boxPhotoCarousel a.lnkCarouselPrev', buttonNext: 'div.boxPhotoCarousel a.lnkCarouselNext' });
    $('div#leftCol div.infoNews ul.feedListing').adidoCarousel({ horizontal: false, resizeMask: true, startPosition: 3 });
    var h = 0;
    $('div#leftCol div.infoNews ul.feedListing li').each(function() {
        var th = $(this).height();
        if (th > h) {
            h = th;
        }
    }).height(h);
    var cmh = h * 3;
    $('div#leftCol div.infoNews div.carouselMask').height(h * 3);
    $('div#leftCol div.infoNews div.carouselPosition').css({ marginTop: (h * 2) }).height(h);
    $('div#leftCol div.infoNews div.itemBox').height(h - 1);

    $('div#rightCol div.infoNews ul.feedListing').adidoCarousel({ horizontal: false, resizeMask: true, startPosition: 3 });
    var h = 0;
    $('div#rightCol div.infoNews ul.feedListing li').each(function() {
        var th = $(this).height();
        if (th > h) {
            h = th;
        }
    }).height(h);
    var cmh = h * 3;
    $('div#rightCol div.infoNews div.carouselMask').height(h * 3);
    $('div#rightCol div.infoNews div.carouselPosition').css({ marginTop: (h * 2) }).height(h);
    $('div#rightCol div.infoNews div.itemBox').height(h - 1);

    $('div#peelBoxes a').bigTarget({ clickZone: 'div:eq(1)' });

    $('#vNav').corners();
    $('div#contentBottom div.sectionLinks').corners();
    $('div.rightColInner').corners();
    $('div.boxPhotoCarousel div.carouselFrame').corners();
    $('div.boxDesc').corners({ detectCSS: true });
    $('div.formHomeSuitability fieldset.fsDetails').corners({ detectCSS: true });

    $('div.dropDownBtn a').click(function(e) {
        e.preventDefault();
        $(this).parent().next().slideToggle();
    });

    /*$('div.box').hover(function() {
    $(this).find('div.peel a').animate({ top: -46, left: -46 });
    }, function() {
    $(this).find('div.peel a').animate({ top: 0, left: 0 });
    });*/

    /*$('#hNav > ul > li').hover(function() {
    var options = {};
    $(this).children('a').effect('puff', options, 500, hNavCallback);
    }, function() {
    $(this).children('a').removeAttr('style').fadeIn();
    });*/

    function hNavCallback() {
        $(this).children('a').show();
    }

    $('input.textClear').focus(function() {
        var t = $(this).attr('title');
        var v = $(this).val();
        if (v == t) {
            $(this).val('');
        }
    }).blur(function() {
        var t = $(this).attr('title');
        var v = $(this).val();
        if (v == '') {
            $(this).val(t);
        }

    });

    $('.searchBox input.textBox').focus(function() {
        var t = $(this).attr('rel');
        var v = $(this).val();
        if (v == t) {
            $(this).val('');
        }
    }).blur(function() {
        var t = $(this).attr('rel');
        var v = $(this).val();
        if (v == '') {
            $(this).val(t);
        }

    });

    $('div.formPopup').each(function() {
        var h = $(this).height();
        var mt = -(h / 2);
        $(this).css({ marginTop: mt });
    });

    $('a.lnkPopup').click(function(e) {
        e.preventDefault();
        var h = $(this).attr('href');
        showPopup2(h);
    });
    $('a.lnkPopupClose').click(function(e) {
        e.preventDefault();
        hidePopup2();
    });

    $('a.lnkChatPopup').click(function(e) {
        e.preventDefault();
        showPopup($('div.formCallBack'));
    });

    $('a.lnkDashboardPopup').click(function(e) {
        e.preventDefault();
        showPopup($('div.popupDashboard'));
    });

    $('a#btnClosePop').click(function(e) {
        e.preventDefault();
        hidePopup($('div.popupDashboard'));
    });

    $('div.formPopup a.popupClose').click(function(e) {
        e.preventDefault();
        var box = $(this).parent();
        hidePopup(box);
    });

    /*  */
    $('fieldset.fsDetails .inputHiLite').focus(function() {
        $(this).stop().animate({ borderBottomColor: '#70bb44', borderLeftColor: '#70bb44', borderRightColor: '#70bb44', borderTopColor: '#70bb44' });
    }).blur(function() {
        $(this).stop().animate({ borderBottomColor: '#D1D1D1', borderLeftColor: '#D1D1D1', borderRightColor: '#D1D1D1', borderTopColor: '#D1D1D1' });
    });

    $('table.tblGuide tr td, table.tblGuide tr th').wrapInner('<div class="inner"></div>');
    // $('table.tblGuide tr:first-child th.total').wrapInner('<div class="inner"></div>');
    $('table.tblGuide tr:first-child th.total div.inner').append('<div class="capTop"></div>');
    // $('table.tblGuide tr:last-child td.total').wrapInner('<div class="inner"></div>');
    $('table.tblGuide tr:last-child td.total div.inner').append('<div class="capBtm"></div>');
    $('table.tblGuide tr th div.inner').each(function() {
        var ph = $(this).parent().height();
        ph = ph - 20;
        $(this).height(ph);
    });
    
    // Remove default value on click/focus
    $('div.formSolarSurvey .textBox').focus(function() {
        var t = $(this).attr('title');
        var v = $(this).val();
        if (v == t) {
            $(this).val('');
        }
    }).blur(function() {
        var t = $(this).attr('title');
        var v = $(this).val();
        if (v == '') {
            $(this).val(t);
        }
    });
    

    $(':first-child').addClass('first');
    $(':last-child').addClass('last');
    $('a[rel=newWindow]').attr('target', '_blank');
});
