/**
* Grunnarbeid Javascript functions
*/
grWP = $('#wbPath').html();

$(function(){
    overNewsThumb();
    loadFeed();
    showImageSlide(); 
    removeDivsNews();  
    checkHash(); 
});

function checkHash() {
    
    var hashes = window.location.hash;  
        hashes = hashes.substr(1);  
    var hash = hashes.split('/');
    
    if(typeof(hashes) != 'undefined' && typeof(hash[0]) != 'undefined' && hash[0] != '') {
        
        if(hash[0] == 7) 
        {
            typePage = 'contacts';
            setTimeout(function()	{
						            	$("html, body").animate({ scrollTop: 625 }, 1500);
						            }, 2500
					  );
        } 
        else 
        {
            typePage = 'pages/'+hash[0];
        }
        
        // check to see if we have submenu
        if(hash[1] != '' && typeof(hash[1]) != 'undefined') 
        {
            submenu = grWP+'/get_submenu/'+hash[1]+'/ #container';
        }
        else 
        {
            submenu = 'hideSub';
        }
        
        
         // load the content
        loadPiece(grWP+'/'+typePage+'/ #container', submenu, hash[2]);   
        
        // set the current menu
        $('.menu li').removeClass('current');
        $('.menu').find('a[href="#'+hash[0]+'/'+hash[1]+'/"]').parents('li').addClass('current');
        
        // this is for the thid param
        if(hash[2] != '') {
            $('.list_projects li').removeClass('current');
            //$('.list_projects').find('a[id="'+hash[2]+'"]').parents('li').addClass('current').prepend('<div class="arrow"></div>');
        }
    }
}
function loadPieceSocial(href) {
    
    closeOpenBoxes();
    //$('.socialMedia').children('img').removeClass('active');
    
    if(href=='t') { media = 'twitter_click'; } else { media = 'facebook_click'; }
    
    f = $('#'+media+'_feed'); 
    f_img = $('#'+media).children('img'); 
    
    if(activeFeedIcon != media) {
        f_img.addClass('active');
        f.parent().parent().css({'z-index':'991'}).children().children().stop().animate({'marginLeft':'118px'},500,"linear");
    } else {
        f_img.removeClass('active');
    }
    /*
    $('.socialMedia').each(function(){$(this).children().removeClass('active')});
    closeOpenBoxes();
    
    if(href=='t' && $('#twitter_click img').hasClass('active') == false) {
        //$('#twitter_click').children().addClass('active');
        $('.f_container_t #container').children().animate({marginLeft:'118px'},500,'linear');
    }
    
    if(href=='f' && $('#facebook_click img').hasClass('active') == false) {
        //$('#twitter_click').children().addClass('active');
        $('.f_container_f #container').children().animate({marginLeft:'118px'},500,'linear');
    }*/
    
    // old
    /*
    if($('#twitter_click img').hasClass('active')) {
        closeOpenBoxes();    
    } else {
        closeOpenBoxes();
        $('.f_container_325').load(href,{},function(){
            Cufon.refresh();
            $('#container').children().animate({marginLeft:'118px'},500,'linear');
        });  
    } */ 
}

function loadPiece(href,hrefBox,contentId) 
{
//	if(hrefBox.indexOf('get_submenu') != -1)
//	{	
//		$("html, body").animate({ scrollTop: 180 }, 1500);
//	}

    if(hrefBox != 'noSub') {
        // close opened boxes
        closeOpenBoxes();    
    }
    else
	{
    	$("html, body").animate({ scrollTop: 625 }, 1500);
	}

    $('.content_loader').stop(true,true).fadeIn(100);
    $('#content').load(href,{},function(responseText, status){

        // remove the stufs from the admin
        $('#content').find('.icons_ed, .icons_sc, input[type=hidden], .ui-resizable-handle, .show_icons_ed_up_del').remove()
        
        Cufon.refresh();
        $('.content_loader').delay(1000).fadeOut(500);
        loagAgain();  
    });
    
    if(hrefBox != 'noSub' && hrefBox != 'hideSub') {
        $('.f_container_325').load(hrefBox,{},function(){
            Cufon.refresh();
            
            if(typeof(contentId) != 'undefined') {
                $('li.suCoId_'+contentId).addClass('current').append('<div class="arrow"></div>');
            }
            
            $(this).css({'z-index':'991'});
            $('#container').animate({marginLeft:'21px'},500,'linear', function(){
                loagAgain();
                
                scrollbar();
            });
        });
    }
}

function closeOpenBoxes() { 
    
    activeFeedIcon = 0;
    $('.socialMedia').each(function(){
        if($(this).children().hasClass('active')) {
            activeFeedIcon = $(this).attr('id');
            $(this).children().removeClass('active');    
        }  
    });
    
    $('.f_container_325').each(function(){
        $(this).children().css({'z-index':'990'}).animate({marginLeft:'380px'},500,'linear');
    });    
    $('.f_container_t').each(function(){
        $(this).css({'z-index':'990'}).children().children().animate({marginLeft:'380px'},500,'linear');    
    });
    $('.f_container_f').each(function(){
        $(this).css({'z-index':'990'}).children().children().animate({marginLeft:'380px'},500,'linear');    
    });
}

function loagAgain() {
    changeListProjects();
    liCurrentProject();
    liCurrentSubmenu();
    overNewsThumb();
    overPictures();
    clickNewsItem(); 
    removeDivsNews();
}

function changeListProjects() {
        
    $('.in_progress_projects').click(function() {
        
        $(this).removeClass('inactiv_button').siblings('.active_projects').addClass('inactiv_button');
        $('.ap_list').fadeOut(500, function(){            
            $('.ipp_list').fadeIn(500, function(){
                scrollbar('.ipp_list');
            });   
        });           
    }); 
    
    $('.active_projects').click(function() {
        
        $(this).removeClass('inactiv_button').siblings('.in_progress_projects').addClass('inactiv_button');
        $('.ipp_list').fadeOut(500, function(){
            $('.ap_list').fadeIn(500, function() {
                scrollbar('.ap_list');
            });     
        });     
    }); 
}

function liCurrentProject() {
    $('.list_projects li a').click(function(){
    	
        $('.list_projects li').each(function(){
            if($(this).hasClass('current')) {
                $(this).removeClass('current').children('.arrow').remove();        
            }                      
        }); 
        $(this).parent().addClass('current').append('<div class="arrow"></div>');  
        scrollDownBody();   
    });
}

function liCurrentSubmenu() {
	
//	console.log('liCurrentSubmenu()');
	
    $('.submenu ul li a').click(function() {
    	
        $('.submenu ul li').each(function(){
            if($(this).hasClass('current')) {
                $(this).removeClass('current');
            }    
        });
        $(this).parents('li').addClass('current');
        scrollDownBody();
    });
}

function overNewsThumb() {
    $('.thumb').click(function(){  
        
        var newsId = $('ul.homePage li').index($(this).parent());  
        //var newsNb = newsId.substr((newsId.length), newsId.length); 
         //$('.list_news li').each(function(){
//            if($(this).hasClass('current')) {
//                $(this).removeClass('current').children('.arrow').remove();        
//            }                      
//        });        
        $('.list_news li:eq('+newsId+')').addClass('current').append('<div class="arrow"></div>'); 
    });
    
    th_title_width = $('.th_title').width()+100;
    $('.thumb').hover(function(){
        th_title = $(this).children().children('.th_title');
        pattern_dots = $(this).children().children().children('img');
        
        th_title.stop().animate({left:'-'+th_title_width +'px'},500,'linear');
        pattern_dots.stop().animate({opacity:'1'},500,'linear');    
    }, function(){
        th_title.stop().animate({left:'0'},500,'linear');
        pattern_dots.stop().animate({opacity:'0.5'},500,'linear');      
    });
}

function overPictures() {
    $('.dImageBox').hover(function(){
        th_titleR = $(this).find('.th_titleR');
        th_titleR.stop().animate({right:'-'+th_titleR.width()+'px'},500,'linear');    
    }, function(){
        th_titleR.stop().animate({right:'0'},500,'linear');
    });
}

function clickNewsItem() 
{
//	console.log('clickNewsItem()');
	
    $('.list_news li a').click(function(){
        $('.list_news li').each(function(){
            if($(this).hasClass('current')) {
                $(this).removeClass('current').children('.arrow').remove();        
            }                      
        });        
        $(this).parent().addClass('current').append('<div class="arrow"></div>');
//        scrollDownBody();         
    });
}

function scrollDownBody() {
    if(window.screen.availHeight <= 768) {
        $("html, body").animate({ scrollTop: 180 }, 1500);    
    } 
}

    
function loadFeed() {
    
    $('.f_container_t').load('socialmedia/twitter/',{},function(){
        Cufon.refresh();
    });
    $('.f_container_f').load('socialmedia/facebook/',{},function(){
        Cufon.refresh();
    });    
}

function showImageSlide() {

    $('.se_bare_bildet a').click(function(){
        
        thisSeBare = $(this);
        closeOpenBoxes();
        aId = $(this).attr('class');
        if($(this).hasClass('activate')) {     
            $('.mp_right_bg').stop(true,true).animate({opacity:0},500,'linear');
            $(this).removeClass('activate').addClass('inactivate').stop(true,true).fadeOut(500, function(){
                $(this).hide();
                thisSeBare.html('<img src="'+grWP+'/img/frontend/se_bare_bildet_icon_activ.png" alt="STILBAKE" /> TILBAKE');
                Cufon.refresh();    
                thisSeBare.fadeIn(500);
                clearInterval(intervalID);
            })
        } else {
            $('.mp_right_bg').stop(true,true).animate({opacity:1},500,'linear');
            $(this).removeClass('inactivate').addClass('activate').stop(true,true).fadeOut(500, function(){
                $(this).hide();
                thisSeBare.html('<img src="'+grWP+'/img/frontend/se_bare_bildet_icon.png" alt="SE BARE BILDET" /> SE BARE BILDET');
                Cufon.refresh();
                thisSeBare.fadeIn(500); 
                intervalID = setInterval(slideImage, timeoutInterval);   
            })
                   
        }  
    });
}

function removeDivsNews() {
//    $('.homePage li').each(function()
//    {
//        newsHtml = $(this).find('.view_content_content').html();
//        
//        console.log('view_content_content: ' + newsHtml);
//        
//        $(this).find('.text').html(newsHtml);
//        $(this).find('.text p:first-child').css({'padding-top':'0px'});    
//    });
}
(function($) {
    $.fn.customFadeIn = function(speed, callback) {
        $(this).fadeIn(speed, function() {
            if(!$.support.opacity)
                $(this).get(0).style.removeAttribute('filter');
            if(callback != undefined)
                callback();
        });
    };
    $.fn.customFadeOut = function(speed, callback) {
        $(this).fadeOut(speed, function() {
            if(!$.support.opacity)
                $(this).get(0).style.removeAttribute('filter');
            if(callback != undefined)
                callback();
        });
    };
    $.fn.customFadeTo = function(speed,to,callback) {
        return this.animate({opacity: to}, speed, function() {
            if (to == 1 && jQuery.browser.msie)
                this.style.removeAttribute('filter');
            if (jQuery.isFunction(callback))
                callback();
        });
    };
})(jQuery);
