$(document).ready(function() { 
	$('form.ajax').live('submit', function(){
		$(this).find("input[name='ajax']").val('1');
		$(this).ajaxSubmit({
			dataType: 'json',
	
			beforeSubmit: function(a,form,o) {
				$('.errors').hide();
			},
	
			success: function(response, statusText, xhr, form)
			{
				var update = $(form).find("input[name='update']").val();
				if(response.code)
				{
					response.code = prepare(response.code);
					$(form).find(".code").html(response.code).show();
				}
				if(! update && response.errors)
				{
					$.each(response.errors, function(k, val)
					{
						val = prepare(val);
						$(form).find(".error"+(k != 0 ? "-"+k : '')).html(val).show();
					});
				}
				if(response.success)
				{
					$(form).clearForm();
					$(form).find('input[type=file]').last().addClass('last');
					$(form).find('input[type=file]').each(function(){
						if(! $(this).has('last'))
						{
							$(this).next('<br>').remove();
							$(this).remove();
						}
					});
					$(form).find('input[type=file]').removeClass('last');
				}
				if(response.add)
				{
					response.add = prepare(response.add);
					if($(form).find("input[name='first']").val())
					{
						$(form).before(response.add);
						$(form).find("input[name='first']").val('');
					}
					else
					{
						$('.'+$(form).attr("id")).append(response.add);
						$('.'+$(form).attr("id")).show();
					}
				}
				if(response.basket && response.basketcount)
				{
					$('.basket-summ').text(response.basket);
					$('.basket-count').text(response.basketcount);
				}
				if(response.form)
				{
					$(form).html(prepare(response.form));
				}
				if(response.form_hide)
				{
					if(response.target && ! response.data)
					{
						$(response.target).hide();
					}
					else
					{
						$(form).hide();
					}
				}
				if(response.redirect)
				{
					window.location = response.redirect;
				}
				if(response.data && response.target)
				{
					$(response.target).html(prepare(response.data));
				}
				return false;
			}
		});
		return false;
	});

	$("form input[type=file]").live('change', function() {
		$(this).after($(this).clone(true).val(''));
		$(this).after($('<br>'));
	});

	$("select[name=depend]").change(function() {
		var price = $(this).find('option:selected').attr('rel');
		$(this).parents('form').find('span').text(price);
	});

	$('.inpnum').live('keydown', function(evt) {
		evt = (evt) ? evt : ((window.event) ? event : null);

		if(evt)
		{
			var elem = (evt.target)
			? evt.target
			:(
			  evt.srcElement
			  ? evt.srcElement
			  :null
			);
		 
			if(elem)
			{
				var charCode = evt.charCode
				?evt.charCode
				:(evt.which
				  ?evt.which
				  :evt.keyCode
				  );
		 
				if ((charCode < 32 ) ||
					(charCode > 44 && charCode < 47) || 
					(charCode > 95 && charCode < 106) || 
					(charCode > 47 && charCode < 58) || charCode == 188 || charCode == 191 || charCode == 190  || charCode == 110)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}
	});
	$('.code-update').live('click', function(){
		$(this).parents("form").find("input[name=update]").val("1");
		$(this).parents("form").submit();
	});

       videoToLightbox(480,309);
       if ($.prettyPhoto != undefined) 
           $(document).ready(function(){
                $("a[rel^='prettyPhoto']").prettyPhoto({
                 deeplinking: false
                });
            });
});

function prepare(string)
{
	string = str_replace('&lt;', '<', string);
	string = str_replace('&gt;', '>', string);
	return string;
}

function str_replace(search, replace, subject, count)
{
	f = [].concat(search),
	r = [].concat(replace),
	s = subject,
	ra = r instanceof Array, sa = s instanceof Array;
	s = [].concat(s);
	if(count)
	{
		this.window[count] = 0;
	}
	for(i = 0, sl = s.length; i < sl; i++)
	{
		if(s[i] === '')
		{
			continue;
		}
		for(j = 0, fl = f.length; j < fl; j++)
		{
			temp = s[i]+'';
			repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
			s[i] = (temp).split(f[j]).join(repl);
			if (count && s[i] !== temp)
			{
				this.window[count] += (temp.length-s[i].length)/f[j].length;
			}
		}
	}
	return sa ? s : s[0];
}

// video
var videos = new Array();
var previews = new Array();

function videoToLightbox(width, height){
    var clipid = 0;
    var videogalmode = 0;
    $('object[data*="vimeo.com"]').each(function(index, value){
        videogalmode = 'thumbnail_large';
        if ($(this).closest('div.videogallery').length === 1) videogalmode = 'thumbnail_medium';
        videos[index] = $(this).attr('data');
        //videos[index] = $('<div>').append($(this).clone()).remove().html();
        clipid = videos[index].substr(videos[index].indexOf('clip_id=', 0)+8, videos[index].indexOf('&', videos[index].indexOf('clip_id=', 0)) - videos[index].indexOf('clip_id=', 0)-8);     
        $.getJSON('http://vimeo.com/api/v2/video/' + clipid +'.json?callback=?', function(data) {
            //previews[index] = '<img class="videopreview" alt="' + data[0].title.split("\"").join("&quot;") + '" title="' + data[0].title.split("\"").join("&quot;") + '" src="' + data[0][videogalmode] + '" />';
            if (videogalmode === 'thumbnail_medium') previews[index] = '<div class="videopreview" alt="' + data[0].title.split("\"").join("&quot;") + '" title="' + data[0].title.split("\"").join("&quot;") + '" style="background:url(' + data[0][videogalmode] + ')"><img alt="" src="/img/play_show_2.png" /></div>';
            else previews[index] = '<div class="videopreviewbig" alt="' + data[0].title.split("\"").join("&quot;") + '" title="' + data[0].title.split("\"").join("&quot;") + '" style="background:url(' + data[0][videogalmode] + ')"><img alt="" src="/img/play_show_big.png" /></div>';
            previewLoad();
        });
    });
    $('div.videogallery').append('<div style="clear:both"></div>');
}

function previewLoad(){
    var cnt=0;
    for (var i in previews){
	if (i) cnt++
    }
    var objstr;
    
    if (cnt == videos.length) 
        $('object[data*="vimeo.com"]').each(function(index, value){
                objstr = $(this).attr('data');
                //objstr = $('<div>').append($(value).clone()).remove().html();
                clipid = objstr.substr(objstr.indexOf('clip_id=', 0)+8, objstr.indexOf('&', objstr.indexOf('clip_id=', 0)) - objstr.indexOf('clip_id=', 0)-8);     
               $(value).replaceWith('<a rel="prettyPhoto[video]" href="http://vimeo.com/' + clipid + '">' + previews[index] + '</a>');
        });

    if (cnt > 0){
       /* if ($.prettyPhoto == undefined){
            var link = $("<link>");
            link.attr({
                type: 'text/css',
                rel: 'stylesheet',
                href: '/css/prettyPhoto.css'
            });
            $("head").append(link ); 
            $.getScript('/js/jquery.prettyPhoto.js', function(){
                $("a[rel^='prettyPhoto[video]']").prettyPhoto({
                    deeplinking: false
                });
            });
        } else { */
            $("a[rel^='prettyPhoto[video]']").prettyPhoto({
                    deeplinking: false
            });
       // }
    } 
}
