var tabs = null;

$(document).ready(function() {
    if ($("#tabs").length == 1) {
        tabs = new ddtabcontent("tabs");
        tabs.setpersist(false);
        tabs.setselectedClassTarget("link");
        tabs.expandit(0)
        tabs.init();
    }
    
    $("#tab1 p a").each(function(index, element) {
        var text = $(this).text();
        text = constrain(text, 355, 'temp-news-item');
        $(this).text(text);
    });
    
    
    urlsegment();
    url = $.url(window.location);
    if( url.segment(3) == 'canco-lofts' ) {
		$('body').append('<div class="hz_window" id="progress"><a href="#">x</a><img src="/images/park-image/1.jpg" alt="1" width="800" height="550" /><img src="/images/park-image/2.jpg" alt="2" width="800" height="550" /><img src="/images/park-image/3.jpg" alt="3" width="800" height="550" /><img src="/images/park-image/4.jpg" alt="4" width="800" height="550" /><img src="/images/park-image/5.jpg" alt="5" width="800" height="550" /><img src="/images/park-image/6.jpg" alt="6" width="800" height="550" /><img src="/images/park-image/7.jpg" alt="7" width="800" height="550" /><img src="/images/park-image/8.jpg" alt="8" width="800" height="550" /><img src="/images/park-image/9.jpg" alt="9" width="800" height="550" /><img src="/images/park-image/10.jpg" alt="10" width="800" height="550" /><img src="/images/park-image/11.jpg" alt="11" width="800" height="550" /><img src="/images/park-image/12.jpg" alt="12" width="800" height="550" /><img src="/images/park-image/13.jpg" alt="13" width="800" height="550" /><img src="/images/park-image/14.jpg" alt="14" width="800" height="550" /><img src="/images/park-image/15.jpg" alt="15" width="800" height="550" /><img src="/images/park-image/16.jpg" alt="16" width="800" height="550" /><img src="/images/park-image/17.jpg" alt="17" width="800" height="550" /><img src="/images/park-image/18.jpg" alt="18" width="800" height="550" /><img src="/images/park-image/19.jpg" alt="19" width="800" height="550" /><img src="/images/park-image/20.jpg" alt="20" width="800" height="550" /><img src="/images/park-image/21.jpg" alt="21" width="800" height="550" /><img src="/images/park-image/22.jpg" alt="22" width="800" height="550" /><img src="/images/park-image/23.jpg" alt="23" width="800" height="550" /><img src="/images/park-image/24.jpg" alt="24" width="800" height="550" /><img src="/images/park-image/25.jpg" alt="25" width="800" height="550" /><img src="/images/park-image/26.jpg" alt="26" width="800" height="550" /><img src="/images/park-image/27.jpg" alt="27" width="800" height="550" /><img src="/images/park-image/28.jpg" alt="28" width="800" height="550" /><img src="/images/park-image/29.jpg" alt="29" width="800" height="550" /><img src="/images/park-image/30.jpg" alt="30" width="800" height="550" /><img src="/images/park-image/31.jpg" alt="31" width="800" height="550" /><img src="/images/park-image/32.jpg" alt="32" width="800" height="550" /><img src="/images/park-image/33.jpg" alt="33" width="800" height="550" /><img src="/images/park-image/34.jpg" alt="34" width="800" height="550" /><img src="/images/park-image/35.jpg" alt="35" width="800" height="550" /><img src="/images/park-image/36.jpg" alt="36" width="800" height="550" /><img src="/images/park-image/37.jpg" alt="37" width="800" height="550" /><img src="/images/park-image/38.jpg" alt="38" width="800" height="550" /><img src="/images/park-image/39.jpg" alt="39" width="800" height="550" /><img src="/images/park-image/40.jpg" alt="40" width="800" height="550" /><img src="/images/park-image/41.jpg" alt="41" width="800" height="550" /><img src="/images/park-image/42.jpg" alt="42" width="800" height="550" /><img src="/images/park-image/43.jpg" alt="43" width="800" height="550" /><img src="/images/park-image/44.jpg" alt="44" width="800" height="550" /><img src="/images/park-image/44.jpg" alt="44" width="800" height="550" /><img src="/images/park-image/44.jpg" alt="44" width="800" height="550" /><img src="/images/park-image/45.jpg" alt="45" width="800" height="550" /><img src="/images/park-image/45.jpg" alt="45" width="800" height="550" /><img src="/images/park-image/45.jpg" alt="45" width="800" height="550" /></div>');
	}
	
	if( $('.hz_window').length != 0 ) { $('body').append('<span class="overlay"></span>'); }
	
	$('.hz_box').click(function(){
		$('.overlay').fadeToggle();
		tgt = $(this).attr('href');
		$(tgt).fadeToggle();
		hz_slide(tgt);
		return false
	});
	
	$('.overlay, .hz_window a ').click(function(){
		$('.hz_window:visible, .overlay:visible').fadeToggle();
		setInterval('swapImages()', 5000);
		return false
	});
    
});

function hz_slide(tgt) {

	clearInterval(backgroundTimer);

	count = 0;
	$('#progress img').hide();
    setTimeout( function(){
	    stopSlide = setInterval(function(){
	
	    	// If a slide is visible, fade it out
	    	if(count != 0) { $('#progress img:nth-child('+count+')').hide(); }
	    	
	    	// Increase counter by 1, used to target next image
	    	count++
	    	
	    	// Fade in current slide count
	    	$('#progress img:nth-child('+count+')').show();
	    	
	    	// Stop interval and close lightbox
	    	if( $('#progress img').length < count ) {
	    		clearInterval(stopSlide);
	    		//$('.overlay, .hz_window').delay(1000).fadeOut();
	    	}
	    }, 1000);
    }, 4000);

}

function constrain(text, ideal_width, className) {
    var temp_item = ('<span class="' + className + '_hide" style="display:none;">' + text + '</span>');
    $(temp_item).appendTo('#tab1');
    var item_width = $('span.' + className + '_hide').width();
    var ideal = parseInt(ideal_width);
    var smaller_text = text;

    if (item_width > ideal_width){
        while (item_width > ideal) {
            smaller_text = smaller_text.substr(0, (smaller_text.length - 1));
            $('.' + className + '_hide').html(smaller_text);
            item_width = $('span.' + className + '_hide').width();
        }
        smaller_text = smaller_text + '...'
    }
    $('span.' + className + '_hide').remove();
    return smaller_text;
}

function urlsegment(){
 var tag2attr = {
        a : 'href',
        img : 'src',
        form : 'action',
        base : 'href',
        script : 'src',
        iframe : 'src',
        link : 'href'
    },
    
key = ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","fragment"], // keys available to query

aliases = { "anchor" : "fragment" }, // aliases for backwards compatability

parser = {
strict : /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, //less intuitive, more accurate to the specs
loose : /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // more intuitive, fails on relative paths and deviates from specs
},

querystring_parser = /(?:^|&|;)([^&=;]*)=?([^&;]*)/g, // supports both ampersand and semicolon-delimted query string key/value pairs

fragment_parser = /(?:^|&|;)([^&=;]*)=?([^&;]*)/g; // supports both ampersand and semicolon-delimted fragment key/value pairs

function parseUri( url, strictMode )
{
var str = decodeURI( url ),
res = parser[ strictMode || false ? "strict" : "loose" ].exec( str ),
uri = { attr : {}, param : {}, seg : {} },
i = 14;

while ( i-- )
{
uri.attr[ key[i] ] = res[i] || "";
}

// build query and fragment parameters

uri.param['query'] = {};
uri.param['fragment'] = {};

uri.attr['query'].replace( querystring_parser, function ( $0, $1, $2 ){
if ($1)
{
uri.param['query'][$1] = $2;
}
});

uri.attr['fragment'].replace( fragment_parser, function ( $0, $1, $2 ){
if ($1)
{
uri.param['fragment'][$1] = $2;
}
});

// split path and fragement into segments

        uri.seg['path'] = uri.attr.path.replace(/^\/+|\/+$/g,'').split('/');
        
        uri.seg['fragment'] = uri.attr.fragment.replace(/^\/+|\/+$/g,'').split('/');
        
        // compile a 'base' domain attribute
        
        uri.attr['base'] = uri.attr.host ? uri.attr.protocol+"://"+uri.attr.host + (uri.attr.port ? ":"+uri.attr.port : '') : '';
        
return uri;
};

function getAttrName( elm )
{
var tn = elm.tagName;
if ( tn !== undefined ) return tag2attr[tn.toLowerCase()];
return tn;
}

$.fn.url = function( strictMode )
{
var url = '';

if ( this.length )
{
url = $(this).attr( getAttrName(this[0]) ) || '';
}

        return $.url( url, strictMode );
};

$.url = function( url, strictMode )
{
if ( arguments.length === 1 && url === true )
        {
            strictMode = true;
            url = undefined;
        }
        
        strictMode = strictMode || false;
        url = url || window.location.toString();
        
        return {
            
            data : parseUri(url, strictMode),
            
            // get various attributes from the URI
            attr : function( attr )
            {
                attr = aliases[attr] || attr;
                return attr !== undefined ? this.data.attr[attr] : this.data.attr;
            },
            
            // return query string parameters
            param : function( param )
            {
                return param !== undefined ? this.data.param.query[param] : this.data.param.query;
            },
            
            // return fragment parameters
            fparam : function( param )
            {
                return param !== undefined ? this.data.param.fragment[param] : this.data.param.fragment;
            },
            
            // return path segments
            segment : function( seg )
            {
                if ( seg === undefined )
                {
                    return this.data.seg.path;
                }
                else
                {
                    seg = seg < 0 ? this.data.seg.path.length + seg : seg - 1; // negative segments count from the end
                    return this.data.seg.path[seg];
                }
            },
            
            // return fragment segments
            fsegment : function( seg )
            {
                if ( seg === undefined )
                {
                    return this.data.seg.fragment;
                }
                else
                {
                    seg = seg < 0 ? this.data.seg.fragment.length + seg : seg - 1; // negative segments count from the end
                    return this.data.seg.fragment[seg];
                }
            }
            
        };
        
};
}
