$(document).ready(function(){
	//initDetectMobile();
	initCufon();
	initLinks();
	initScrollTo();
	initWork();
	initHomeSlider();
	initMenu();
	initProject();
});

function initCufon() {
	Cufon.replace('#header div.topbar h2, #top-content div.case p, #main div.text-block p span, #main div.slide-block p,  #main div.team-item h4, #content div.service-item h4, #work-container div.item p, #content div.related div.item p, #content div.subtitle h3, #content div.testimonial p,#block-block-7 h2' , {fontFamily: 'Mrs Eaves'});
	Cufon.replace('#header div.topbar ul.menu li a, #menu ul.menu li a, #main ul.list li,#block-block-7 h3' , {fontFamily: 'Knockout H31' , hover:true, letterSpacing:'1px'});
	Cufon.replace('#main div.slide-block a.more, #main div.slide-block h3, #footer div.menu li a, #block-menu-menu-footer-menu a, #work-container div.item h3, #content div.related div.item h3' , {fontFamily: 'Knockout H31' , hover:true, letterSpacing:'0.5px'});
//	Cufon.replace('#header div.topbar div.block-campaignmonitor h2' , {fontFamily: 'Knockout H31', letterSpacing:'1px'});
	Cufon.replace('#top-content div.case h2, #main div.twitter h2,#main div.recent-blog h2,#main div.post div.tags, #top-content div.case a.more, #main div.text-block a.more, #main div.post ul.links li a.more, #top-content ul.filter li a, #top-content ul.navigation li a, #content a.visit, div.post div.sidebar h4, div.comment div.author' , {fontFamily: 'Knockout H32', hover:true});
	Cufon.replace('#main div.text-block strong.author, #main div.team-item h3, div.content-right h4, #content div.service-item h3, #content div.contact h3,#content div.project h4,#content h4, #main label,#content ul.awards h4' , {fontFamily: 'Knockout H32'});	
	Cufon.replace('#main div.team-item h4,#content div.service-item h4' , {fontFamily: 'Mrs Eaves'});
	Cufon.replace('#main div.slide-block h2, #main div.post h3, #main div.title-page h2, #main div.post-header h2, #main div.twitter strong.name, #content div.project h2,#content h2,#content div.content-right h2, #sidebar-right div.block h2, div.post div.sidebar h2, #comments h2' , 
				{fontFamily: 'Knockout H29', 
					hover:true,
					onAfterReplace: function() {
						initTitles();
					}
				});
	Cufon.replace('#top-content div.case h3' , 
				{fontFamily: 'Knockout H29', 
					hover:true,
					onAfterReplace: function() {
						initCaseStydy();
					}
				});
}

function initLinks() {
	var links = $('a[rel*="external"]');
	$.each(links, function(index, item) {
		$(item).attr('target','_self');
	});
	
	var links = $('a[href*="http://"]');
	$.each(links, function(index, item) {
		$(item).attr('target','_self');
	});
}

function initTitles() {
	if($('div.title-page').get(0)) {
		$('div.title-page').each(function(index) {
			var $this = $(this);
			var title_wrapper_width = $this.width();
			var title_width = $this.find('h2').width();
			var span_width = parseInt((parseInt(title_wrapper_width)-title_width)/2);
			span_width = span_width-10;
			$this.find('span.border').width(span_width);
		});
	}
}

var initedCaseStydy = 0;
function initCaseStydy() {
	if($('#slider').get(0)) {
		var countCaseStydy = $('#slider div.item').size();
		initedCaseStydy += 1;
		if(initedCaseStydy<countCaseStydy) return;
		var slider_height = 227;
		$('#slider div.item').each(function(i) {
			$this = $(this);
			var height = $this.height();
			var diff = slider_height - height;
			if(diff>30) {
				var top = diff/2-17;
				$this.css('margin-top', top+'px');
			}
		});
	}
}

function initScrollTo() {
	$('#content div.sidebar-left a[rel*="node"]').click(function() {
		var scrollTo_id = $(this).attr('rel');
		$.scrollTo($('#'+scrollTo_id), 500);
		return false;
	});
}

function initWork() {
	//init filter
	if($('#block-bcadet-work_filter').get(0)) {	
		$('ul.filter a').click(function() {
			//content restore
			$('#work-container div.item').removeClass('hidden');
			$('#work-container div.item').removeClass('visible');
			
			//tabs init
			var $this = $(this);
			if($this.hasClass('active')) {
				$this.removeClass('active');
			}
			else {
				$('ul.filter a[rel="item"]').removeClass('active');
				if($this.attr('rel')=='item') {
					$('ul.filter .active').removeClass('active');
				}
				$this.addClass('active');
			}
			Cufon.replace('#top-content ul.filter li a' , {fontFamily: 'Knockout H32', hover:true});
			
			//content init
			$('ul.filter li a.active').each(function() {
				var filter = $(this).attr('rel');
				$('#work-container div.item').each(function() {
					if(!$(this).hasClass(filter)) {
						if(!$(this).hasClass('visible')) {
							$(this).addClass('hidden');	
						}
					} else {
						$(this).removeClass('hidden');
						$(this).addClass('visible');
					}
				});
			});
			
			if(!$('ul.filter li a.active').get(0)) {
				$('#work-container div.item').removeClass('hidden');
				$('#work-container div.item').addClass('visible');
			}
			
			//content animate
			$('#work-container > div[class*=visible]').fadeIn('slow');
			$('#work-container > div[class*=hidden]').fadeOut('normal');
			return false;
		});
	}
	
	//init cufon hover
	$('#content div.related div.item').hover(
		function() {
			Cufon.replace('#content div.related div.item h3' , {fontFamily: 'Knockout H31' , hover:true, letterSpacing:'0.5px'});
			$(this).find('div.item-inner').stop(true, true).animate({ backgroundColor: "#c8c8b9" }, 'normal');
		},
		function() {
			Cufon.replace('#content div.related div.item h3' , {fontFamily: 'Knockout H31' , hover:true, letterSpacing:'0.5px', color:'#598A98'});
			$(this).find('div.item-inner').stop(true, true).animate({ backgroundColor: "#232420" }, 'normal');
		}
	);
	
	$('#work-container div.item').hover(
		function() {
			var index = $('div.item').index($(this));
			Cufon.replace('div.item h3:eq('+index+')' , {fontFamily: 'Knockout H31' , hover:true, letterSpacing:'0.5px'});
			$(this).find('div.item-inner').stop(true, true).animate({ backgroundColor: "#c8c8b9" }, 'normal');
		},
		function() {
			var index = $('div.item').index($(this));
			Cufon.replace('div.item h3:eq('+index+')' , {fontFamily: 'Knockout H31' , hover:true, letterSpacing:'0.5px', color:'#598A98'});
			$(this).find('div.item-inner').stop(true, true).animate({ backgroundColor: "#232420" }, 'normal');
		}
	);
	
	$('#content div.related div.item, #work-container div.item').click(function() {
		var href = $(this).find('h3 a').attr('href');
		window.location = href;
	});
	
}


var in_motion_img = 0;
var in_motion_txt = 0;

function initHomeSlider() {
	//Top slider
	if($('#slider').get(0)) {
		//image slider
		$('#visual').cycle({  
		    speed: 'slow',
		    timeout: 0,
		    before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
				in_motion_img = 1;
				return false;
			},
			after: function(currSlideElement, nextSlideElement, options, forwardFlag) {	
				if(in_motion_txt!=1) {
					var index = $('#visual > img').index(nextSlideElement);
		    		$('#slider').cycle(index);
				}
				in_motion_img = 0;
				in_motion_txt = 0;
				return false;  
			}
		});
		
		//text slider
		$('#slider').cycle({  
		    speed: 'slow',
			fx: 'scrollHorz',
		    timeout: 5000,
		    next: $('div.case a.next'),  		       
			prev: $('div.case a.previous'),
		    before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
				var index = $('#slider > div.item').index(nextSlideElement);
				//subtitle animate
				if(forwardFlag==true) {
					$('#slider div.item:eq('+index+') p').css('marginLeft', '339px');
				}
				else {
					$('#slider div.item:eq('+index+') p').css('marginLeft', '-339px');
				}
				$('#slider div.item:eq('+index+') p').animate({'marginLeft': '0'}, 1000);

				
				//link
				var $link = $('#slider div.item:eq('+index+') a.read');
				$('#block-bcadet-home_slider a.more').attr('href', $link.attr('href'));
				$('#block-bcadet-home_slider a.more').text($link.text());
				Cufon.replace('#top-content div.case a.more' , {fontFamily: 'Knockout H32', hover:true});
				
				//type
				var type = $('#slider div.item:eq('+index+') div.type').text();
				$('#block-bcadet-home_slider div.case h2').text(type);
				Cufon.replace('#top-content div.case h2' , {fontFamily: 'Knockout H32', hover:true});
				
				in_motion_txt = 1;
				return false;
			},
			after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
				if(in_motion_img!=1) {
					var index = $('#slider > div.item').index(nextSlideElement);
					//slide image
					$('#visual').cycle(index);
				}
				in_motion_img = 0;
				in_motion_txt = 0;
				return false;
			}
		});
	}
	
	//Content sliders
	$('#block-bcadet-home_whats_new ul.slider-body').jcarousel({
		scroll: 1, 
		wrap: 'circular',
		initCallback: whats_new_initCallback,
		buttonNextHTML: null,
        buttonPrevHTML: null
	});	
	/**
	* We use the initCallback callback
	* to assign functionality to the controls
	*/
	function whats_new_initCallback(carousel) {	
		$('#block-bcadet-home_whats_new div.slider').append('<a class="jcarousel-prev" href="#">Prev</a>')
		$('#block-bcadet-home_whats_new div.slider').append('<a class="jcarousel-next" href="#">Next</a>');
		$('#block-bcadet-home_whats_new div.slider a.jcarousel-next').bind('click', function() {
		    carousel.next();
			setTimeout ( "fixCufon()", 50 );
		    return false;
		});	
		$('#block-bcadet-home_whats_new div.slider a.jcarousel-prev').bind('click', function() {
		    carousel.prev();
			setTimeout ( "fixCufon()", 50 );
		    return false;
		});
	};
	
	
	$('#block-bcadet-home_services ul.slider-body').jcarousel({
		scroll: 1, 
		wrap: 'circular',
		initCallback: home_services_initCallback,
		buttonNextHTML: null,
        buttonPrevHTML: null
	});
	/**
	* We use the initCallback callback
	* to assign functionality to the controls
	*/
	function home_services_initCallback(carousel) {	
		$('#block-bcadet-home_services div.slider').append('<a class="jcarousel-prev" href="#">Prev</a>')
		$('#block-bcadet-home_services div.slider').append('<a class="jcarousel-next" href="#">Next</a>');
		$('#block-bcadet-home_services div.slider a.jcarousel-next').bind('click', function() {
		    carousel.next();
			setTimeout ( "fixCufon()", 50 );
		    return false;
		});	
		$('#block-bcadet-home_services div.slider a.jcarousel-prev').bind('click', function() {
		    carousel.prev();
			setTimeout ( "fixCufon()", 50 );
		    return false;
		});
	};
	
	
	
	$('#block-bcadet-home_whats_new ul.slider-body li, #block-bcadet-home_services ul.slider-body li').click(function() {
		var href = $(this).find('h3 a').attr('href');
		if(href.indexOf('http')!==-1) {
			window.open(href,'_newtab');
			return false;
		}
		else {
			window.location = href;
			return false;
		}
	});
	
}

function strpos( haystack, needle, offset) {
    var i = (haystack+'').indexOf( needle, offset ); 
    return i===-1 ? false : i;
}

function fixCufon() {
	Cufon.replace('#main div.slide-block h3', {fontFamily: 'Knockout H31' , hover:true, letterSpacing:'0.5px'});
	Cufon.replace('#main div.slide-block p' , {fontFamily: 'Mrs Eaves'});
}

function initMenu() {
	$('#menu ul.menu li').click(function(){
		var href = $(this).find('a').attr('href');
		window.location.href = href;
	});
	
	//init cufon hover
	$('#menu ul.menu li').hover(
		function() {
			Cufon.replace('#header #menu ul.menu li a' , {fontFamily: 'Knockout H31' , hover:true, letterSpacing:'1px'});
		},
		function() {
			Cufon.replace('#header #menu ul.menu li a' , {fontFamily: 'Knockout H31' , hover:true, letterSpacing:'1px'});
		}
	);
}

function initProject() {
	if($('div.project div.main-image').get(0)) {
		
		var top = $('div.project div.image-wrapper').css('top');
		if(top=='auto') top = 0;
		
		//init wrapper css
		var width = $('div.project div.image-wrapper').css('width');
		$('div.project div.image-wrapper').css({
			'top': 'auto',
			'position': 'relative',
			'width':$('div.project div.image-wrapper img').width()
		});
		//init image css
		$('div.project div.image-wrapper img').css({
			'top': top,
			'position': 'absolute'
		});
		//add mask
		$('div.project div.image-wrapper').append('<div class="mask"></div>');
		//init mask css
		$('div.project div.image-wrapper div.mask').css({
			'top': 0,
			'left': 0,
			'width':$('div.project div.image-wrapper').width(),
			'height':$('div.project div.image-wrapper ').height(),
			'position': 'absolute'
		});
		
		//init view link
		$('div.project div.image-wrapper').append('<a href="#view" class="view">View</a>');
		var $link = $('div.project div.image-wrapper a');
		$link.click(function() {
			if($link.attr('class')=='view') {
				actionProjectImage('view');
				
			}
			else {
				actionProjectImage('hide');
			}
			return false;
		});
		
	}
}

var collapced_top = 0;
var collapced_height = 0;

function actionProjectImage(action) {
	var $link = $('div.project div.image-wrapper a');
	$link.attr('class', 'hidden');
	$link.attr('href', '#hidden');	
	if(action=='view') {		
		collapced_height = $('div.project div.image-wrapper').height();
		collapced_top = $('div.project div.image-wrapper img').css('top');
		$('div.project div.image-wrapper div.mask').css('display', 'none');
		var height = $('div.project div.image-wrapper img').height();
		$('div.project div.image-wrapper img').animate({'top': 0}, 'slow');
		$('div.project div.image-wrapper').animate({'height': height}, 'slow',function() {
			$('div.project div.image-wrapper').css('overflow', 'visible');
			$link.attr('class', 'hide');
			$link.attr('href', '#hide');
		});
	}
	else {
		$('div.project div.image-wrapper').css('overflow', 'hidden');
		$('div.project div.image-wrapper').animate({'height': collapced_height}, 'slow');
		$('div.project div.image-wrapper img').animate({'top': collapced_top},'slow',function() {
				$('div.project div.image-wrapper div.mask').css('display', 'block');
				$link.attr('class', 'view');
				$link.attr('href', '#view');
		});
	}
}

/*function initDetectMobile(){
	var width = screen.width;
	if (screen.width<=768)
	{
 		window.location="/mobile";
	}
}*/
