﻿// page init

$(function() {

	initGallery();

	initAccordion();

	initOpenClose();

	isi_prod_slider();

	forminputs();

});



// open-close blocks function

function initOpenClose() {

	var _slideSpeed = 450;

	var _activeClass = 'active';

	$('ul.open-close-list > li').each(function(){

		var _holder = $(this);

		var _opener = _holder.find('.opener');

		var _slider = _holder.find('.slide');

		_opener.click(function(){

			if(_holder.hasClass(_activeClass)) {

				_slider.slideUp(_slideSpeed,function(){

					_holder.removeClass(_activeClass);

				});

			} else {

				_holder.addClass(_activeClass);

				_slider.slideDown(_slideSpeed);

			}

			return false;

		});

		if(_holder.hasClass(_activeClass)) _slider.show();

		else _slider.hide();

	});

}



// accordion function

function initAccordion() {

	var _activeClass = 'opened';

	var _slideSpeed = 500;

	$('div.middle').each(function(){

		var _accordion = $(this);

		var _items = _accordion.find('div.hold:has(div.hold-body)');

		_items.each(function(){

			var _holder = $(this);

			var _opener = _holder.find('>div.hold-title');

			var _slider = _holder.find('>div.hold-body');

			_opener.click(function(){

				var _levelItems = _holder.parent().children(':has(div.hold-body)').not(_holder);

				if(_holder.hasClass(_activeClass)) {

					_holder.removeClass(_activeClass);

					_slider.slideUp(_slideSpeed);

				} else {

					_holder.addClass(_activeClass);

					_slider.slideDown(_slideSpeed);

					// collapse others

					_levelItems.removeClass(_activeClass);

					_levelItems.find('>div.hold-body:visible').slideUp(_slideSpeed);

				}

				return false;

			});



			if(_holder.hasClass(_activeClass)) _slider.show();

			else _slider.hide();

		});

	});

}



// scroll gallery function

function initGallery() {

	var _activeClass = 'active';

	var _switchTime = 10000;

	var _minWidth = 1000;

	var _speed = 650;



	$('div#gallery').each(function(){

		// gallery options

		var _holder = $(this);

		var _btnLeft = _holder.find('.btn-prev, a.prev');

		var _btnRight = _holder.find('.btn-next, a.next');

		var _paginationLinks = _holder.find('.tools ul li');

		var _slidesHolder = _holder.find('.wrapper');

		var _slider = _slidesHolder.find('ul.gallery-info');

		var _slides = _slider.children();

		var _slidesCount = _slides.length;

		var _slideWidth = _slides.eq(0).width();

		var _originalWidth = _slideWidth;

		var _currentIndex = 0;

		var _oldIndex = 0;

		var _direction;

		var _timer;

		var sliding = false;



		// flexible width gallery

		function reCalc() {

			var _width;

			var _diff;

			if($(window).width() > _minWidth) _width = $(window).width();

			else _width = _minWidth;

			_slideWidth = _width;



			_diff = (_width-_originalWidth)/2;

			_slider.css({marginLeft:_diff});



			_slidesHolder.css({width:_width});

		}



		// gallery init

		_currentIndex = _paginationLinks.index(_paginationLinks.filter('.'+_activeClass).eq(0));

		if(_currentIndex<0) _currentIndex=0;

		_slider.css({

			position:'relative',

			height:_slides.eq(0).outerHeight(true)

		});

		_slides.hide().eq(_currentIndex).show();

		_slides.css({

			position:'absolute',

			top:0

		});

		reCalc();



		// gallery control

		_btnLeft.click(function(){

			if(!sliding) {

				sliding = true;

				prevSlide();

			}

			return false;

		});

		_btnRight.click(function(){

			if(!sliding) {

				sliding = true;

				nextSlide();

			}

			return false;

		});

		_paginationLinks.each(function(_ind){

			$(this).click(function(){

				if(!sliding) {

					sliding = true;

					slideNum(_ind);

				}

				return false;

			});

		});



		// gallery animation

		function prevSlide() {

			_oldIndex = _currentIndex;

			if(_currentIndex > 0) _currentIndex--;

			else _currentIndex = _slidesCount-1;

			_direction=0;

			switchSlide();

		}

		function nextSlide() {

			_oldIndex = _currentIndex;

			if(_currentIndex < _slidesCount-1) _currentIndex++;

			else _currentIndex = 0;

			_direction=1;

			switchSlide();

		}

		function slideNum(_number) {

			if(_number != _currentIndex) {

				_oldIndex = _currentIndex;

				_direction = (_number > _currentIndex ? 1 : 0);

				_currentIndex = _number;

				switchSlide();

			} else {sliding = false;}

		}

		function switchSlide() {

			var _prevSlide = _slides.eq(_oldIndex);

			var _nextSlide = _slides.eq(_currentIndex);

			_nextSlide.css({left:2000*(_direction ? 1 : -1)}).show();

			_prevSlide.animate({left:2000*(_direction ? -1 : 1)},{duration:_speed, queue:false,complete:function(){

				_slides.css({

					left: _slideWidth*(_direction ? -1 : 1),

					display: 'none'

				});

				_slides.eq(_currentIndex).css({

					left: 0,

					display: 'block'

				});

				sliding = false;

			}});

			_nextSlide.animate({left:0},{duration:_speed, queue:false});

			updatePagination();

			autoSlide();

		}

		function updatePagination() {

			_paginationLinks.removeClass(_activeClass).eq(_currentIndex).addClass(_activeClass)

		}

		function autoSlide() {

			if(_timer) clearTimeout(_timer);

			_timer = setTimeout(nextSlide,_switchTime);

		}

		autoSlide();

		$(window).resize(reCalc);

	});

}

function createCookie(name,value,days) {

	if (days) {

		var date = new Date();

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

		var expires = "; expires="+date.toGMTString();

	}

	else var expires = "";

	document.cookie = name+"="+value+expires+"; path=/";

}

function readCookie(name) {

	var nameEQ = name + "=";

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

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

		var c = ca[i];

		while (c.charAt(0)==' ') c = c.substring(1,c.length);

		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);

	}

	return null;

}

function save(city){

	createCookie('city',city,1);

	document.getElementById('save-button').style.display = 'none';

}

function change(city){

 $.ajax({  

     type: "POST",  

     data: "change_city=1",  

     url: '/media/wp-content/themes/media/save_city.php',

     success: function (msg){

		$('#ajax-container').html(msg);

     }

   });

}

function isi_prod_slider(){

	var _frames = $('.prod-slider-item');

	

	var _next = $('.box-prod-slider div.next');

	var _prev = $('.box-prod-slider div.prev');

	

	var currentindex = 0;

	var maxindex = _frames.length-1;

	var duration = 500;

	

	_frames.hide().eq(0).show();	

	

	_next.bind('click', isi_next);

	_prev.bind('click', isi_prev);

	

	function isi_prev(){

		_prev.unbind('click');

		previndex = currentindex;

		currentindex = (currentindex == maxindex) ? 0 : ++currentindex;

		_frames.eq(currentindex).css('left',"255px").show().animate({

			left: '0'

		}, duration);

		_frames.eq(previndex).animate({

			left: '-255px'

		}, duration, function(){

			$(this).hide();

			_prev.bind('click', isi_prev);

		});

	}

	

	function isi_next(){

		_next.unbind('click');

		previndex = currentindex;

		currentindex = (currentindex == 0) ? maxindex : --currentindex;

		_frames.eq(currentindex).css('left',"-255px").show().animate({

			left: '0'

		}, duration);

		_frames.eq(previndex).animate({

			left: '255px'

		}, duration, function(){

			$(this).hide();

			_next.bind('click', isi_next);

		});

	}

}

function forminputs(){

	$('.products-sidebox .cform input, .products-sidebox .cform textarea').each(function(){

		$(this).focus(function () {

			$(this).prev("label").addClass("focus");

		});

			  

		$(this).keypress(function () {

			$(this).prev("label").addClass("has-text").removeClass("focus");

		});

			  

		$(this).blur(function () {

			if($(this).val() == "") {

				$(this).prev("label").removeClass("has-text").removeClass("focus");

			}

		});

	});

}
