/**
 * Detects mobiles
 */
function isMobile() {
	
	var index = navigator.appVersion.indexOf("Mobile");
	return (index > -1);
}
function isBlackBerry() {
	var index = navigator.userAgent.indexOf("BlackBerry");
	return (index > -1);
}
function isiPad() {
	
	var index = navigator.appVersion.indexOf("iPad");
	return (index > -1);
}

/**
     * Refreshes the width of the project division (images)
     */
function refresh_projects_width_images() {
    var total_width = 0;
    for(var i = 0; i < $total_images_width.length; i++) {
        total_width += $total_images_width[i];
    }
    
    $('#project-bg-wrap').css('width', total_width + 1 + 'px');
    

}
    
/**
     * Refreshes the width of the project division
     */
function refresh_projects_width() {

    width_projects = width;
    nb_projects_screen = Math.floor(width_projects / project_width);
    $('#projects-container').css('width', width_projects +'px');

}


function opacify_images() {
    var loop_width = 0;
    var width_of_visible = 0;
    var parent_width = $('#project-bg-wrap').parent().width();
    var thumb_next = $('#thumb-next');
                
    for(var i = 0; i < $total_images_width.length; i++) {
        loop_width += $total_images_width[i];

        if(loop_width > new_margin_left && loop_width - new_margin_left < parent_width) {
            $('.project-bg:nth-child(' + (i+1) + ') img').fadeTo('fast', '1');
                        
            if(i == ($total_images_width.length-1)) {
                final_image_visible = true;
            } else {
                final_image_visible = false;
            }
                        
            width_of_visible += $total_images_width[i];
                        

            next_invisible_image = i+2;



            $(thumb_next).width(60 + (parent_width - width_of_visible));

                
                        
        } else {
            $('.project-bg:nth-child(' + (i+1) + ') img').fadeTo('fast', '0.5');
        }
                    
                    
                    
    }
                
}

    
/**
     * set up a global array which will be available for both window.load and document.ready
     */
var $total_images_width = new Array();

var next_invisible_image = 0;

var final_image_visible = false;
    
/**
     * Get the location string of the current page
     */
var current_page = window.location.href;
    
/**
     * The height of the browser
     */
var height = $(window).height();


/**
     * The width of the browser
     */
var width = $(window).width();
    
    
/**
     * Width of each project
     */

var project_width = 442;

if(current_page.match(/\/clients/)) {
    project_width = 232;
}
    
/**
     * Number of projects on each "screen"
     */
var nb_projects_screen = 0;


/**
     * The current images
     */
var current_image = 0;


/**
 * Set the new_margin_left for the scroller
 */

var new_margin_left = 0;
    
$(function() {
    if(((isMobile() && !isiPad()) || isBlackBerry()) && !current_page.match(/\/index.php/)) {
        var str = document.URL;
        
        if(current_page.match(/www.sisteris.com/))
            document.location = str.replace('www.sisteris.com/', 'm.sisteris.com/');
        else
            document.location = str.replace('sisteris.com/', 'm.sisteris.com/');
    }

    /*
     * Hide 'prev' icon for project images
     */
    $('#thumb-prev, #prev').addClass('hidden');

    

    $(window).resize(function() {
        width = $(window).width();
        height = $(window).height();
        //        fix_ie();
        refresh_projects_width();
        refresh_projects_width_images();
        
        if ( !($.browser.msie && parseInt($.browser.version, 10) < 9) ) {
            //        if ( !($.browser.msie && parseInt($.browser.version, 10) < 9) && current_page.match(/frontend_dev.php\/$/) ) {
            resize_video();
        }
        
        //        if(current_page.match(/\/project\//)) {
        opacify_images();
    //        }
        
    });


    /**
     * Load what-we-do-page specific functions
     */



    /**
        * add a span to all anchor elements in #what-we-do to make the css animation work
        */

    $('#projects h3 a.animated').each(function(){
            
        var text = $(this).text();
            
        $(this).html($('<span class="on">' + text + '</span><span class="off">' + text + '</span>'));
    });


    
    
    /**
     * Load homepage specific functions
     */


     
    /**
         * Resizes the video to fill all the screen
         */
    function resize_video() {

        var video = '#background-video, #poster';

        //        var height = $(window).height();
        //        var width = $(window).width();
        
        if(!($.browser.msie && parseInt($.browser.version, 10) < 9 )) {
        
            var ratio = width / height;

            var video_height = $('#background-video').height();
            var video_width = $('#background-video').width();
            var ratio_video = video_width / video_height;

            if (ratio <= ratio_video) {
                $(video).css('height', height+"px");
                var new_width = parseInt(height * ratio_video);

                $(video).css('width', new_width+"px");
                $(video).css('top', -height/2 +"px");
                $(video).css('left', -new_width/2 +"px");

            }
            else {

                $(video).css('width', width+"px");
                var new_height = parseInt(width * (1/ratio_video));
                $(video).css({
                    'height': 'auto'
                });
                $(video).css('top', -new_height/2 +"px");
                $(video).css('left', -width/2 +"px");

            }
        }
    }

	
	if ( $.browser.msie )  {
		setTimeout (function(){
			resize_video();
		} , 1000 );
	}
    $("video").bind("play", function() {
            resize_video();
    });

    $("video").bind("loadstart", function() {
        if ( !$.browser.msie )
            resize_video();
    });

    $("video").bind("ended", function() {
        $('body').css('background-image', "url('../images/poster.jpg')");
        $("video").fadeOut('slow', function(){
            $(".what-we-do-projects").fadeIn('slow', function() {
                $("#main-section").css({'min-width': 1324});
            });
        });

    });

    if ( $.browser.opera) {
        $('video').hide();
        $('body').addClass('not-supported');
    }
        
    if( navigator.userAgent.match(/iPad/i) != null || navigator.userAgent.match(/iPhone/i)) {
            
        if( !current_page.match(/\/blog/) )
            $('body').css('background-image', "url('../images/poster.jpg')");
        $('#background-video').hide();

            
    }
        


    /**
     * Load category-page specific functions
     */




    /**
        * Number of projects
        */
    var nb_projects = $('#projects article').length;
        
    /**
        * The width of the project division
        */


    /**
        * The left margin
        */
    var margin_left_projects = 0;



    refresh_projects_width();

    if(current_page.match(/\/category\//) || current_page.match(/\/tag\//) || current_page.match(/\/parent\//) || current_page.match(/\/client\//) || current_page.match(/\/clients/)) {
        $('#projects').css('width', nb_projects * project_width +'px');
    }

    /* Events */
    $('#next, #prev').click(function(){
        if($(this).is('#next')) {
            if(Math.abs(margin_left_projects - nb_projects_screen * project_width) >= nb_projects * project_width)
                margin_left_projects = 0;
            else
                margin_left_projects -= nb_projects_screen * project_width;
        } else {
            if(margin_left_projects >= 0)
                margin_left_projects = 0;
            else
                margin_left_projects += nb_projects_screen * project_width;
        }


        $('#projects').animate({
            'margin-left': margin_left_projects
        },
        1000,
        function() {
            if(margin_left_projects == 0)
                $('#prev').addClass('hidden');
            else
                $('#prev').removeClass('hidden');
        });

        return false;
    });



    $("article.project").hover(


        function(){
            $(this).children('h3').children('a').css('color', 'white');
            $(this).find('div.thumb').children('a').stop().animate({
                "border-left-width": "15px",
                "border-top-width": "15px",
                "width": "356px",
                "height": "140px"
            },
            150);
        },
        function(){
            $(this).children('h3').children('a').css('color', '#86643F');
            $(this).find('div.thumb').children('a').animate({
                "width": "385px",
                "height": "170px",
                "border-left-width": "0px",
                "border-top-width": "0px"
            },
            150);
        }
        );




    /**
         * Load project-page specific functions
         */




    $('.project-description-link').click(function(){
            
            


        if($('.project-desc').height() < 21) {
            var marginTop = $('.project-desc').css('margin-top');
            $('.project-desc').animate({
                'height': $('.project-content').height() - marginTop.replace('px', '')
            }, 500, function() {
                $('.project-description-link span').text('read less...');
            });
        } else {
            $('.project-desc').animate({
                'height': 20
            }, 500, function() {
                $('.project-description-link span').text('read more...');

            });
        }
        return false;
    });
// DISABLED
//    refresh_projects_width_images();
    $('.project-bg:nth-child(1) img').css('opacity', '1');
        
        
        
    $('#thumb-next').hover(function(){

        $('.project-bg:nth-child(' + next_invisible_image + ') img').css('opacity', '1');
    }, function(){
        $('.project-bg:nth-child(' + next_invisible_image + ') img').css('opacity', '0.5');
    });
    
    
    $('#thumb-next, #thumb-prev, h2.project-title a').click(function(e){
        

        

        var show_prev_icon = false;
        new_margin_left = 0;


        if(current_image == 1) {
            show_prev_icon = false;
        } else {
            show_prev_icon = true;
        }

        if($(this).is('#thumb-next')) {
            if(current_image == ($total_images_width.length - 1) || final_image_visible == true) {
                current_image = 0;
                show_prev_icon = false;
            } else {
                current_image++;
            }
            show_prev_icon = true;
        }
        else {
            if(current_image <= 0) {
                current_image = 0;
                show_prev_icon = false;
            } else {
                current_image--;
            }

        }
            
            
        if($(this).parent().hasClass('project-title')) {
            current_image = 0;
        }
            

        for(var i = 0; i < current_image; i++) {
            new_margin_left += $total_images_width[i];
        }
        
        if(current_image == 0) {
            show_prev_icon = false;
        }


        $('#project-bg-wrap').animate({
            'margin-left': '-' + (new_margin_left) +'px'
        },
        750,
        function() {
                
            opacify_images();
                
            if(current_image +1 == $total_images_width.length || final_image_visible == true) {
                $('#thumb-next').css({
                    'background-image': 'url(/images/rwd.png)'
                });
            } else {
                $('#thumb-next').css({
                    'background-image': 'url(/images/next.png)'
                });
            }

            
            if(show_prev_icon == false)
                $('#thumb-prev').addClass('hidden');
            else
                $('#thumb-prev').removeClass('hidden');
        });
            
            
        return false;
            
    });
        
        

    $('article.what-we-do-thumb').hover(function(){

        $(this).children('h3').children('a').children('.on').stop().animate({
            'margin-top': '-67px'
        });
    
    }, function() {

        $(this).children('h3').children('a').children('.on').stop().animate({
            'margin-top': '0px'
        });

    });
    
    
    
    
    
    
        $('.who-we-are-description-link').click(function(){
            

        if($('#who-we-are-container').height() < 21) {
            $('#who-we-are-container').animate({
                'height': $('.column:first-child').height()
            }, 500, function() {
                $('.who-we-are-description-link span').text('read less...');
            });
        } else {
            $('#who-we-are-container').animate({
                'height': 0
            }, 500, function() {
                $('.who-we-are-description-link span').text('read more...');
            });
        }
        return false;
    });
    
    
    
    


});

$(window).load(function() {

    if ( !current_page.match(/\/blog/) && $.browser.msie && parseInt($.browser.version, 10) < 9 ) {
        setTimeout (function(){
            $('body').css('background-image', "url('../images/poster.jpg')");
            $("video").fadeOut('slow', function(){
                $(".what-we-do-projects").fadeIn('slow');
            });
        } , 11000 );
    }
    

    var i = 0;
    
    $('.project-bg').each(function(){
        
        var childImage = $(this).children('img, iframe, object, video, div');
        
        var paddingAmount = $(childImage).css('padding-left').replace('px', '');
        
        var thisWidth = parseInt($(childImage).width()) + (parseInt(paddingAmount) * 2) + 2;
        
        width_of_all_images += thisWidth;
        $total_images_width[i] = thisWidth;
        
        i++;
        
    });
    
    
    refresh_projects_width_images();
    opacify_images();

});

var width_of_all_images = 0;
