/*---- clear inputs ---*/
function clearInputs(){
	$('input:text, input:password, .field-line textarea').each(function(){
		var _el = $(this);
		var _val = _el.val();
		_el.bind('focus', function(){
			if(this.value == _val) this.value = '';
		}).bind('blur', function(){
			if(this.value == '') this.value = _val;
		});
	});
}
/*--- IE6 hover ---*/
function ieHover(h_list, h_class){
	if($.browser.msie && $.browser.version < 7){
		if(!h_class) var h_class = 'hover';
		$(h_list).mouseenter(function(){
			$(this).addClass(h_class);
		}).mouseleave(function(){
			$(this).removeClass(h_class);
		});
	}
}
/*--- images gallery ---*/
function initImageGall(){
	var change_speed = 500; //in ms
	$('div.popup-gallery-hold').each(function(){
		var _hold = $(this);
		var _btn = _hold.find('div.popup-gallery ul a');
		var _a = startItem();
		var img_hold = _hold.find('div.big-img');
		_btn.removeClass('active').eq(_a).addClass('active');
		_btn.eq(_a).data('img', $('<img src="'+_btn.eq(_a).attr('href')+'" alt="'+_btn.eq(_a).attr('rel')+'"/>'));
		_btn.eq(_a).data('img').addClass('active').css('opacity', 1).appendTo(img_hold);
			
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		
		var fade_f = true;
		function changeEl(_ind){
			if(fade_f && _ind != _a){
				fade_f = false;
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				_btn.eq(_a).data('img').removeClass('active').animate({opacity: 0}, {queue:false, duration: change_speed});
				if(_btn.eq(_ind).data('img')){
					_btn.eq(_ind).data('img').addClass('active').animate({opacity: 1}, {queue:false, duration: change_speed});
					_a = _ind;
					fade_f = true;
				}
				else{
					_btn.eq(_ind).data('img', $('<img />'));
					_btn.eq(_ind).data('img').load(function(){
						_btn.eq(_ind).data('img').addClass('active').animate({opacity: 1}, {queue:false, duration: change_speed});
						_a = _ind;
						fade_f = true;
					});
					_btn.eq(_ind).data('img').css('opacity', 0);
					img_hold.append(_btn.eq(_ind).data('img'));
					_btn.eq(_ind).data('img').attr('src', _btn.eq(_ind).attr('href')).attr('alt', _btn.eq(_ind).attr('rel'));
				}
			}
		}
	});
}
/*--- custom video gallery ---*/
function getQueryString() {
    var _href = location.href;
    var _qs = _href.substr(_href.indexOf("?") + 1, _href.length).split('&');
    var _array = new Array();
    for (var i in _qs) {
        var _s = _qs[i].split('=');
        _array[_s[0]] = _s[1];     
    }
    return _array;
}

function startItem() {
    var vnum = getQueryString()["item"] || 0;
    return vnum < 3 ? vnum : 0;
}

function initVideoGall(){
	$('div.video-gallery-hold').each(function(){
		var _hold = $(this);
		var _btn = _hold.find('div.side-flash > div.vids-item');
		var _a = startItem();
		var video_info = _hold.find('div.flash-video .ttl');
		
		for(var i = 0; i < _btn.length; i++){
			_btn.eq(i).data('info', _btn.eq(i).find('div.video-descr'));
		}
		_btn.removeClass('active').eq(_a).addClass('active');
		video_info.html(_btn.eq(_a).data('info').html());
		swfobject.embedSWF(_btn.eq(_a).find('a.btn-play').attr('href'), "video-hold", "481", "298", "9.0.0", "swf/expressInstall.swf");
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		function changeEl(_ind){
			if(_ind != _a){
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				video_info.html(_btn.eq(_ind).data('info').html());
				swfobject.embedSWF(_btn.eq(_ind).find('a.btn-play').attr('href'), "video-hold", "481", "298", "9.0.0", "swf/expressInstall.swf");
				_a = _ind;
			}
		}
	});
}

/*--- share btn ---*/
function shareBtn() {
    $('div.url-share').each(function() {
        var _id = Math.round(Math.random() * 1000000);
        var _hold = $(this);
        var _field = _hold.find('input:text');
        var _btn = _hold.find('div.btn-copy');
        _field.val(window.parent.location.href).attr('readonly', 'readonly').focus(function() {
            this.select();
        });
        _field.attr('id', 'field_' + _id);
        _btn.attr('id', 'btn_' + _id).parent().attr('id', 'hold_' + _id);

        var clip = new ZeroClipboard.Client();
        clip.setHandCursor(true);
        clip.addEventListener('mouseOver', function() {
            clip.setText(_field.val());
            _hold.addClass('hovered');
        });
        clip.addEventListener('mouseOut', function() {
            _hold.removeClass('hovered');
            _hold.removeClass('hovered-succes');
        });
        clip.addEventListener('complete', function(client, text) {
            _hold.addClass('hovered-succes');
        });
        clip.glue('btn_' + _id, 'hold_' + _id);
    });
}
/*--- custom video gallery ---*/
$('document').ready(function() {
    initImageGall();
    initVideoGall();
    clearInputs();
    ieHover('span.submit-field, input.submit-field, div.vids-item', 'hover');
    $('.reload-parent').click(function() {
        top.document.location.href = this.href;
    });
    $('a.btn-reload').click(function() {
        window.location.href = this.href;
        return false;
    });
    $('a.btn-store-product').click(function() {
        var url = ('/store-finder.aspx' + $('.text-field input[type="hidden"]').val() + '&s=' + $('.txt-search-store-prod').val());
        top.document.location.href = url;
        return false;
    });
    $('.txt-search-store-prod').keypress(function(event) {
        if (event.keyCode == 13) {
            $('.btn-store-product').click();
            return false;
        }
    });
    $('div.url-share input:text').val(window.parent.location.href).attr('readOnly', true).focus(function() {
        this.select();
    });
    shareBtn();
});
