$(document).ready(function() {
	$("div.wr").css("display", "none");

	$('.feedback').click(function(){
		$('#feedback').animate({top:'10px'},300).animate({top:'-10px'},100).animate({top:'-1px'},100);
		$('#feedback-name').focus();
		$('.feed a').addClass('active');
		return false							
	});	
	
	$('#feedback .close').click(function(){
		$('#feedback').animate({top:'-381px'},250);
		$('.feed a').removeClass('active');
		$('#feedback input, #feedback textarea').each (function () {
			var e = $(this);
			if (e.attr ('type') == 'submit') return;
			e.val ('');
		});
		$('#contactFields').show ();
		$('#sentHint').hide ();
		return false							
	});	
	
	$('.feed a').click(function(){
		if ($(this).hasClass('active')) {
			$(this).removeClass('active');	
			$('#feedback').animate({top:'-385px'},300);
		} else {
			$(this).addClass('active');	
			$('#feedback').animate({top:'10px'},300).animate({top:'-10px'},100).animate({top:'-1px'},100);
			$('#feedback-name').focus();
		}
		return false							
	});	
	
	$('.close').hover(function(){
	 $(this).addClass('active');
	},function(){
	 $(this).removeClass('active');
	});


	$('.gallery .popup .close').click(function(){
		$(this).parent().hide();									   
	});
	
	$('.gallery .pi a').click(function(){
		$('.gallery .popup .close').click();
		popupclass = $(this).attr('class');
		$(this).parent().parent().parent().children('.popup.'+popupclass+'-popup').show();
		return false								   
	});
	
	$('.print a').click(function(){
		window.print();
		return false						 
	});
	
	
	$(window).scroll(function(){		
		if($.browser.msie && $.browser.version == 6) {
			
		} else {
			if ($.browser.safari) {
				browsfixed = $('body').get(0).scrollTop;
			} else {
				browsfixed = $('html').get(0).scrollTop;
			
			}
				ddd = $('#wrapper').get(0).offsetLeft + 60;
			if (browsfixed > 340) 
			{$('#navi').css('position','fixed').css('top','0px').css('left',ddd)} else
			{$('#navi').css('position','absolute').css('top','350px').css('left','60px')}	
		}
	});
	
	
	$('#navi li a').hover(function(){
		if ($(this).parent().hasClass('active')) {return false}
		if($.browser.msie && $.browser.version == 6) {return false}
		$(this).parent().addClass('hover');					  
	},function(){
		$(this).parent().removeClass('hover');		
	});
	
	
	$('.pics').hover(function(){
		$(this).parent().addClass('active');					  
	},function(){
		$(this).parent().removeClass('active');		
	});
	
	if ($.browser.msie && $.browser.version == 6) {
		$('.menu td.active strong').hide();
		$('.menu td.active.nochild strong').show();
	}
	
	$('.parallax .bg div').hover(function(){
		$(this).children('div.link').stop().animate({width:100+'%'},500);
	},function(){
		$(this).children('div.link').stop().animate({width:0},500);
	});
	
	
	maxheight = 0;
	$('.portfolio-gallery .one').each(function(){
		 if ($(this).height() > maxheight) {maxheight = $(this).height();}
	});
	$('.portfolio-gallery .one').each(function(){
		$(this).height(maxheight+"px");
	});
	
	
	
	

	$('.menu .m').hover(function(){
		if ($(this).parent().hasClass('active')) {return false} else {
			$(this).children('.sub').show();
			if (!$(this).parent().hasClass('nochild')) {
				
				$('.menu td.active .bg, .menu td.active .sub').hide();	
			}
			
			if ($.browser.msie && $.browser.version == 6) {
				return false
			} else {
				$(this).addClass('hovered');
				$(this).parent().prev().children('.m').addClass('prev-act');		
			}
		}
	},function(){
		if ($(this).parent().hasClass('active')) {return false}	else {
			$(this).children('.sub').hide();
			if (!$(this).parent().hasClass('nochild')) {
				$('.menu td.active .bg, .menu td.active .sub').show();	
			}
			if ($.browser.msie && $.browser.version == 6) {
				return false
			} else {
				$(this).removeClass('hovered');
				$(this).parent().prev().children('.m').removeClass('prev-act');
			}
		}
	});

	
	subs();
	var	mar = str_replace('px','',$('.pic').css('margin-right'))-0;
	indexpic(mar);
	$(window).resize(function(){
		subs();
		indexpic(mar);
	});
	
	$('.parallax .links a').click(function(){
		$('.parallax .links').removeClass('active');
		$(this).parent().addClass('active');
		return false
	});
	$('.popup .close').click(function(){
		$(this).parent().parent().removeClass('active');
		return false
	});
	
	$('.list ol li a.collapse').click(function(){
		if ($(this).parent().hasClass('active')) {
			if ($.browser.msie) {
				$(this).parent().children('.wr').hide();	
				$(this).parent().removeClass('active');
			} else {
				$(this).parent().children('.wr').slideUp(300,function(){
					$(this).parent().removeClass('active')													  
				});		
			}
		} else {
			if ($.browser.msie) {
				$(this).parent().children('.wr').show();	
				$(this).parent().addClass('active');
			} else {
				$(this).parent().children('.wr').slideDown(300);
				$(this).parent().addClass('active');
			}
		}
		return false					
	});
	
	if($.browser.safari ) {
		windowwidth = $(window).width();
		$('.parallax .bg').css('left',windowwidth/2);			
	}

	
	$('.area-right').hover(function(){
		windowwidth = $(window).width();
		$('.parallax .bg').stop().animate({left:-1920+windowwidth+'px', marginLeft:0},2000);	
		},function(){
		$('.parallax .bg').stop();
	});
			
	$('.area-left').hover(function(){
		$('.parallax .bg').stop().animate({left:0, marginLeft:0},2000);	
		},function(){
		$('.parallax .bg').stop();
	});
	
	
	/*Scripts for main! Nothing after!*/
	if ($('body').hasClass('index')){return false} else {
		$('#logo').hover(function(){
			$(this).addClass('active');	
			$('.ico-home').addClass('active');			
		},function(){
			$(this).removeClass('active');	
			$('.ico-home').removeClass('active');		
		});
		$('.ico-home').hover(function(){
			$(this).addClass('active');	
			$('#logo').addClass('active');			
		},function(){
			$(this).removeClass('active');	
			$('#logo').removeClass('active');		
		});	
	}
	
	
	
}); 

function subs(){
	$('.menu div.m .bg div').each(function(){
		wi = $(this).parent().parent().innerWidth();
		minus = $(this).parent().next('.sub').innerWidth();
		$(this).css('width',wi-minus-26);
	});
}

function indexpic(mar){
	if ($(window).width() < 1280) {
		$('.pic').css('margin-right',mar+'px')	
	} else {		
		$('.pic').css('margin-right',mar+100+'px')	
	}
}



function str_replace(search, replace, subject, count) {
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
 
    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}