$(window).load(function () {
    // LOAD LAYOUTS AND FOOTER    
    documentSize();
});

$(document).ready(function(){
//$(window).load(function () {
    // DEFINE IMG SIZE - NEWS SECTION/RIGHT COLUMN
    $('.item').find('img').each(function(){
        if($(this).width()>($(this).height()+25)){
            $(this).css({ 'width':'300px' });
        }else{
            $(this).css({ 'height':'75px' });
        }
    });
    
    // LOAD LAYOUTS AND FOOTER
    //documentSize();
    
    if($('body').hasClass('fullscreen')){
	// get Image Captions
        var ImageCaptions = new Array();
        $('.hiddenCaptions').each(function(){
            ImageCaptions.push($(this).html());
        });
        var ImageDescriptions = new Array();
        $('.hiddenDescriptions').each(function(){
            ImageDescriptions.push($(this).html());
        });
        // SLIDESHOW
        $(function(){
            $.fn.supersized.options = {  
                startwidth: 640,  
                startheight: 480,
                vertical_center: 1,
                slideshow: 1,
                navigation: 1,
                thumbnail_navigation: 0,
                transition: 1, //0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
                pause_hover: 0,
                slide_counter: 1,
                slide_captions: 1,
                slide_interval: 5000,
                slides : [
                        {image : '/images/slides/001.jpg', title : '<span>'+ImageCaptions[0]+'<span class="descr">'+ImageDescriptions[0]+'</span></span>', url : ''},
                        {image : '/images/slides/002.jpg', title : '<span>'+ImageCaptions[1]+'<span class="descr">'+ImageDescriptions[1]+'</span></span>', url : ''},  
                        {image : '/images/slides/003.jpg', title : '<span>'+ImageCaptions[2]+'<span class="descr">'+ImageDescriptions[2]+'</span></span>', url : ''},
                        {image : '/images/slides/004.jpg', title : '<span>'+ImageCaptions[3]+'<span class="descr">'+ImageDescriptions[3]+'</span></span>', url : ''},
                        {image : '/images/slides/005.jpg', title : '<span>'+ImageCaptions[4]+'<span class="descr">'+ImageDescriptions[4]+'</span></span>', url : ''},  
                        {image : '/images/slides/006.jpg', title : '<span>'+ImageCaptions[5]+'<span class="descr">'+ImageDescriptions[5]+'</span></span>', url : ''},
                        {image : '/images/slides/007.jpg', title : '<span>'+ImageCaptions[6]+'<span class="descr">'+ImageDescriptions[6]+'</span></span>', url : ''},
                        {image : '/images/slides/008.jpg', title : '<span>'+ImageCaptions[7]+'<span class="descr">'+ImageDescriptions[7]+'</span></span>', url : ''},  
                        {image : '/images/slides/009.jpg', title : '<span>'+ImageCaptions[8]+'<span class="descr">'+ImageDescriptions[8]+'</span></span>', url : ''},
                        {image : '/images/slides/010.jpg', title : '<span>'+ImageCaptions[9]+'<span class="descr">'+ImageDescriptions[9]+'</span></span>', url : ''},
                        {image : '/images/slides/011.jpg', title : '<span>'+ImageCaptions[10]+'<span class="descr">'+ImageDescriptions[10]+'</span></span>', url : ''},  
                        {image : '/images/slides/012.jpg', title : '<span>'+ImageCaptions[11]+'<span class="descr">'+ImageDescriptions[11]+'</span></span>', url : ''},
                        {image : '/images/slides/013.jpg', title : '<span>'+ImageCaptions[12]+'<span class="descr">'+ImageDescriptions[12]+'</span></span>', url : ''},
                        {image : '/images/slides/014.jpg', title : '<span>'+ImageCaptions[13]+'<span class="descr">'+ImageDescriptions[13]+'</span></span>', url : ''},
                        {image : '/images/slides/015.jpg', title : '<span>'+ImageCaptions[14]+'<span class="descr">'+ImageDescriptions[14]+'</span></span>', url : ''},
                        {image : '/images/slides/016.jpg', title : '<span>'+ImageCaptions[15]+'<span class="descr">'+ImageDescriptions[15]+'</span></span>', url : ''}
                ]
                // Headline like <strong>vaho</strong>producción
            };
            $('#supersized').supersized();
	});
        var htmlString = "<div id='controls-wrapper'>"
                        +"<div id='controls'>"
                        +"<div id='navigation'>"
                        +"<span class='slide_nav' id='prevslide'>&nbsp;</span>"
                        +"<span class='slide_nav' id='nextslide'>&nbsp;</span>"
			+"</div>"
                        +"</div>"
                        +"</div>"
                        +"<span class='slide_nav_arrow' id='prevslide_arrow'>&nbsp;</span>"
                        +"<span class='slide_nav_arrow' id='nextslide_arrow'>&nbsp;</span>"
                        +"<div id='supersized'></div>";
        $('body').append(htmlString);	
    }
        
    // MENU ANIMATION
    $('.menu').find('li').hover(
        function(){
            $(this).find('.subMenu').stop(true,true).slideDown();
            $(this).find('a').first().addClass('selected');
        },
        function(){
            $(this).find('.subMenu').stop(true,true).delay(300).hide();
            $(this).find('a').first().removeClass('selected');
        }
    );
    $('.subMenu').find('li').hover(
        function(){
            $(this).find('.menuFamilies').stop(true,true).delay(300).show();
        },
        function(){
            $(this).find('.menuFamilies').stop(true,true).delay(300).hide();
        }
    );
    $('.menuFamilies').find('li.menuFamilies_li').hover(
        function(){
            $(this).find('.menuModel').stop(true,true).show();
        },
        function(){
            $(this).find('.menuModel').stop(true,true).delay(300).hide();
        }
    );


    // ======================================================================================================================

        
    // PROYECTOS
    $('.checkbox').click(function(){
        var getCompany = $(this).html();
        getCompany = getCompany.replace(/<\/?[a-zA-Z0-9 ="_]+>/g,"");
        getCompany = getCompany.replace("&amp;","&");
        
        if($(this).hasClass('all')){
            if($(this).hasClass('choosen')){
                $('.checkbox').each(function(){
                    $(this).removeClass('choosen');
                });
                $('.gallery').find('img').each(function(){
                    $(this).addClass('not_fitting');
                    $(this).removeClass('choosen');
                });
            }else{
                $('.checkbox').each(function(){
                    $(this).addClass('choosen');
                });
                $('.gallery').find('img').each(function(){
                    $(this).removeClass('not_fitting');
                    $(this).addClass('choosen');
                });
            }
        }else{
            if($(this).hasClass('choosen')){
                $(this).removeClass('choosen');
                $('.gallery').find('img').each(function(){
                    if($(this).attr('alt')==getCompany){
                        $(this).addClass('not_fitting');
                        $(this).removeClass('choosen');
                    }
                });
		var object = $('img.choosen:first');
		var empresa = object.attr('alt');
            }else{
                $(this).addClass('choosen');
                $('.gallery').find('img').each(function(){
                    if($(this).attr('alt')==getCompany){
                        $(this).removeClass('not_fitting');
                        $(this).addClass('choosen');
                    }else if(!$(this).hasClass('choosen')){
                        $(this).addClass('not_fitting');
                    }
                });		
		var object = $('img[alt="'+getCompany+'"]');
		var empresa = getCompany;
            }
        }
	
	if($('.selector a').hasClass('choosen')){
	    // load first selected project in right bar
	    //var object = $('img[alt="'+getCompany+'"]');
	    var url_big 	= object.attr('image');
	    var url_lightbox= object.attr('imagebig');
	    var description = object.attr('description');
	    $('#big_preview').css('background','url("'+url_big+'") left top no-repeat');
	    $('#big_preview').attr('href',url_lightbox);
	    $('#thumbnails').find('img').removeClass('black');
	    $('#description_proyecto').html(description);
	    
	    var url_clickedImage = object.attr('src');
	    thumbs(empresa,url_clickedImage);
	}
    	
    });
    var hasClassNotFitting = false;
    $('.gallery').find('img').hover(
        function(){
            if($(this).hasClass('not_fitting')){
                hasClassNotFitting = true;
                $(this).removeClass('not_fitting');
            }
        },
        function(){
            if(!$(this).hasClass('choosen') && hasClassNotFitting){
                $(this).addClass('not_fitting');
            }
        }
    );
    // append click-ability
    $('.thumb').bind('click',function(){
            url_big     = $(this).attr('image');
	    url_lightbox= $(this).attr('imagebig');
            description = $(this).attr('description');
            $('#big_preview').css('background','url("'+url_big+'") left top no-repeat');
	    $('#big_preview').attr('href',url_lightbox);
            $('#thumbnails').find('img').removeClass('black');
            $(this).addClass('black');
            $('#description_proyecto').html(description);
        });
    // dynamically loaded objects
    var class_chosen;
    $('.gallery').find('img').click(function(){
        //get values
        var url_big             = $(this).attr('image');
	var url_lightbox        = $(this).attr('imagebig');
        var url_clickedImage    = $(this).attr('src');
        var empresa             = $(this).attr('alt');
        var title               = $(this).attr('title');
        var description         = $(this).attr('description');
        
        //make changes
        $('#nombre_empresa').html(empresa);
        $('#title_proyecto').html(title);
        $('#description_proyecto').html(description);
        
        $(this).removeClass('not_fitting');
        $(this).addClass('choosen');
        $('.gallery').find('img').each(function(){
            $(this).css('border-color','#c3c3c3');
        });
        $(this).css('border-color','#464646');
        //load big preview
        $('#big_preview').css('background','url("'+url_big+'") left top no-repeat');
	$('#big_preview').attr('href',url_lightbox);
        
	thumbs(empresa,url_clickedImage);
    });
    
    
    // ======================================================================================================================
    
    /* CATALEG SLIDE */
    /* DEFINE VARIABLES */
    var original_duration = 10000;
    
    /* DO NOT CHANGE */
    var number_of_images = $('#image_content').find('li').size();;
    var number_of_rows = $('#image_content').find('ul').size();
    var images_per_row = $('#image_content').find('ul:first-child').find('li').size();
    var li_marginRight = parseFloat($('#image_content').find('li').css("margin-right"));
    var li_marginBottom = parseFloat($('#image_content').find('li').css("margin-bottom"));
    
    // set container-height
    var container_height = ((parseFloat($('#image_content').find('li').css("height"))+li_marginBottom) * number_of_rows)+20+(20-li_marginBottom);
//    $('#image_container').css({"height":container_height});
    
    // IE fix
    var IEwidth_li = 0;
    if (navigator.appName == 'Microsoft Internet Explorer'){
            var IEversion = parseFloat(getInternetExplorerVersion());
            if(IEversion<8){
                    var negativeTop = -container_height;
                    
                    $('#image_content').css({"top":negativeTop});
                    IEwidth_li = parseFloat($('#image_content').find('.image_wrapper').css("width"));
                    $('#image_content').find('li').css({"width":IEwidth_li});
                    //$('#image_content').find('a').css({"width":IEwidth_li});
            }
    }
    
    // position the arrows and the text in the scrollbars
    var arrow_position = -1*((container_height/2)+10);
    $('#scrollbar_left_text').css({marginTop:arrow_position});
    $('#scrollbar_right_text').css({marginTop:arrow_position});
    
    var container_width = parseFloat($('#image_container').css("width"));
    // set content-width (slider)
    var content_width = ((parseFloat($('#image_content').find('li').css("width"))+li_marginRight) * images_per_row)+34+(34-li_marginRight);
    // IE fix
    /*if (navigator.appName == 'Microsoft Internet Explorer'){
            var IEversion = getInternetExplorerVersion();
            if(IEversion<8){
                    content_width = ((IEwidth_li+li_marginRight) * images_per_row)+34+(34-li_marginRight);
            }
    }*/
    if(content_width<=container_width){
            content_width=container_width;
            $('#scrollbar_left').css({"visibility":"hidden"});
            $('#scrollbar_right').css({"visibility":"hidden"});
    }
    $('#image_content').css({"width":content_width});
    if (navigator.appName == 'Microsoft Internet Explorer'){
            var IEversion = getInternetExplorerVersion();
            if(IEversion<8){
                    //content_width +=1000;
                    //$('#image_content').css({"width":content_width});
            }
    }
    
    var slide_width = content_width-container_width;
    var slide_rtl = -1 * slide_width;
        slide_rtl = slide_rtl+"px";
        
    var marginLeft_now, duration;
    $('#scrollbar_right').mouseenter(function(){
            marginLeft_now = parseFloat($('#image_content').css("margin-left"));
            duration = (original_duration * (content_width+marginLeft_now) / content_width)/2;
            
            $('#image_content').animate({"margin-left": slide_rtl},duration);
    });
    $('#scrollbar_right').mouseleave(function(){
            $('#image_content').stop(true);
    });
    
    var slide_ltr = 0;
        slide_ltr = slide_ltr+"px";
    var duration_difference;
    $('#scrollbar_left').mouseenter(function(){
            marginLeft_now = parseFloat($('#image_content').css("margin-left"));
            duration_difference = original_duration * (content_width+marginLeft_now) / content_width;
            duration = original_duration-duration_difference;
            
            $('#image_content').animate({"margin-left": slide_ltr},duration);
    });
    $('#scrollbar_left').mouseleave(function(){
            $('#image_content').stop(true);
    });
});

/* CHECK IE VERSION */
function getInternetExplorerVersion(){
	var browser = navigator.userAgent;
	var pattern  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
	if (pattern.exec(browser) != null){
		browserVersion = parseFloat( RegExp.$1 );
	}
	return browserVersion;
}

// DEFINE DOCUMENT MESSUREMENTS
function documentSize()
{
    // get size of whole body
    var bodyHeight = $('body').height();
    // get necessary size of contents
    var contentHeight_left = $('#content_left').height();
    var contentHeight_right = $('#content_right').height();
    if(contentHeight_right>contentHeight_left){
        var contentHeight = contentHeight_right;
    }else{
        var contentHeight = contentHeight_left;
    }
    // get size of footer
    var footerHeight = $('#wrapper_footer').height();
    
    var contentMinHeight = bodyHeight-footerHeight;
    if(contentMinHeight>contentHeight){ var newHeight = contentMinHeight; }else{ var newHeight = contentHeight; }
    var wholeHeight = newHeight+footerHeight;
    
    // main content - left
    $('#container').css({"height":newHeight+"px"});
    $('#main_content').find('.background_left').css({"height":newHeight+"px"});
    $('#main_content').find('.background_left').find('.content_wrapper').css({"height":newHeight+"px"});
    // main content - right
    if($('body').hasClass('fullscreen')){        
        // FITTINGs
        $('#main_content').find('.background_right').css({"height":contentHeight_right+"px"});
        $('#main_content').find('#content_right').css({"height":contentHeight_right+"px"});
        $('.slide_nav').css({"height":newHeight+"px"});
        var position = newHeight*3/4-33;
        $('.slide_nav_arrow').css({"top":position+"px"});
    }else{
	$('#wrapper').css({"height":newHeight});
        $('#main_content').find('.background_right').css({"height":newHeight+"px"});
    }
    // footer content
    $('#footer_content').find('.background_left').css({"height":footerHeight+"px"});
    $('#footer_content').find('.background_right').css({"height":footerHeight+"px"});
	
	return newHeight;
}

// PLACE THUMBNAILS FOR PROJECT GALLERY
function thumbs(company,url_clickedImage){
    //get number of projects
    var items = 0;
    $('#thumbnails').find('a').detach();
    $('.gallery').find('img').each(function(){  if($(this).attr('alt')==company){ items++; } });
    
    // place thumbs
    var item_no = 1;
    $('.gallery').find('img').each(function(){
	if($(this).attr('alt')==company){
	    //get values
	    url_thumb   = $(this).attr('src');
	    url_big     = $(this).attr('image');
	    url_lightbox= $(this).attr('imagebig');
	    title       = $(this).attr('title');
	    descr = $(this).attr('description');
	    if($(this).attr('src') == url_clickedImage){ class_chosen = "black"; }else{ class_chosen = ""; }
	    if(item_no == items){ class_last = "right"; }else{ class_last = ""; }
	    
	    //append thumbnail--------------
	    $('#thumbnails').append('<a><img class="thumb '+class_chosen+" "+class_last+'" src="'+url_thumb+'" alt="'+company+'" image="'+url_big+'" imagebig="'+url_lightbox+'" title="'+title+'" description="'+descr+'" /></a>');
	    item_no++;
	}
    });
    $('#thumbnails').find('img').bind('click',function(){
	url_big     = $(this).attr('image');
	url_lightbox= $(this).attr('imagebig');
	title       = $(this).attr('title');
	descr = $(this).attr('description');
	$('#big_preview').css('background','url("'+url_big+'") left top no-repeat');
	$('#big_preview').attr('href',url_lightbox);
	$('#thumbnails').find('img').removeClass('black');
	$(this).addClass('black');
	$('#title_proyecto').html(title);
	$('#description_proyecto').html(descr);
    });
}
