var timeout         = 500;
var closetimer		= 0;
var ddmenuitem      = 0;

$(document).ready(function() {

// read cookie and set style - if none exist use full graphic style     
var c = readCookie('style');
if (c) {
switchStylestyle(c);
} else {
full_graphics()
};

// switch styles        
$('.styleswitch').click(function() {
switchStylestyle(this.getAttribute("rel"));
return false;
});

// txt chnager
if($.cookie('TEXT_SIZE')) {
		$('#wrapper').addClass($.cookie('TEXT_SIZE'));
	}
	$('.resizer a').click(function() {
		var textSize = $(this).parent().attr('class');
		$('#wrapper').removeClass('small medium large').addClass(textSize);
		$.cookie('TEXT_SIZE',textSize, { path: '/', expires: 10000 });
		return false;
	});


// external links       
extLinks();



// load press release on front page     
loadPressRelease();

// initialise validation on both forms      
$("#contactForm").validate();
$("#myHomeForm").validate();

// toggle press nav
$('div.pressNav > ul:gt(0)').hide(); 

$('div.pressNav > h3').wrapInner('<a>'+ '</a>');
$('div.pressNav > h3').click(function() {
$(this).toggleClass('opened').next().slideToggle('fast');
 });

// dl - change bgd on hover, update message in title tag and update status bar
$( 'dl.pressRelease, dl.portfolioItem:not(.noImage)'  ).hover( function() {
    
        $( this ).addClass( 'hover' );
        $( this ).css( 'cursor', 'pointer' );
        $( 'dl.pressRelease' ).attr( 'title', 'Click here to see full article' );
        $( 'dl.portfolioItem:not(.noImage)' ).attr( 'title', 'Click here to see a larger image' );
        window.status=$(this).find("a:first").attr("href");

    }, function() {
        
            $( this ).removeClass( 'hover' );
    });
    
// add click here message to portfolio items that contain images
$( 'dl.portfolioItem:not(.noImage)' ).find('dd:last').after("<dd><small>[ Click to see larger image ]</small></dd>");
    
        
// add onclick behaviour when link found inside press release
    $( 'dl.pressRelease' ).click(function(){
      window.location=$(this).find("a:first").attr("href"); return false;
    });
    
// add thickbox to onclick behaviour when link found inside portfolio with image
    $( 'dl.portfolioItem:not(.noImage)' ).click(function(){
      tb_show( $(this).find("a:first").attr("title"), $(this).find("a:first").attr("href") )
    }); 
    
    
    
// slideshow type one

$('#slideShow').cycle({ 
    fx:     'fade', 
    speed:  'fast', 
    timeout: 0, 
    pager:  '#slideShowNav', 
    pagerAnchorBuilder: function(idx, slide) { 
        // return selector string for existing anchor 
        return '#slideShowNav li:eq(' + idx + ') a'; 
    } 
});

// slideshow type two

$('#gallery').cycle({ 
    fx:    'fade', 
    pause:  1 
});


// Residential scroller - homepage
$('#scrollup').cycle({ fx:'fade', timeout: 6000, delay:  -2000 } );

// Commercial scroller - homepage
$('#commScrollup').cycle({ fx:'fade', timeout: 6000, delay:  -2000 } );

// end onload
});


function jsddm_open() {
jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
	}

function jsddm_close() {
if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}

function jsddm_timer(){	
closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer() {	
if(closetimer)
	{	window.clearTimeout(closetimer);
		closetimer = null;}
		}

function jsddm_remove() {
$('#jsddm > li').unbind('mouseover', jsddm_open);
$('#jsddm > li').unbind('mouseout',  jsddm_timer);
}

function jsddm_setup() {
$('#jsddm > li').bind('mouseover', jsddm_open);
$('#jsddm > li').bind('mouseout',  jsddm_timer);
}

document.onclick = jsddm_close;


// external links
function extLinks() {

    $( "a[@rel='external']" ).click( function(){
        window.open( this.href );
        return false;
    });
    
};
    

// Loads press release into box on home page
function loadPressRelease() {

var url = 'http://www.summerfield.co.uk/press/';
var addr = (url + " dl:first > dt , dl:first > dd:not(.img) ");

$("#sidebar div.roundBox").show();      
$('#pressReleaseTicker').load(addr);
};

// cookie functions http://www.quirksmode.org/js/cookies.html
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 var 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 eraseCookie(name)
{
    createCookie(name,"",-1);
};
// /cookie functions


// curved background on feature panel
function roundCorners() {
$("div.roundBox").wrapInner('<div class="boxOut">'+ 
'<div class="bdy">'+ 
'<div class="r">'+ 
'<div class="mid">'+ 
'</div>'+ 
'</div>'+ 
'</div>'+ 
'</div>');

$('div.boxOut').prepend('<div class="hdr">'+
'<div class="r"></div>'+
'</div>')
.append('<div class="ftr">'+
'<div class="r"></div>'+
'</div>');
};


 



// style switcher
function switchStylestyle(styleName) {
$('link[@rel*=style][title]').each(function(i) {
this.disabled = true;
if (this.getAttribute('title') == styleName) this.disabled = false;
if (styleName == 'summerfield_developments_txt') { low_graphics() };
if (styleName == 'summerfield_developments') { full_graphics() };
});
createCookie('style', styleName, 365);
};

// low graphics
function low_graphics() {
$("body").removeClass("jscript");
jsddm_remove();
$('#jsddm ul').css('visibility', 'visible');
};

// full graphics
function full_graphics() {
$("body").addClass("jscript");
$('#jsddm ul').css('visibility', 'hidden');
jsddm_setup();

// if rounded corners are already there, don't add them again
if ($("div.boxOut").length == 0) { roundCorners() };
};