/*
	Autor: Grzegorz Jaworek
	E-mail: grzegorz.jaworek@gmail.com
	www: http://gjaworek.pl 
	Tel.: +48 696 818 300
*/

jQuery(document).ready(function(){
	jQuery('.totop').click(function () {
		scroll(0,0);
		return false;
	});

	jQuery('#top .slideshow').cycle({ 
		fx:     'scrollHorz', 
		speed:  1000, 
		timeout: 7544
	});
	
	jQuery('#sidebar .slideshow').cycle({ 
		fx:     'scrollHorz', 
		speed:  1000, 
		timeout: 6791,
		cleartypeNoBg: true
	});

	jQuery('#menu li a').each(function () { jQuery(this).append('<span class="copy">'+jQuery(this).text()+'</span>'); });
    jQuery('#menu li a').not('#menu li.current_page_item a')
        .css( {backgroundPosition: "-62px 100%"} )
        .mouseover(function(){
            jQuery(this).children('img').stop().animate({right:"0"}, {duration:500})
            jQuery(this).stop().animate({backgroundPosition:"(-22px 100%)"}, {duration:500})
        })
        .mouseout(function(){
            jQuery(this).children('img').stop().animate({right:"40px"}, {duration:200})
            jQuery(this).stop().animate({backgroundPosition:"(-62px 100%)"}, {duration:200, complete:function(){
                jQuery(this).css({backgroundPosition: "-62px 100%"})
            }})
        });
	
	jQuery('.info p').hide();
	jQuery('.info').hoverIntent(toggleIn,toggleOut);
	/*
	jQuery('#file_1').uploadify({
		'uploader'       : 'scripts/uploadify.swf',
		'script'         : 'scripts/uploadify.php',
		'cancelImg'      : 'images/btn-clear.gif',
		'folder'         : 'tmpupload',
		'queueID'        : 'file_quenue_1',
		'buttonImg'      : 'images/btn-browse.gif',
		'wmode'          : 'transparent',
		'width'          : 365,
		'height'         : 25,
		'auto'           : true,
		'multi'          : true
	});
	jQuery('#file_upload_1').click(function () {
		jQuery('#'+jQuery(this).attr('id').replace('upload_','')).uploadifyUpload();
		return false;
	});
	*/
	
	//loadFonts();
	
	if ( jQuery.browser.msie ) {
		if (jQuery.browser.version == '6.0') {
			jQuery('#page').append('<div id="page_bg" />');
			jQuery('#page_bg').height(jQuery('#page').height());
			jQuery('.crop').each(function () { jQuery(this).html('<div style="height: 105px; overflow: hidden; float: left; position: relative; z-index: -1;">'+jQuery(this).html()+'</div>'); });
			jQuery('.info p').each(function () { jQuery(this).replaceWith('<div class="p">'+jQuery(this).html()+'</div>'); });
		}
		if (jQuery.browser.version == '7.0') {
		}
	} 	
	if ( /chrome/.test( navigator.userAgent.toLowerCase() ) ) {
	} 	
	if ( /safari/.test( navigator.userAgent.toLowerCase() ) ) {
	} 
	if ( jQuery.browser.opera ) {
	} 	
	
	jQuery('.lightwindow').each(function () {
		jQuery(this).attr('rel','lightwindow');
	});
	jQuery('.lightwindow').fancybox();
	
	if (jQuery('#regulations').attr('checked')) {
		jQuery('#submitform').show();
	} else {
		jQuery('#submitform').hide();
	}
});

jQuery(window).load(function(){
	jQuery("#footer .simpleimage img").animate({opacity: .3});
	jQuery('#footer .simpleimage a img').mouseover(function () { jQuery(this).animate({ opacity: 1 }, 200); });
	jQuery('#footer .simpleimage a img').mouseout(function () { jQuery(this).animate({ opacity: .3 }, 200); });
});

function loadFonts(){ 
	try {
		Cufon.replace('h2, h3, .content h4, h5', { fontFamily: 'arial' });
		Cufon.replace('ul#menu > li > a, #offer-nav a', { fontFamily: 'arial', hover: true });
	} catch(err) {}
}	

function initCountdown(liftoffTime){ 
	jQuery('#countdown').countdown({
		until: liftoffTime, 
		format: 'dHMS', 
		layout: '<span class="day">{dn}</span><span class="sep"> dni </span><span class="hour">{hn}</span> godzin <span class="sep"></span>', 
		compact: true, 
		description: ''
	});
}

function toggleIn(){ jQuery(this).children('p').animate({height: 'toggle', width: 'toggle'}, 400);}
function toggleOut(){ jQuery(this).children('p').animate({height: 'toggle', width: 'toggle'}, 400);}

function showUploadedImages(id, delid) {
	jQuery.ajax({
		url: "/scripts/showUploadedImages.php",
		data: "id=" + id + '&delid=' + delid,
		cache: false,
		success: function(html) {
			jQuery("#uploadList").html(html);
		}
	});
}

function vote(id) {
  //alert();
  document.getElementById("vote").value=id
  document.getElementById("voteform").submit();
	// jQuery('#vote').value = id;
	// jQuery('#voteform').submit();
}

var correct = false;
function sendForm() {
	correct = true;
	//if (!jQuery('#regulations').attr('checked')) return false;
	if (jQuery('#comment').val().length > 500) {
		jQuery('#commentInfo').html('komentarz ma ponad 500 zn.');
		checkComment();
		correct = false
	} else {
    checkComment();
  }
	if (!jQuery('#school').val()) { checkField('school'); correct = false; }
	if (!jQuery('#city').val()) { checkField('city'); correct = false; }
	if (!jQuery('#tutor').val()) { checkField('tutor'); correct = false; }
	if (!jQuery('#authors').val()) { checkField('authors'); correct = false; }
	if (!jQuery('#mail').val() || !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(jQuery('#mail').val()))) { checkField('mail'); correct = false; }
	if (!jQuery('#phone').val() || !(/^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/.test(jQuery('#phone').val())) ) { checkField('phone'); correct = false; }
	if (correct) { jQuery('#add').submit();}
}

function showInput() {
	if (jQuery('#regulations').attr('checked')) {
		jQuery('#submitform').show();
	} else {
		jQuery('#submitform').hide();
	}
}

function checkField(field) {
	var ok = false;
	if (jQuery('#'+field).val()) {
		ok = true
	}
	if (jQuery('#'+field).attr('name') == 'mail' && !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(jQuery('#mail').val())) ) {
		ok = false;
	}
	if (jQuery('#'+field).attr('name') == 'phone' && !(/^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/.test(jQuery('#phone').val())) ) {
		ok = false;
	}
	if (ok) {
		jQuery('#'+field + 'Field').removeClass('error');
		jQuery('#'+field + 'Field').addClass('valid');
	} else {
		jQuery('#'+field + 'Field').removeClass('valid');
		jQuery('#'+field + 'Field').addClass('error');
	}
}
function checkComment() {
	jQuery('#commentCount').html(jQuery('#comment').val().length);
	if (jQuery('#comment').val().length > 500) {
		jQuery('#commentField').removeClass('valid');
		jQuery('#commentField').addClass('error');
		return;
	}
	jQuery('#commentField').removeClass('error');
	jQuery('#commentField').addClass('valid');
}
