$(document).ready(function() {
    
    bVersion = getInternetExplorerVersion();
    
    if ($("body").hasClass("frontpage")) {
        frontpage_image_resize();
        $(window).resize(frontpage_image_resize);
    }
    
    popup();
    
    simple_tooltip(".hoverNav","tooltip");
    
    counter = 1;
    totalPages = $('.testimonialsText').size();
    totalCounter = Math.ceil(totalPages / 2);
    
    showreelCounter = 1;
    totalShowreels = $('.showreelItem').size();
    
    $('.testimonialsText').hide();
    $('.testimonialsContent .testimonialsText:eq(0)').show();
    $('.testimonialsContent .testimonialsText:eq(1)').show();
    $('.testimonialsContent .testimonialsText:eq(0)').css({'padding-top':'20px','height':'235px'});
    
    $('.showreelItem').hide();
    $('.showreelItem:eq(0)').show();
    
    $('.prev').click(function(e) {
       	e.preventDefault();
       	if(counter > 1) {
			counter = counter - 1;    
			$('.testimonialsText').hide();
			newIndex1 = counter * 2;
		    newIndex2 = (counter * 2) - 1;
		    $('.testimonialsContent .testimonialsText:eq('+(newIndex1-1)+')').show();
            $('.testimonialsContent .testimonialsText:eq('+(newIndex2-1)+')').show();
       }
   });
   
   $('.next').click(function(e) {
       e.preventDefault();
       if(counter < totalCounter) {
           counter = counter + 1;
           $('.testimonialsText').hide();
           newIndex1 = counter * 2;
		   newIndex2 = (counter * 2) - 1;
           $('.testimonialsContent .testimonialsText:eq('+(newIndex1-1)+')').show();
           $('.testimonialsContent .testimonialsText:eq('+(newIndex2-1)+')').show();
       }
   });
   
   $('.showreelPrev').click(function(e) {
       	e.preventDefault();
       	if(showreelCounter > 1) {
			showreelCounter = showreelCounter - 1;    
			$('.showreelItem').hide();
			newIndex1 = showreelCounter - 1;
		    $('.showreelItem:eq('+(newIndex1)+')').show();
       }
   });
   
   $('.showreelNext').click(function(e) {
       e.preventDefault();
       if(showreelCounter < totalShowreels) {
           showreelCounter = showreelCounter + 1;
           $('.showreelItem').hide();
           newIndex1 = showreelCounter - 1;
           $('.showreelItem:eq('+(newIndex1)+')').show();
       }
   });
    
});


function simple_tooltip(target_items, name){
	 $(target_items).each(function(i){
	 	$("body").append("<div class='"+name+"' id='"+name+i+"'><p>"+$(this).attr('title')+"</p></div>");
		var my_tooltip = $("#"+name+i);
		$(this).removeAttr("title").mouseover(function(){
		my_tooltip.css({opacity:0.8, display:"none"}).fadeIn(50);
	 	}).mousemove(function(kmouse){
	 		my_tooltip.css({left:kmouse.pageX+15, top:kmouse.pageY+15});
	 	}).mouseout(function(){
	 		my_tooltip.fadeOut(50);
	 	});
	 });
}

function getInternetExplorerVersion() {
	var rv = -1;
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
		rv = parseFloat( RegExp.$1 );
	}
	return rv;
}

function popup() {
    $('a.popupLink').bind('click', function(e) { popupWindow($(this).attr('href'), $(this).attr('title').replace(/[^A-z]/ig, ''),'width=550, height=600, scrollbars'); e.preventDefault(); });
}

function popupWindow(url, name, attributes){
	var newWindow = window.open(url, name, attributes);
	newWindow.focus();
}
 
function frontpage_image_resize() {
    var frontpage_image = $("#frontPageImage img");
    
    var w = frontpage_image.attr("width");
    var h = frontpage_image.attr("height");
    var r = w/h;
    
    var window_w = $(window).width();
    var window_h = $(window).height();
    var window_r = window_w/window_h;
    
    var new_w; var new_h;
    if (window_r >= r) {
        new_w = window_w;
        new_h = window_w/r;
    } else {
        new_w = window_h*r;
        new_h = window_h;
    }
    frontpage_image.attr("width", new_w);
    frontpage_image.attr("height", new_h);
}
																				function viewHtmlOnly(status) {
	if (status == true) {
		status = 1;
	} else {
		status = 0;
	}
	document.cookie = escape('htmlOnly') + "=" + escape(status) + "; path=/";
	window.location.reload();
}
function checkIsHtmlOnly() {
	var htmlOnly = readCookie('htmlOnly');
	return htmlOnly == 1;	
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
    	var c = ca[i];
    	while (c.charAt(0)==' ') c = c.substring(1,c.length);
    	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
