$(function(){
  $(document).ready(function() {
  	$('.message').animate({top:'-=80'},2200);
	jQuery('#video-gallery ul li a').prettyPhoto();
	jQuery('.video_thumb').each(function(){
		var a = jQuery(this)
		var b = jQuery(this).find('img');
		var equasion;
		if (a.width() < b.width()) {
			equasion = ((b.width()-a.width()) / 2)
			b.css('margin-left','-'+equasion+'px');
		}
	});
	$("#dada").click(function() {     
		$("#delege").dialog({       
			show: 'explode',       
			hide: 'explode',       
			width: '440px'     
			});  
		});

	$(".edit").click(function() {
		var a = jQuery(this).attr('rel');
		var b = jQuery(this).parent().find('a[rel=prettyPhoto]');
		jQuery('#eodit').find('textarea[name=description]').html(b.attr('title'));
		jQuery('#eodit').find('textarea[name=embed]').html(jQuery('#used'+a).html());
		jQuery('#eodit').find('input[name=video]').val(b.attr('href'));
		jQuery('#eodit').find('input[name=id]').val(a);
		jQuery('#eodit').find('input[name=title]').val(b.find('img').attr('alt'));
		$("#eodit").dialog({       
			show: 'explode',       
			hide: 'explode',       
			width: '440px'     
		});  
	});
	$(".delete").click(function() {
		var b = jQuery(this);
		var id = jQuery(this).attr('rel');
		if (confirm('are you sure?')) {
			jQuery.ajax({
				url: "/video.html",
				type: "POST",
				cache: false,
				data: "act=delete&delete="+id,
				success: function(data) {
					b.parent().remove();
				}
			});
			
		}
	});
  });

 $('.phone, .num_person').live('keypress',function(event){if(event.which == 45) return true;if(!((event.which >= 48 && event.which <= 57) || event.which==8 || !event.which))return false;});              

 var lbConfig = {
     overlayBgColor: '#000',
     overlayOpacity: 0.6,
     imageBlank  : 'img/ligthbox/blank.gif',
     imageLoading: 'img/lightbox/ico-loading.gif',
     imageBtnClose: 'img/lightbox/btn-close.gif',
     imageBtnPrev: 'img/lightbox/btn-prev.gif',
     imageBtnNext: 'img/lightbox/btn-next.gif',
     containerResizeSpeed: 350,
     txtImage: '',
     txtOf: ''
 };
 
 // init Lightbox
 $('a.lbox').lightBox(lbConfig);
  
   
 // init product category menu items 
 $('.ct').live('click',function(e){
        var ct = $(this);
        var pos = ct.offset();
        if(ct.parent().siblings().length > 0)
        {
            ct.parent().siblings().find('.data').slideUp(200,function(){$(this).remove();});
        }
        if(ct.siblings('.data').length > 0)
        {
            ct.siblings('.data').animate({opacity:0.2},'fast').slideUp(200,function(){$(this).remove();});
            return false;
        }
        $('.indicator').css('left',pos.left+ct.innerWidth()-85).css('top',pos.top+4).show();
        $.post('cms/getkey',{'key':ct.attr('id').substr(3),'model':'Category'},function(data){
            if(data.error == undefined)
            {
                var str = '<div class="data" style="display:none;">';
                if(data.categories.length > 0)
                {
                    for(var i in data.categories)
                    {
                        str += '<div class="category"><h3 class="ct" id="ct-'+data.categories[i].id+'">'+data.categories[i].name+'</h3></div>';
                    };
                }
                if(data.products.length > 0)
                {
                    var cls = ct.parent().html().toLowerCase().indexOf('<h3') != -1 ? 'small' : '';
                    str += '<ul class="product '+cls+'">';
                    for(var i in data.products)
                    {
                        str += '<li><h4 class="pr" id="pr-'+data.products[i].id+'"><div style="float:right;text-align:right;"><span>'+data.products[i].price.replace('.',',')+'</span></div>'+data.products[i].name+'</h4><p>'+data.products[i].description+'</p></li>';
                    }
                        str += '</ul>';
                }
                str += '</div>';    
                if(str != '')$(str).appendTo(ct.parent()).css('opacity','0.2').slideDown(200).animate({opacity:1},'slow');
                var image_file = data.root.image;
                if($.trim(image_file) != '')
                {  
                    $('#thumbs').stopSlideShow(); 
                    $('#thumbs a').attr('href','images/cms/'+data.root.key+'/'+image_file);
                    my_slideshowActive = false; 
                    $('#thumbs').superbgimage({reload:true}).hide();
                }
            }
            else
            {
                alert(data.error);
            }
            $('.indicator').hide();
        },'json');
    });
    

    // Options for SuperBGImage
    $.fn.superbgimage.options = {
        transition: 1, // fadeIn
        slideshow: 0, // 0-none, 1-autostart slideshow
        speed: 'slow' // animation speed
    };

    // initialize SuperBGImage
    $('#thumbs').superbgimage().hide();

     
    $('pr').live('click',function(){
        var pr = $(this);
    })
    
   if( $('.msg p').html().length )
   {
       $('.msg').fadeIn('slow');
       setTimeout(function(){$('.msg').fadeOut('slow')},3000);
   }

   $('.cms .cmng .category').hover(function(){var cat =$(this);cat.css('background-color','#fafafa');cat.find('div').eq(0).show()},function(){var cat = $(this);cat.css('background-color','transparent');cat.find('div').eq(0).hide()});
   $('.cms .cmng .prod').hover(function(){var prod =$(this);prod.find('div').eq(0).show()},function(){var prod = $(this);prod.find('div').eq(0).hide()});
 
});

function sec( elm )
{
    $('.'+elm+' h1').css('cursor','pointer').live('click',function(){
      $('#content').css('padding','0').fadeOut(400).html('').fadeIn('fast');
      $(this).siblings().toggleClass(function(i,cls){
            cls.indexOf('expand') != -1 ? $(this).slideUp(300): $(this).slideDown(300);
            return 'expand';
      });
      $('#content').css('background','transparent');
    }).siblings().hide();
}

