// JavaScript Document

jQuery(document).ready(function () {

	jQuery("a[rel='mylightbox']").colorbox({opacity:0.5,slideshow:true,speed:900});

	jQuery(".youtube").colorbox({iframe:true, width:650, height:550});

	jQuery(".inline1").colorbox({inline:true, href:"#inline_lightbox1", rel:false});

	

	jQuery('.photosCon').cycle({fx: 'fade'});

	jQuery('#comment_form').appendTo('#whole_screen_mode');

	CounterAdd();

	jQuery(".page").hover(

      function () {

        jQuery('#page_info').fadeIn(300);

      }, 

      function () {

        jQuery('#page_info').fadeOut(100);

      }

    );

});

function onFocus(id){

	if(jQuery('#'+id).html()=='Comment:'){

		jQuery('#'+id).html('');

	}	

}

function onBlur(id){

	if(jQuery('#'+id).html()==''){

		jQuery('#'+id).html('Comment:');

	}	

}

function onFocus2(id){

	if(id == 'name'){

		if(jQuery('#'+id).val()=='Name:'){

			jQuery('#'+id).val('');

		}

	}

	else if(id == 'email'){

		if(jQuery('#'+id).val()=='E-mail:'){

			jQuery('#'+id).val('');

		}

	}

}

function onBlur2(id){

	if(id == 'name'){

		if(jQuery('#'+id).val()==''){

			jQuery('#'+id).val('Name:');

		}

	}

	else if(id == 'email'){

		if(jQuery('#'+id).val()==''){

			jQuery('#'+id).val('E-mail:');

		}

	}	

}

function submitContact(){

	nameVal = jQuery('#name').val();

	emailVal = jQuery('#email').val();

	messageText = jQuery('#message').html();

	

	if(nameVal!='' && nameVal!='Name:' && emailVal!='' && emailVal!='E-mail:'){

		document.ChronoContact_contactme.submit();

	}

	else{

		alertError('You have to fill the "name" and the "email" input!');

	}

}

function alertError(msg){

	jQuery('#whole_screen_mode').html('<div id="comment_form"><div class="comment_form_bottom"><div class="comment_form_wrapper" style="min-height:0px;"><a id="close_graph" href="javascript:hideCommForm();"></a><div id="comment_form_fields"><p class="error_msg">'+msg+'</p></div></div></div></div>');

	jQuery('#comment_form_fields').css('display','block');

	jQuery('#whole_screen_mode').fadeIn(400);



}

function showCommForm(id){

	jQuery('#whole_screen_mode').html('<div id="comment_form"><div class="comment_form_bottom"><div class="comment_form_wrapper"><a id="close_graph" href="javascript:hideCommForm();"></a><div id="loading"></div><div id="comment_form_fields"></div></div></div></div>');

	jQuery('#whole_screen_mode').fadeIn(400, function(){

	jQuery('#loading').css('display','block');

	jQuery.ajax({
		
		url:"index.php?option=com_form&id="+id+"&tmpl=component&format=raw",

		success: function(msg){

			jQuery('#comment_form_fields').html(msg);

			setTimeout(function(){

			jQuery('#loading').css('display','none');

			jQuery('#comment_form_fields').css('display','block');

			},300);

			//mootools adding comment

			$('comment-form').addEvent('submit', function(e){

      			new Event(e).stop();

      			var log2 = $('formLog').empty().addClass('formLogLoading');
				
      			this.send({

        			update: log2,

        			onComplete: function(res){

          				log2.removeClass('formLogLoading');

  						if(typeof(Recaptcha) != "undefined"){ 

								Recaptcha.reload();

  						}

          				if (res.substr(13, 7) == 'success') {

							beforeItem = '#comments'+id+' > .firstcomment';

							if(jQuery(beforeItem).length > 0){

								jQuery('#newComment').insertBefore(beforeItem);

								hideCommForm();

								jQuery('#newComment').fadeIn(100, function(){

									jQuery('#newComment').removeAttr('style');

									jQuery(beforeItem).removeClass('firstcomment');

									jQuery('#newComment').addClass('firstcomment');

									jQuery('#newComment').removeAttr('id');

								});

							}

							else{

								curritemlist = '.com_con'+id;

								jQuery(curritemlist).html('<ul class="commentsList" id="comments'+id+'"></ul><a id="comment_btn" href="javascript:showCommForm('+id+');">Comment</a><div class="clear"></div>');

								jQuery('#newComment').appendTo('#comments'+id);

								hideCommForm();

								jQuery('#newComment').fadeIn(100, function(){

									jQuery('#newComment').removeAttr('style');

									jQuery('#newComment').addClass('firstcomment');

									jQuery('#newComment').removeAttr('id');

								});

							}

          				}

        			}

      			});

    		});

		}

	});

	});

}

function CounterAdd(){

	if(jQuery.cookie('webappers_pageviews') == null){

		jQuery.cookie('webappers_pageviews',1,{ expires: 0});	

	}

	else{

		counter = jQuery.cookie('webappers_pageviews');

		counter++;

		jQuery.cookie('webappers_pageviews',counter,{ expires: 0});

	}

	jQuery('#page_counter').html(jQuery.cookie('webappers_pageviews'));

	//alert(jQuery.cookie('webappers_pageviews'));

}

function hideCommForm(){

	jQuery('#whole_screen_mode').fadeOut(300, function(){

		jQuery('#comment_form_fields').css('display','none');

		jQuery('#whole_screen_mode').html('');

	});

	

}

function ajaxGetArchiveDay(url,dataString){

	jQuery('#k2Container').html('<div class="article_preloader"></div>');									

	dataString = dataString + "&type=ajax";
	
	jQuery.ajax({

		type: "POST",

		url:url,

		data: dataString,

		success: function(msg){
			start_num = msg.search('<div id="startofarticles"></div>');

			end_num = msg.search('<div id="endofarticles"></div>');

			newhtml = msg.substring(start_num,end_num)
			
			jQuery('#k2Container').html(newhtml);
			
			CounterAdd();
			jQuery("a[rel='mylightbox']").colorbox({opacity:0.5});

		}

	});

}

function showitem(id,speedOut,speedIn){

	if(current_sel != id){

	myid="#userItemView"+current_sel;

	myidpast= "#userItemBtn"+current_sel;

	myidnew= "#userItemBtn"+id;

	jQuery(myidpast).removeClass('curr');

	jQuery(myid).fadeOut(speedOut, function(){

		mycid = "#userItemView"+id;

		jQuery(mycid).fadeIn(speedIn);

		jQuery(myidnew).addClass('curr');

	});

	current_sel = id;

	}

}

function showitemComm(id,speedOut,speedIn){

	if(current_sel != id){

	myid="#userItemView"+current_sel;

	myidpast= "#userItemBtn"+current_sel;

	myidnew= "#userItemBtn"+id;

	jQuery(myidpast).removeClass('curr');

	jQuery(myid).fadeOut(speedOut, function(){

		mycid = "#userItemView"+id;

		jQuery(mycid).fadeIn(speedIn);

		jQuery(myidnew).addClass('curr');

		jQuery.scrollTo(mycid+' .comments_con',900);

	});

	current_sel = id;

	}

	else{

	mycid = "#userItemView"+id;

	jQuery.scrollTo(mycid+' .comments_con',900);

	}

}

function showComment(year,month,id){

	url = 'index.php';

	dataString = '?option=com_k2&view=itemlist&task=date&month='+month+'&year='+year+'&catid=1&type=ajax';

	ajaxGetArchiveDayAndItem(url,dataString,id);

}

function ajaxGetArchiveDayAndItem(url,dataString,id){

	jQuery('#k2Container').html('<div class="article_preloader"></div>');									

	jQuery.ajax({

		type: "POST",

		url:"http://www.webapper.gr/"+url,

		data: dataString,

		success: function(msg){

			start_num = msg.search('<div id="startofarticles"></div>');

			end_num = msg.search('<div id="endofarticles"></div>');

			newhtml = msg.substring(start_num,end_num)

			jQuery('#k2Container').html(newhtml);

			CounterAdd();

			showitemComm(id,200,300);

			jQuery("a[rel='mylightbox']").colorbox({opacity:0.5});

		}

	});

}

jQuery.cookie = function(name, value, options) {

    if (typeof value != 'undefined') { // name and value given, set cookie

        options = options || {};

        if (value === null) {

            value = '';

            options.expires = -1;

        }

        var expires = '';

        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {

            var date;

            if (typeof options.expires == 'number') {

                date = new Date();

                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));

            } else {

                date = options.expires;

            }

            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE

        }

        // CAUTION: Needed to parenthesize options.path and options.domain

        // in the following expressions, otherwise they evaluate to undefined

        // in the packed version for some reason...

        var path = options.path ? '; path=' + (options.path) : '';

        var domain = options.domain ? '; domain=' + (options.domain) : '';

        var secure = options.secure ? '; secure' : '';

        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');

    } else { // only name given, get cookie

        var cookieValue = null;

        if (document.cookie && document.cookie != '') {

            var cookies = document.cookie.split(';');

            for (var i = 0; i < cookies.length; i++) {

                var cookie = jQuery.trim(cookies[i]);

                // Does this cookie string begin with the name we want?

                if (cookie.substring(0, name.length + 1) == (name + '=')) {

                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));

                    break;

                }

            }

        }

        return cookieValue;

    }

};
