var scroll;
var scrollApi;

/*function resizeTextBoxes(height, spacing){
    
    if(typeof height == 'undefined' || height == '') height = 320;
    var fdHeight = $('#featuredDescription').outerHeight();
    var fdPosition = $('#featuredDescription').offset();
    
    var fwPosition = $('#itemDesc').offset();
    
    var padding = fwPosition.top - (fdPosition.top + fdHeight);
    var box = $('#itemDesc');
    
    var distance = typeof spacing == 'undefined' ? 20 : spacing;
    
    if(padding < distance){
        
        var finalHeight = box.outerHeight() - Math.abs( padding - distance );
        console.log('final1', finalHeight);
        if (finalHeight > 320 && typeof meet != 'undefined' && meet != null)
            finalHeight = 320;
        box.css('height', finalHeight);
        
        //box.css('overflow','auto');
    //    $('.jspVerticalBar').show();
    } else if(padding > 70){
        var finalHeight = box.outerHeight() - Math.abs( padding - distance );
        console.log('final2', finalHeight);
        if(Math.abs(finalHeight) > 320) finalHeight = 320;
        box.css('height',Math.abs(finalHeight)+'px');
        box.css('overflow','hidden');
    //    $('.jspVerticalBar').hide();
        //box.css('padding','15px');
    }
    if(scrollApi != undefined && $('#itemDesc .jspContainer').length){
      
        scrollApi.reinitialise();
    } else {
        
        scroll = $('#itemDesc').jScrollPane({animateScroll: true});
        scrollApi = scroll.data('jsp');
    }
}*/

function getSpace(){

    var fdHeight = $('#featuredDescription').outerHeight();
    var fdPosition = $('#featuredDescription').offset();
    var fwPosition = $('#itemDesc').offset();
    
    return fwPosition.top + $('#itemDesc').outerHeight() - (fdPosition.top + fdHeight);
}

function resizeTextBoxes(height, spacing){
    
    if(typeof height == 'undefined' || height == '') height = 320;
    var fdHeight = $('#featuredDescription').outerHeight();
    var fdPosition = $('#featuredDescription').offset();
    
    var fwPosition = $('#itemDesc').offset();
    
    var padding = getSpace();
    var box = $('#itemDesc');
    
    var distance = typeof spacing == 'undefined' ? 20 : spacing;
    
    spacing = distance;
    console.log('test',padding, distance);
    if(padding < 320){
        
        /*var finalHeight = box.outerHeight() - Math.abs( padding - distance );
        
        if (finalHeight > 320 && typeof meet != 'undefined' && meet != null){
            
            finalHeight = 320;
        }*/
        finalHeight = padding - spacing;
        console.log('resize 1');
        box.css('height', finalHeight);
    
    } else{

        if(padding > 320 + spacing){
            console.log('resize 2');
            finalHeight = 320;
        }
        else {
            console.log('resize 3');
            finalHeight = 320 - spacing;
        }
        /*
        if(padding > 320) finalHeight = 320;
        else if(padding > spacing){
           
            finalHeight = 320;
            if(finalHeight > padding) finalHeight = padding - spacing;
    
        }
        */
        box.css('height',Math.abs(finalHeight)+'px');
        box.css('overflow','hidden');
        
    //    $('.jspVerticalBar').hide();
        //box.css('padding','15px');
    }
    if(scrollApi != undefined && $('#itemDesc .jspContainer').length){
    
        scrollApi.reinitialise();
    } else {
        
        scroll = $('#itemDesc').jScrollPane({animateScroll: true});
        scrollApi = scroll.data('jsp');
    }
}

function resizeBlogTextBoxes(height){
   
    //if(height == undefined || height == '') height = 320;
    
    var fdHeight = $('#featuredDescription').outerHeight();
    var fdPosition = $('#featuredDescription').offset();
    
    var pbPosition = $('#pageBoxes').offset();
    
    var box = $('#itemDesc');

    var distance = 50;
    
    box.css('height', (pbPosition.top - (fdPosition.top + fdHeight)) - distance + 'px');
    
    if(scrollApi != undefined){
        
        scrollApi.reinitialise();
    } else {
       
        scroll = $('#itemDesc').jScrollPane({animateScroll: true});
        scrollApi = scroll.data('jsp');
    }
}

function calculateBlogTextBoxes(height){
   
    //if(height == undefined || height == '') height = 320;
    
    var fdHeight = $('#featuredDescription').outerHeight();
    var fdPosition = $('#featuredDescription').offset();
    
    var pbPosition = $('#pageBoxes').offset();
    
    var box = $('#itemDesc');

    var distance = 50;

    return (pbPosition.top - (fdPosition.top + fdHeight)) - distance ;
    
}

function resizeDescBox(){

    var fdHeight = $('#featuredDescription').outerHeight();
    var fdPosition = $('#featuredDescription').offset();
    
    var fwPosition = $('#footerDesc').offset();
    
    var padding = fwPosition.top - (fdPosition.top + fdHeight);
    var box = $('#footerDesc');
    
    var distance = 20;

    if(padding < distance){
        box.css('width', '880px');

    } else if(padding > 70){
        box.css('width','414px');
    }
    
}

function switchPage(p){
    var pageRez = page;
    
    if(p != page){

        //move page in the animation layer
        $('#bg' + items.project.config.id + '-' + (p-1)).css('z-index', '20');
        
        //make the page bg visible
        $('#bg'  + items.project.config.id + '-' + (p-1)).css('left', '-1920px');
        $('#bg'  + items.project.config.id + '-' + (p-1)).fadeIn('10', function(){
            //slide it in view
            $('#bg'  + items.project.config.id + '-' + (p-1)).animate(
                {
                    left: '0'
                },
                1000,
                function(){
                    //put the prev bg in the deposit layer
                    $('#bg'  + items.project.config.id + '-' + (pageRez-1)).css('z-index', '15');
                    $('#bg'  + items.project.config.id + '-' + (pageRez-1)).fadeOut('10', function(){
                        $('#bg' + items.project.config.id + '-' + (pageRez-1)).css('left', '0px');
                    });
                    //put the page bg in the view layer
                    $('#bg' + items.project.config.id + '-' + (p-1)).css('z-index', '19');
                }
            );
        });
        
        $('#item'+ page +'.itemContent').fadeOut('fast', function(){
            $('#item'+ p +'.itemContent').fadeIn('fast', function(){
                scrollApi.reinitialise();
            });
        });
        
        
        $('#p' + page).css('background-image', 'url("'+ siteUrl + 'images/page_bullet.png")');
        $('#p' + p).css('background-image', 'url("'+ siteUrl + 'images/page_bullet_sel.png")');
        
        page = p;
    }
}

function switchBlogPage(p){
    
    switch(p){
        case '1':
            $('#featuredDescription h2').html('OUR LIFE ENCASED IN BRICK WOOD &amp; STEEL');
            $('#featuredDescriptionText').html("Welcome. It's our office, where we make the good stuff happen. From late nights to early morning, great design to total shenannegans, it all happens right here. ");
        break;
        
        case '2':
            $('#featuredDescription h2').html('2 LIFE ENCASED IN BRICK WOOD &amp; STEEL');
            $('#featuredDescriptionText').html("2. It's our office, where we make the good stuff happen. From late nights to early morning, great design to total shenannegans, it all happens right here. ");
        break;
        
        case '3':
            $('#featuredDescription h2').html('3 LIFE ENCASED IN BRICK WOOD &amp; STEEL');
            $('#featuredDescriptionText').html("3. It's our office, where we make the good stuff happen. From late nights to early morning, great design to total shenannegans, it all happens right here. ");
        break;
        
        case '4':
            $('#featuredDescription h2').html('4  LIFE ENCASED IN BRICK WOOD &amp; STEEL');
            $('#featuredDescriptionText').html("4. It's our office, where we make the good stuff happen. From late nights to early morning, great design to total shenannegans, it all happens right here. ");
        break;
        
        case '5':
            $('#featuredDescription h2').html('5 LIFE ENCASED IN BRICK WOOD &amp; STEEL');
            $('#featuredDescriptionText').html("5. It's our office, where we make the good stuff happen. From late nights to early morning, great design to total shenannegans, it all happens right here. ");
        break;
    }
    
    $('#item'+ page +'.itemContent').fadeOut('fast', function(){
        $('#item'+ p +'.itemContent').fadeIn('fast', function(){
            scrollApi.reinitialise();
        });
    });
    
    $('#p' + page).css('background-image', 'url("'+ siteUrl + 'images/page_bullet.png")');
    $('#p' + p).css('background-image', 'url("'+ siteUrl + 'images/page_bullet_sel2.png")');
    
    page = p;
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );

    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if ( expires )
    {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function htmlEntities(str) {
    return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}

