/******* Text Box Hints START ********/
/**
* @author Remy Sharp
* @url http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
*/

(function ($) {
	$.fn.hint = function (blurClass) {
	    if (!blurClass) blurClass = 'blur'; 	
	    return this.each(function () {
	        var $input = $(this),
	            title = $input.attr('title'),
	            $form = $(this.form),
	            $win = $(window);
	        function remove() {
	            if (this.value === title && $input.hasClass(blurClass)) {
	                $input.val('').removeClass(blurClass);
	            }
	        }
	        // only apply logic if the element has the attribute
	        if (title) { 
	            // on blur, set value to title attr if text is blank
	            $input.blur(function () {
	                if (this.value === '') {
	                    $input.val(title).addClass(blurClass);
	                }
	            }).focus(remove).blur(); // now change all inputs to title            
	            // clear the pre-defined text when form is submitted
	            $form.submit(remove);
	            $win.unload(remove); // handles Firefox's autocomplete
	        }
	    });
	};
})(jQuery);

$(function(){
	// elements with class 'blur'
	$('.blur').hint();
});
/******* Text Box Hints END ********/

/******* Weather Right Block START ********/
		$(document).ready(function(){
			$('a.searchImg').click(function(){ //When link (Избери град или Върни се обратно) is clicked
				var currentW = $('#searchCity'); // Set variable currentW to value of href attribute of clicked link				
				$(currentW).toggle();
				return false;			
			});
		});		
/******* Weather Right Block END ********/

/******* Tabs START ********/
		$(document).ready(function(){
			$('div#lcmTabs div#lcmTabsInner > div').hide(); // Hide all divs
			$('div#lcmTabs div#lcmTabsInner > div:first').show(); // Show the first div
			$('div#lcmTabs div#lcmTabsInner ul li a:first').addClass('select'); // Set the class of the first link to active
			$('div#lcmTabs div#lcmTabsInner ul li a').click(function(){ //When any link is clicked
			$('div#lcmTabs').css('background-position', '297px 4px');
			$('div#lcmTabs div#lcmTabsInner ul li a').removeClass('select'); // Remove active class from all links
			$(this).addClass('select'); //Set clicked link class to active
			var currentTab = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
			$('div#lcmTabs div#lcmTabsInner> div').hide(); // Hide all divs
			$(currentTab).show(); // Show div with id equal to variable currentTab
			return false;
			});
			$('div#lcmTabs div#lcmTabsInner ul li a:last').click(function(){
				$('div#lcmTabs').css('background-position', '297px 0');
			});
		});
		/*$(document).ready(function(){
			$('div#maTabs > div#mainArticle, div#mainArticleVideo, div#mainArticlePhoto').hide(); // Hide all divs
			$('div#maTabs > div#mainArticle').show(); // Show the first div
			$('div#maTabs ul li a:first').addClass('select'); // Set the class of the first link to active
			$('div#maTabs ul li a').click(function(){ //When any link is clicked
			$('div#maTabs ul li a').removeClass('select'); // Remove active class from all links
			$(this).addClass('select'); //Set clicked link class to active
			var currentTab1 = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
			$('div#maTabs > div#mainArticle, div#mainArticleVideo, div#mainArticlePhoto').hide(); // Hide all divs
			$(currentTab1).show(); // Show div with id equal to variable currentTab
			return false;
			});
		});*/
		$(document).ready(function(){
                        $('div#aTabs div#phones, div#clothes, div#jewelry, div#auto, div#antiques').hide(); // Hide all divs
                        $('div#aTabs div.tabContentPromRow:first').show();
                        $('div#aTabs div.auctionCat a:first').addClass('select'); // Set the class of the first link to active
                        $('div#aTabs div.auctionCat a').click(function(){ //When any link is clicked
                        $('div#aTabs div.auctionCat a').removeClass('select'); // Remove active class from all links
                        $(this).addClass('select'); //Set clicked link class to active
                        var currentTab2 = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
                        $('div#aTabs div.tabContentPromRow').hide(); // Hide all divs
                        $(currentTab2).show(); // Show div with id equal to variable currentTab
                        return false;
                        });
		});
/******* Tabs END ********/

/******* Change Font size START ********/
		$(document).ready(function(){
			// Reset Font Size
			var originalFontSize = $('#fullArticle').css('font-size');
			$("#resetFont").click(function(){
				$('#fullArticle').css('font-size', originalFontSize);
                                $('#fullArticle p').css('font-size', originalFontSize);
				return false;
			});
			// Increase Font Size
			$("#plusFont").click(function(){
				var currentFontSize = $('#fullArticle').css('font-size');
				var currentFontSizeNum = parseFloat(currentFontSize);
				var newFontSize = currentFontSizeNum + 1;
				$('#fullArticle').css('font-size', newFontSize);
                                $('#fullArticle p').css('font-size', newFontSize);
				return false;			
			});
			// Decrease Font Size
			$("#minusFont").click(function(){
				var currentFontSize = $('#fullArticle').css('font-size');
				var currentFontSizeNum = parseFloat(currentFontSize);
				var newFontSize = currentFontSizeNum - 1;
				$('#fullArticle').css('font-size', newFontSize);
                                $('#fullArticle p').css('font-size', newFontSize);
			return false;
			});
		});
/******* Change Font size END ********/

/******* MAIN ARCTICLE IMAGE SLIDE START ********/
		$(function() {
			$('#mainArticleImgSlide').cycle({
				fx: 'fade',
				speed: 400,				
				timeout: 0,
				containerResize: true,
				after: onAfter,			
				next: '#maNext',
				prev: '#maPrev',
				pager: '#maPhotoNav',
				pagerAnchorBuilder: function(idx, slide) { 
					// return selector string for existing anchor 
					return '#maPhotoNav a:eq(' + idx + ')'; 
				}
			});
			function onAfter() {
			    $('.mainArticleImg span.imageAuthor').html(this.getElementsByTagName('img')[0].getAttribute('rel'));
            }									
		});		
/******* MAIN ARCTICLE IMAGE SLIDE END ********/

/******* Sliding the Photo Gallery Caption and Play/Pause button change START ********/
    	$(document).ready(function() {
			$('#maPhotoSlideNav ul li a').click(function(){ // On paging click
				var aText = $(this).html();
        		changePhotoAlt('picture', aText);
				return false;
			});			
			changePhotoAlt('picture', 1);		
		});		
		$(document).ready(function() { 
			$('#maPSPlay').click(function(){ // Changing the Play/Pause button on Click event
				$(this).toggleClass('maPSPause');
				return false;
			});
		});
		$(document).ready(function() { 		
			$('#maPhotoSlideBlock .toggle').click(function(){ // Sliding the Photo Gallery Caption on Click event
				$('#maPhotoSlideBlock .maPhotoSlideHoverCaption').slideToggle('slow', function(){
					$('#maPhotoSlideBlock .toggle').toggleClass('toggleUp');
				});
				return false;
			});
		});
/******* Sliding the Photo Gallery Caption Play/Pause button change END ********/

/******* RIGHT VIDEO AND PHOTO TABS AND SLIDE START ********/
		$(document).ready(function(){
			$('div#videoPhotoBlock > div').hide(); // Hide all divs
			$('div#videoPhotoBlock > div:first').show(); // Show the first div
			$('div#videoPhotoBlock ul li a:first').addClass('select'); // Set the class of the first link to active
			$('div#videoPhotoBlock ul li a').click(function(){ //When any link is clicked
			$('div#videoPhotoBlock ul li a').removeClass('select'); // Remove active class from all links
			$(this).addClass('select'); //Set clicked link class to active
			var currentTab = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
			$('div#videoPhotoBlock > div').hide(); // Hide all divs
			$(currentTab).show(); // Show div with id equal to variable currentTab
			return false;
			});
		});
		$(function() {
			$('#videoSlide').cycle({ 
				fx: 'scrollLeft', 
				speed: 'fast', 
				delay: 200,
				timeout: 0, 
				next: '#videosNext',
				prev: '#videosPrev',
				pager: '#videoNav',
				pagerAnchorBuilder: function(idx, slide) { 
					// return selector string for existing anchor 
					return '#videoNav a:eq(' + idx + ')'; 
				}
			});
		});
		$(function() {
			$('#photoSlide').cycle({ 
				fx: 'scrollLeft', 
				speed: 'fast', 
				delay: 300,
				timeout: 0, 
				next: '#photosNext',
				prev: '#photosPrev',
				pager: '#photoNav',
				pagerAnchorBuilder: function(idx, slide) { 
					// return selector string for existing anchor 
					return '#photoNav a:eq(' + idx + ')'; 
				}
			});
		});
/******* RIGHT VIDEO AND PHOTO TABS AND SLIDE START ********/

/******* SMOOTH SCROLLING START ********/		
$(document).ready(function(){
	$('#rcTop a[href*=#], #cr_fc a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html, body').animate({scrollTop: targetOffset}, 1000);
				return false;
			}
		}
	});
});
/******* SMOOTH SCROLLING END ********/	

/******* PHOTO START ********/
var oldAstro=1;
var astro = 1;
var currentImage = 1;
var prevImage = 1;
var interval = 1500;
var slide = 0;
var picNum = 1;
var timerID = 0;
var picNumComment = 1;

function nextImage()
{
  var maxImages = document.getElementById('maxImages').value;
  prevImage = currentImage;

  if(currentImage != maxImages) currentImage += 1;
  else { currentImage = 1;  picNum = 1; picNumComment = 1; }
  
  document.getElementById('maPhotoSlide'+prevImage).style.display = 'none';
  document.getElementById('maPhotoSlide'+currentImage).style.display = ''; 
  document.getElementById('as'+currentImage).className = 'activeSlide';
	
  if(prevImage) document.getElementById('as'+prevImage).className = 'a';
  
  //Scroll controls to current element and get picture alt tag
  changePhotoAlt('picture', currentImage);
	var LeftScroll = 22*currentImage-110;

	$('#maPhotoSlideNav').scrollLeft(LeftScroll);	
		
	oldAstroScroll = oldAstro = currentImage;
}

function slideOne() 
{
  var maxImages = document.getElementById('maxImages').value;
  
  if (picNum>=maxImages) 
  {
    picNum = picNumComment = 1;
    $('#picNum').html( picNum );
    $('#picNumComment').html( picNumComment );
  }
  else 
  {
    $('#picNum').html( ++picNum );
    $('#picNumComment').html( ++picNumComment );
  }	
  
  nextImage();
  timerID = setTimeout(slideOne, interval);
}

function slideImages(stop) 
{
  //If stop is set to 0, make slide=1 and stop scrolling
  if (stop == 0) 
  {
    slide = 1;
    $('#maPSPlay').removeClass();
    $('#maPSPlay').addClass('maPSPlay');
  }
  
  if(slide == 0) {
    slide = 1;
    timerID = setTimeout(slideOne, interval);
  }
  else {
    slide = 0;
    clearTimeout(timerID);
    ShowAstro( oldAstro );
  }
}

function ShowAstro(astro) 
{
	// Select button	
	$('#as'+oldAstro).removeClass();
	$('#as'+astro).addClass('activeSlide');
	$('#maPhotoSlide'+oldAstro).hide();
	$('#maPhotoSlide'+astro).show();
	
  currentImage = astro;  
	picNum = picNumComment = astro;

	$('#picNum').html( picNum );
	$('#picNumComment').html( picNumComment );
	
	//Scroll controls to current element
	var LeftScroll = 22*astro-110;
	
	changePhotoAlt('picture', picNum);
	$('#maPhotoSlideNav').scrollLeft(LeftScroll);
	
	oldAstroScroll = oldAstro = astro;
}

var oldAstroScroll = oldAstro;

function ScrollAstro(direction, totalNumber) {
	// move step
	if(direction == 'next') {
		oldAstroScroll += 1;
		if(oldAstroScroll > totalNumber ) oldAstroScroll = 1;
	}
	else {
		oldAstroScroll -= 1;
		if(oldAstroScroll < 1 ) oldAstroScroll = totalNumber;
	}
	
	ShowAstro(oldAstroScroll);
}

//Changes the photo alt
function changePhotoAlt(divId, picNumber)
{
  if (document.getElementById(divId+picNumber)) 
  {
    var images = document.getElementById(divId+picNumber);
  }
  
  if (images) $('#picText').text(images.alt);
}

window.onload = function() {ShowAstro( oldAstro );} 

/******* PHOTO END **********/ 	

/******* FACEBOOK SCRIPT START *********/
function fbs_click() 
{
  u=location.href;
  t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
  return false;
}
/******* FACEBOOK SCRIPT END *********/


/******* FORM LIMITATION SCRIPT START *********/
function limitChars(textid, limit, infodiv){

	 var text = $('#'+textid).val(); 
	 var textlength = text.length;
	 
	 if(textlength > limit){
		
		$('#' + infodiv).html('Не можете да пишете повече от '+limit+' символа!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else{
		$('#' + infodiv).html('Имате още '+ (limit - textlength) +' позволени символа');
		return true;
	}

}

// start limitation
$(function(){
	$('#userComment').keyup(function(){
		limitChars('userComment', 2000, 'charlimitinfo');
	})
});


/******* FORM LIMITATION SCRIPT END *********/



/******* FORM CHANGE COLOR START *********/
$(function(){
	$('#userComment').click(function(){
		$(this).css('color', '#000');
	})
});

$(function(){
	$('#userComment').blur(function(){
		if($(this).val() == 'Максимум 2000 символа') $(this).css('color', '#999999');
	})
});
/******* FORM CHANGE COLOR END *********/
