/* User Scripts */
$(document).ready(function(){
	$('#navigation li li.current_page_item').parent().parent().addClass('current_page_item'); //Navigation
	$('#navigation li li li.current_page_item').parent().parent().parent().parent().addClass('current_page_item'); //Navigation
	$('#home .widget:last, #bottom .widget:last, #home .widget:last h3').css('background', 'none'); //Home and Bottom
	$('#bottom .widget #recent-widget p:last').css('padding', '0'); //Bottom
	$('#middle .post:last').css('background', 'none'); //Middle
	$('blockquote').prepend('<div class="blq"></div>'); //Middle
	$('input:text').css('border-radius', '3px').css('-moz-border-radius', '3px').css('-webkit-border-radius', '3px'); //Validator
	$('textarea').css('border-radius', '3px').css('-moz-border-radius', '3px').css('-webkit-border-radius', '3px'); //Validator
	$('.pp_gallery ul a').css('border', '1px rgba(0,0,0,0.5) solid'); //Validator
	$('.coin-slider').css('zoom', '1'); //Validator
});



/* Navigation */
$(document).ready(function(){
	var $i = $j = $m = $n = 0;
	$.each($('#navigation li'), function() { $i = $i + 1; });
	$.each($('#navigation li li'), function() { $m = $m + 1; });
	$i = $i - $m;
	$j = (980 / $i) + ((100 / ($i - 1)) / 100) - 2;
	$j = ($j).toPrecision(7);
	$('#navigation li').css('width', $j + 'px');
	$.each($('#navigation li li'), function() { $(this).css('width', '225px'); });
	$.each($('#navigation li li li'), function() { $(this).css('width', '225px'); });
	$('#navigation li:last-child').css('background', 'none').css('padding-right', '0');
	$('#navigation li li').css('background', 'none').css('padding-right', '0');
	$('#navigation li li li').css('background', 'none').css('padding-right', '0');
});

$(document).ready(function(){
	$('#navigation li ul').parent().addClass('prnt');
	$('#navigation li li ul').parent().removeClass('prnt').addClass('prnt2');
	$('#navigation li ul li:not(:first-child)').css('border', '0');
	$('#navigation li li ul li').css('border', '0');
	$('#navigation li.prnt').hover(function(){
		$(this).find('ul:first').stop().css({overflow:'hidden', height:'auto', display:'none'}).slideDown(300, function(){
			$(this).css({overflow:'visible', height:'auto'});
		});
	}, function(){
		$(this).find('ul:first').stop().slideUp(300, function(){
			jQuery(this).css({overflow:'hidden', display:'none'});
		});
	}); 
	$('#navigation li.prnt li.prnt2').hover(function(){
		$(this).find('ul:first').stop().css({overflow:'hidden', height:'auto', display:'none'}).slideDown(300, function(){
			$(this).css({overflow:'visible', height:'auto'});
		});
	}, function(){
		$(this).find('ul:first').stop().slideUp(300, function(){
			jQuery(this).css({overflow:'hidden', display:'none'});
		});
	}); 
});



/* Content/Sidebar Height */
$(document).ready(function(){
	var $cheight = $('#content').height();
	var $sheight = $('#sidebar').height();
	if( $cheight > $sheight ){ $('div#sidebar').height( $cheight - 60 ); }
});



/* Right Content/Left Sidebar Height */
$(document).ready(function(){
	var $cheight = $('#content.fr').height();
	var $sheight = $('#sidebar.fl').height();
	if( $cheight > $sheight ){ $('div#sidebar').height( $cheight ); }
});



/* Project/Sidebar Height */
$(document).ready(function(){
	var $cheight = $('.middle_content .pr_thumb').height();
	var $sheight = $('.middle_content #sidebar').height();
	if( $cheight > $sheight ){ $('div#sidebar').height( $cheight + 60 ); }
});



/* Home Widgets Separator */
$(document).ready(function(){
	var $fheight = $('#home .widget:eq(0)').height();
	var $sheight = $('#home .widget:eq(1)').height();
	var $theight = $('#home .widget:eq(2)').height();
	if( $fheight > $sheight && $fheight > $theight ){
		$('#home .widget').css('height', $fheight);
	}else if( $sheight > $fheight && $sheight > $theight ){
		$('#home .widget').css('height', $sheight);
	}else{
		$('#home .widget').css('height', $theight);
	}
});



/* Bottom Widgets Separator */
$(document).ready(function(){
	var $fheight = $('#bottom .widget:eq(0)').height();
	var $sheight = $('#bottom .widget:eq(1)').height();
	var $theight = $('#bottom .widget:eq(1)').height();
	if( $fheight > $sheight && $fheight > $theight ){
		$('#bottom .widget').css('height', $fheight);
	}else if( $sheight > $fheight && $sheight > $theight ){
		$('#bottom .widget').css('height', $sheight);
	}else{
		$('#bottom .widget').css('height', $theight);
	}
});



/* Search Text */
$(document).ready(function(){
	$('.search input#search, .search input#middlesearch').focus(function(){ 
		if( $(this).val() == 'Search...' ){ $(this).val(''); } 
	});
	$('.search input#search, .search input#middlesearch').blur(function(){ 
		if( $(this).val() == '' ){ $(this).val('Search...'); } 
	});
});



/* Bottom Show/Hide */
$(document).ready(function(){
	$('#bottom .hide').hide();
	$('#bottom .bottom').slideUp(250, function(){
		$('#footer').css('margin', '-11px 0 0');
	});
	$('#bottom .show').click(function() {
		$('#bottom .show').hide();
		$('#bottom .hide').show();
		$('#bottom .toggle').css('margin', '0 0 -11px');
		$('#bottom .bottom').slideDown(500, function(){
			$('#footer').css('margin', '0');
			$body_height = $('body').height();
			$('html, body').animate({scrollTop: $body_height}, 1000);
		});
		return false;
	});
	$('#bottom .hide').click(function() {
		$('#bottom .hide').hide();
		$('#bottom .show').show();
		$('#bottom .bottom').slideUp(500, function(){
			$('#footer').css('margin', '-11px 0 0');
			$('#bottom .toggle').css('margin', '0');
		});
		return false;
	});
});



/* Form */
function submitform() {
    document.forms["commentform"].submit();
	return false;
};



/* Flickr Lightbox */
$(document).ready(function() {
	$("#flickr .flickr_badge_image a").each(function(i){
		var src = $(this).find("img").attr("src");
		var title = $(this).find("img").attr("title");
		var src2 = src.replace(/_s.jpg/g, ".jpg");
		$(this).removeAttr("href");
		$(this).attr({
			href: src2,
			title: title,
			rel: 'prettyPhoto[flickr_gal]'
		});
	});
});



/* Pretty Photo Lighbox */
/* <![CDATA[ */
	jQuery(document).ready(function($) {
	  $("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'normal',
		padding: 20,
		opacity: 0.65,
		showTitle: true,
		allowresize: true,
		counter_separator_label: ' / ',
		theme: 'light_square',
		wmode: 'opaque',
		autoplay: 1,
		hideflash: false,
		modal: false,
		changepicturecallback: function(){},
		callback: function(){},
		markup: '<div class="pp_pic_holder"> \
			  <div class="pp_top"> \
				<div class="pp_left"></div> \
				<div class="pp_middle"></div> \
				<div class="pp_right"></div> \
			  </div> \
			  <div class="pp_content_container"> \
				<div class="pp_left"> \
				<div class="pp_right"> \
				  <div class="pp_content"> \
					<div class="pp_fade"> \
					  <a href="#" class="pp_expand" title="Expand the image">Expand</a> \
					  <div class="pp_loaderIcon"></div> \
					  <div class="pp_hoverContainer"> \
						<a class="pp_next" href="#">next</a> \
						<a class="pp_previous" href="#">previous</a> \
					  </div> \
					  <div id="pp_full_res"></div> \
					  <div class="pp_details clearfix"> \
						<p class="pp_description"></p> \
						<span class="pp_close">Press Esc or <a href="#">Close</a></span> \
						<div class="pp_nav"> \
						  <a href="#" class="pp_arrow_previous">Previous</a> \
						  <p class="currentTextHolder">0/0</p> \
						  <a href="#" class="pp_arrow_next">Next</a> \
						</div> \
					  </div> \
					</div> \
				  </div> \
				</div> \
				</div> \
			  </div> \
			  <div class="pp_bottom"> \
				<div class="pp_left"></div> \
				<div class="pp_middle"></div> \
				<div class="pp_right"></div> \
			  </div> \
			</div> \
			<div class="pp_overlay"></div> \
			<div class="ppt"></div>',
		image_markup: '<img id="fullResImage" src="" />',
		flash_markup: '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{width}" height="{height}"><param name="wmode" value="{wmode}" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="{path}" /><embed src="{path}" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="{width}" height="{height}" wmode="{wmode}"></embed></object>',
		quicktime_markup: '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="{height}" width="{width}"><param name="src" value="{path}"><param name="autoplay" value="{autoplay}"><param name="type" value="video/quicktime"><embed src="{path}" height="{height}" width="{width}" autoplay="{autoplay}" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>',
		iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" frameborder="no"></iframe>',
		inline_markup: '<div class="pp_inline clearfix">{content}</div>'
	  });
	});
/* ]]> */



/* Scroll Top */
$(document).ready(function(){
	$('a.scroll_top').click(function(){
		$('html, body').animate({scrollTop:0}, 'slow');
		return false;
	});
});



/* Toggle */
$(document).ready(function(){ 
	var $togg = $('.togg');
	$.each($togg, function(){
		var $tog = $(this).find('span.tog');
		var $tabc = $(this).find('.tab_content');
		if ($tog.hasClass('current')){
			$tog.toggle(
				function(){
					$(this).removeClass('current');
					$tabc.hide('slide', { direction: 'up' }, 300);
				},
				function(){
					$(this).addClass('current');
					$tabc.show('slide', { direction: 'up' }, 300);
				}
			);
		}else{
			$tabc.hide();
			$tog.toggle(
				function(){
					$(this).addClass('current');
					$tabc.show('slide', { direction: 'up' }, 300);
				},
				function(){
					$(this).removeClass('current');
					$tabc.hide('slide', { direction: 'up' }, 300);
				}
			);
		}
	});
});



/* Lightbox Image Hover */
$(document).ready(function(){
	var $rel = $('#middle a img').parent().attr('rel');
	var $hov = $('#middle a img').parent();
	$hov.addClass('hov');
	if($rel){ 
		$imgh = $('a.hov').find('img').height();
		$imgw = $('a.hov').find('img').width();
		$('a.hov').prepend('<span></span>');
		$('a.hov').find('span').css({ width: $imgw, height: $imgh, margin: '5px' });
		$.each($hov, function(){
			var $hovs = $(this).find('span');
			$hovs.css('opacity', '0');
			$(this).hover(
				function(){
					$hovs.stop().animate({ opacity: '0.5' }, 500);
				},
				function(){
					$hovs.stop().animate({ opacity: '0' }, 500);
				}
			);
		});
	}
});






/* Contact Form */
function checkemail(emailaddress){
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i); 
	return pattern.test(emailaddress); 
}

$(document).ready(function(){ 
	var $math1 = Math.floor( Math.random() * (100) );
	var $math2 = Math.floor( Math.random() * (100) );
	$('#contactform #math1').text($math1);
	$('#contactform #math2').text($math2);
	var $mathsum = $math1 + $math2;
	$('#contactform a#formsend').click(function(){ 
		var $name 	= $('#name').val();
		var $email 	= $('#email').val();
		var $subject = $('#subject').val();
		var $message = $('#message').val();
		var $confirm = $('#confirm').val();
		var $contactemail = $('#contactemail').val();
		if($name != '' && $name.length < 3){ $nameshort = true; }else{ $nameshort = false; }
		if($name != '' && $name.length > 30){ $namelong = true; }else{ $namelong = false; }
		if($email != '' && checkemail($email)){ $emailerror = true; }else{ $emailerror = false; }
		if($subject != '' && $subject.length < 3){ $subjectshort = true; }else{ $subjectshort = false; }
		if($subject != '' && $subject.length > 100){ $subjectlong = true; }else{ $subjectlong = false; }
		if($message != '' && $message.length < 3){ $messageshort = true; }else{ $messageshort = false; }
		if($confirm != '' && $confirm != $mathsum){ $confirmerror = true; }else{ $confirmerror = false; }
		$('.loading').fadeIn('fast');
		if($name != '' && $nameshort != true && $namelong != true && $email != '' && $emailerror != false && $subject != '' && $subjectshort != true && $subjectlong != true && $message != '' && $messageshort != true && $confirm != '' && $confirmerror != true && $contactemail != ''){ 
			$.post('php/sendmail.php', 
				{contactemail: $contactemail, name: $name, email: $email, subject: $subject, message: $message}, 
				function(data){
					$('.loading').fadeOut('fast');
					$('#contactform').fadeOut();
					$('#name, #subject, #url, #email, #message, #confirm').val('').css({'border':'1px solid #d2d2d2'});
					$('#contactform label span').remove();
					$('<div class="box info"><table><tbody><tr><td class="b_img"></td><td>Thank you, ' + $name + '! <br />Your message has been send successfully.</td></tr></tbody></table></div>').insertBefore('#contactform');
					$('.box').delay(5000).fadeOut(1000, function(){ 
						$(this).remove(); 
						$('#contactform').fadeIn();
					});
				}
			);
			return false;
		}else{
			$('.loading').fadeOut('fast');
			$('<div class="box error"><table><tbody><tr><td class="b_img"></td><td>Your message has not been send successfully.<br />It was an error on page!</td></tr></tbody></table></div>').insertBefore('#contactform');
			$('.box').delay(5000).fadeOut(1000, function(){ $(this).remove(); });
			if($name == ''){ 
				$('#name').css({'border':'1px solid #ee185d'}); 
				$('#name').parent('p').find('label').find('span').remove(); 
				$('#name').parent('p').find('label').append('<span>You forgot to enter your name.</span>'); 
			}else if($nameshort == true){ 
				$('#name').css({'border':'1px solid #ee185d'}); 
				$('#name').parent('p').find('label').find('span').remove(); 
				$('#name').parent('p').find('label').append('<span>Entered name must not be shorter than 3 characters.</span>'); 
			}else if($namelong == true){ 
				$('#name').css({'border':'1px solid #ee185d'}); 
				$('#name').parent('p').find('label').find('span').remove(); 
				$('#name').parent('p').find('label').append('<span>Entered name must not be longer than 30 characters.</span>'); 
			}else{ 
				$('#name').css({'border':'1px solid #d2d2d2'}); 
				$('#name').parent('p').find('label').find('span').remove(); 
			}
			if($email == ''){ 
				$('#email').css({'border':'1px solid #ee185d'}); 
				$('#email').parent('p').find('label').find('span').remove(); 
				$('#email').parent('p').find('label').append('<span>You forgot to enter your email address.</span>'); 
			}else if($emailerror == false){ 
				$('#email').css({'border':'1px solid #ee185d'}); 
				$('#email').parent('p').find('label').find('span').remove(); 
				$('#email').parent('p').find('label').append('<span>You entered an invalid email address.</span>'); 
			}else{ 
				$('#email').css({'border':'1px solid #d2d2d2'}); 
				$('#email').parent('p').find('label').find('span').remove(); 
			}
			if($subject == ''){ 
				$('#subject').css({'border':'1px solid #ee185d'}); 
				$('#subject').parent('p').find('label').find('span').remove(); 
				$('#subject').parent('p').find('label').append('<span>You forgot to enter the subject of your message.</span>'); 
			}else if($subjectshort == true){ 
				$('#subject').css({'border':'1px solid #ee185d'}); 
				$('#subject').parent('p').find('label').find('span').remove(); 
				$('#subject').parent('p').find('label').append('<span>Entered subject must not be shorter than 3 characters.</span>'); 
			}else if($subjectlong == true){ 
				$('#subject').css({'border':'1px solid #ee185d'}); 
				$('#subject').parent('p').find('label').find('span').remove(); 
				$('#subject').parent('p').find('label').append('<span>Entered subject must not be longer than 100 characters.</span>'); 
			}else{ 
				$('#subject').css({'border':'1px solid #d2d2d2'}); 
				$('#subject').parent('p').find('label').find('span').remove(); 
			}
			if($message == ''){ 
				$('#message').css({'border':'1px solid #ee185d'}); 
				$('#message').parent('p').find('label').find('span').remove(); 
				$('#message').parent('p').find('label').append('<span>You forgot to enter the text of your message.</span>'); 
			}else if($messageshort == true){ 
				$('#message').css({'border':'1px solid #ee185d'}); 
				$('#message').parent('p').find('label').find('span').remove(); 
				$('#message').parent('p').find('label').append('<span>Entered message must not be shorter than 3 characters.</span>'); 
			}else{ 
				$('#message').css({'border':'1px solid #d2d2d2'}); 
				$('#message').parent('p').find('label').find('span').remove(); 
			}
			if($confirm == ''){ 
				$('#confirm').css({'border':'1px solid #ee185d'}); 
				$('#confirm').parent('p').find('label').find('span').remove(); 
				$('#confirm').parent('p').find('label').append('<span>You forgot to fill the confirmation field.</span>'); 
			}else if($confirmerror == true){ 
				$('#confirm').css({'border':'1px solid #ee185d'}); 
				$('#confirm').parent('p').find('label').find('span').remove(); 
				$('#confirm').parent('p').find('label').append('<span>Entered confirmation value is incorrect.</span>'); 
			}else{ 
				$('#confirm').css({'border':'1px solid #d2d2d2'}); 
				$('#confirm').parent('p').find('label').find('span').remove(); 
			}
			return false;
		}
	});
});

