$(function(){
	// fix background argb for IE < 8
	if ($.browser.msie && parseInt($.browser.version) < 9) {
		var bg = $('.gallery_images').css('background-color'),
			red = toHex(bg[0]),
			green = toHex(bg[1]),
			blue = toHex(bg[2]),
			alpha = toHex(bg[3]*255);
		$('#.gallery_images, .content').css('background-color', '#' + red + green + blue);
	}
	
	$('.sub-menu').each(function(){
		var cnt = $('.menu-item', this).size();
		$(this).css('top', -(cnt * 35 ));
	});
	
	$('.sub-menu a').each(function(){
		var $this = $(this);
		$this.html($this.html().replace(' ', '&nbsp;'));
	});
	
	$('.galleries').each(function(){
		var $this = $(this);
		$this.data('width', $this.width());
	});
	
	if ($('.header_gal').hasClass('off')) {
		var $g = $('.galleries');
		$g.css('marginLeft', -$g.data('width'));
	};
	
	$('.galleries_switch').click(function(){
		var p = $(this).parent(),
			gals = $('.galleries'),
			glist = $('.gallery_list').toArray();
		if (p.hasClass('on')){
			gals.animate({
				marginLeft: -gals.data('width')
			}, 1000);
			p.switchClass('on', 'off', 1000);
			// var _h1 = setInterval(function(){
			// 	if (glist.length <= 0) {
			// 		clearInterval(_h1);
			// 	};
			// 	$(glist.pop()).hide();
			// }, 200);
		}else{
			gals.animate({
				marginLeft: 0
			}, 1000);
			// var _h2 = setInterval(function(){
			// 	if (glist.length <= 0) {
			// 		clearInterval(_h2);
			// 	};
			// 	$(glist.shift()).show();
			// }, 200);
			p.switchClass('off', 'on', 0);
		}
	});
	
	if ($('.header_images.off').size() > 0){
		$('.gallery_images').css('height', '0%');
	}
	
	$('.gallery_image_btn').click(function(){
		if ($(this).parent().hasClass('on')){
			$('.gallery_images').animate({
				opacity: 0,
				height: '0'
			}, 'slow', function(){
				$('.header_images').switchClass('on', 'off', 1000);
			});
		}else{
			$('.header_images').switchClass('off', 'on', 1000, function(){
				$('.gallery_images').animate({
					opacity: 1,
					height: $(window).height()
				}, 'slow', function(){
					$('.gallery_images').get(0).style.height = '100%';
				});
			});
		}
		return false;
	});
	
	$('input[type=text], textarea').each(function(){
		var _v = $(this).data('def'),
			$this = $(this);
		if ($this.val() == '') {
			$this.val(_v);
		};
		$this.focus(function(){
			if ($this.val() == _v){
				$this.val('');
			}
		}).blur(function(){
			if ($this.val() == ''){
				$this.val(_v);
			}
		});
	});
	
	$('.content').each(function(){
		content_size($(this));
	});
	$(window).resize(function(){
		content_size($('.content'));
	});
	
	// thumbs slide
	var start_slide = $.cookie('slideshow-index') || 1;
	if (typeof slides_array != 'undefined'){
		if (start_slide > slides_array.length){
			start_slide = slides_array.length;
		}
		$.supersized({
			slideshow               : 1,
			autoplay                : 0,
			start_slide             : start_slide,
			stop_loop               : 0,
			random                  : 0,
			slide_interval          : slide_interval,
			transition              : 1,
			transition_speed        : 1000,
			new_window              : 1,
			pause_hover             : 0,
			keyboard_nav            : 1,
			performance             : 2,
			image_protect           : 1,

			min_width               : 0,
			min_height              : 0,
			vertical_center         : 1,
			horizontal_center       : 1,
			fit_always              : 0,
			fit_portrait            : 1,
			fit_landscape           : 0,

			slide_links             : 'blank',
			thumb_links             : 1,
			thumbnail_navigation    : 1,
			slides                  :  slides_array,

			progress_bar            : 1,
			mouse_scrub             : 0
		});
	};
	
	$('#thumb-list').wrap('<div id="thumbs-scroller" />');
	$('#thumbs-scroller').height($(window).height() - 90)
		.mousemove(function(e){
			var y = e.pageY - $(window).scrollTop() - 40,
				h = $(this).height(),
				p = y / h,
				tlist = $('#thumb-list'),
				tl_h = (tlist.height() - h);
			if (tl_h > 0) {
				tlist.stop().animate({
					top: -(tl_h * p)
				}, 500, 'easeOutBack');
			}else{
				tlist.stop().animate({
					top: 0
				}, 500, 'easeOutBack');
			}
		});
	
	$('#pauseplay').click(function(){
		setTimeout(function(){
			$.cookie('slideshow-state', (vars.is_paused)?0:1, { expires: 100, path: '/' });
		}, 500);
	})
	
	//content toggler
	if ($('.content').size() > 0){
		$('.content_toggle').show().click(function(){
			var $t = $(this);
			if ($t.hasClass('closed')){
				$('.content').slideDown('slow');
				$t.removeClass('closed');
			}else{
				var ai = $('.activeslide img'),
					iw = parseInt(ai.css('width')),
					ih = parseInt(ai.css('height'));
				if (ih < iw){
					ai.css('width', '100%');
				};
				$('.content').slideUp('slow');
				$t.addClass('closed');
			}
		});
	}
	
	//contact form
	$('#contact_form').submit(function(){
		var form = this,
			fields = [
				{
					name: 'name',
					message: 'Write Your Name',
					def: 'Name',
					gr: 'e'
				},{
					name: 'email',
					message: 'Write Your E-Mail',
					def: 'E-Mail',
					gr: 'e'
				},{
					name: 'subject',
					message: 'Write Subject',
					def: 'Subject',
					gr: 'e'
				},{
					name: 'message',
					message: 'Write Message',
					def: 'Message'
				}
			];
		
		res = check_form(this, fields);
		
		if (res) {
			//send form
			var form_data = $(this).serializeArray();
			form_data.push({
				name: 'action',
				value: 'dev_contact_form'
			});
			$.post(post_url, form_data, function(data){
				if (data.error == ''){
					alert(data.message);
					form.reset();
				}else{
					alert(data.error);
				}
			}, 'json');
			return false;
		};
		
		return res;
	});
	
	// comment form
	$('.comment-post-btn').click(function(){
		$('#respond').submit();
		
		return false;
	});
});

function content_size($this){
	var logo_size = $('.logo').height() || 90,
		top_offset = (logo_size + 20),
		$win = $(window);
	if (($win.height() - top_offset - 85) < $this.height()) {
		$this.addClass('content_to_top');
		$this.css('top', top_offset + 'px');
	}else{
		$this.removeClass('content_to_top');
		$this.css('top', '');
	}
	
	// slider scroll
	$('#thumbs-scroller').height($win.height() - 0);
}

function check_form(self, fields){
	var res = true;
	
	var scroll = true;
	for(ind in fields){
		var field = fields[ind];
		var f = $('*[name="'+field.name+'"]', self);
		switch (f.attr('type')){
			case 'checkbox':
				if (f.filter(':checked').length === 0) {
					f.last().tipsy({
						gravity: 'nw',
						fallback: field.message,
						trigger: 'manual'
					}).tipsy('show');
					if (scroll) {
						f.last().focus();
						scroll = false;
					};
					res = false;
				}else{
					f.last().tipsy('hide');
				}
				break;
				
			default:
				if ($.trim(f.val()) === '' || $.trim(f.val()) == field.def) {
					f.tipsy({
						gravity: field.gr || 'w',
						fallback: field.message,
						trigger: 'manual'
					}).tipsy('show');
					if (scroll) {
						f.last().focus();
						scroll = false;
					};
					res = false;
				}else{
					f.tipsy('hide');
				}
				break;
		}
	}

	return res;
}

function toHex(val) {
	val = parseInt(val);
	val = Math.max(0,val);
	val = Math.min(val,255);
	val = Math.round(val);
	return "0123456789ABCDEF".charAt((val-val%16)/16) + "0123456789ABCDEF".charAt(val%16);
};

