var path = document.location.pathname;
if (path.charAt(path.length - 1) != '/') 
    path = path + '/';


function ie6_width(min, max){
    w = document.documentElement.clientWidth;
    return (w <= min) ? min + "px" : ((w >= max) ? max + "px" : "auto");
}

var simpleUppod = {
    load: function(obj_id, swf_path, flv_path, skin_path, flv_name, w, h){
        var width = (w != undefined) ? w : 320;
        var height = (h != undefined) ? h : 240;
        if (obj_id == undefined) 
            alert("obj_id - is undefined");
        if (swf_path == undefined) 
            alert("swf_path - is undefined");
        if (flv_path == undefined) 
            alert("flv_path - is undefined");
        if (skin_path == undefined) 
            alert("skin_path - is undefined");
        str = '';
        //  if( flv_name != undefined )
        //    str += '<div class="title">'+flv_name+'</div>';
        str += '<object classid="clsid27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '" id="spin_video" align="middle">';
        str += '<param name="allowScriptAccess" value="sameDomain" />';
        str += '<param name="allowFullScreen" value="true" />';
        str += '<param name="movie" value="' + swf_path + '" />';
        str += '<param name="menu" value="false" />';
        str += '<param name="quality" value="high" />';
        str += '<param name="wmode" value="transparent" />';
        str += '<param name="bgcolor" value="#fff" />';
        str += '<param name="FlashVars" value="st=' + skin_path + '&file=' + flv_path + '&autolay" />';
        str += '<embed src="' + swf_path + '" FlashVars="st=' + skin_path + '&file=' + flv_path + '&autolay" menu="false" quality="high" wmode="transparent" bgcolor="#fff" width="' + width + '" height="' + height + '" name="spin_video" align="middle" allowScriptAccess="sameDomain" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
        str += '</object>';
        $("#" + obj_id).empty().append(str);
    }
}

var site = {

    index: -1,
    timer: null,
    
    init: function(){
        var nv = navigator.appName, ua = navigator.userAgent;
        
        $('.lightbox').lightbox();
        $('.lightbox-layout').lightbox();
		
		$('.tab-tool .h3').click(function() {
			var i = $('.tab-tool .h3').index(this);
			var $con = $('.dwrap .tab-con').eq(i);
			$('.dwrap .tab-con').removeClass('active').hide();
			$('.tab-tool .h3').removeClass('current').eq(i).addClass('current');
			
			$con.addClass('active').show();
			$.slider[i].recalc_size();
		});
		
		$('.tab-con').picbox({
			range : 104
		});
		
		$('.field-obj').each(function() {
			var max_h = 0;
			$('.obj-info .address', this).each(function() {
				if( max_h < $(this).height() ) {
					max_h = $(this).height();
				}
			});
			if( max_h ) {
				$('.obj-info .address', this).height( max_h );
			}
		});
		
        $('form.validate').validate({
            hightlight_error: function($field, error_type){
                var $msg_target;
                if ($field.is(':radio')) 
                    $msg_target = $field.parents('fieldset');
                else 
                    $msg_target = $field;
                
                $field.addClass('field-error').addClass('field-error-' + error_type);
                var error_message = $field.attr('xv:' + error_type + '-msg');
                
                if (error_message && !$field.data('error-message')) {
                    var id = 'error-message-' + Math.round(Math.random() * 1e16);
                    $msg_target.after('<span id="' + id + '" class="field-error-message">' + error_message + '</span>');
                    $field.data('error-message', id);
                }
                else {
                    $('#' + $field.data('error-message')).show();
                }
                
                setTimeout(function(){
                    $field.removeClass('field-error');
                    $('#' + $field.data('error-message')).hide();
                }, 5000);
            }
        });
		
        $('.mask').mask('+9 (999?) 999-9999');
        
        
        $('form.validate').submit(function(){
            var $form = $(this), $root = $form.parent();
            
            if (!$form.hasClass('xv-valid')) {
                return false;
            }
            
            $.ajax({
                url: $form.attr('config:uri'),
                type: $form.attr('config:method'),
                dataType: 'json',
                data: $form.serialize(),
                
                success: function(data){
                    switch (data.state) {
                        case 'error':
                            $('#done', $root).show().find('td').empty().append(data.message.join('<br/>'));
                            break;
                        case 'done':
                            $('#done', $root).show().height($form.height()).addClass('mes-highlight').find('td').html(data.message);
                            $form.hide();
                            break;
                    }
                    
                    $form.find('input').attr('disabled', false);
                    if (data.state == 'error') {
                        setTimeout(function(){
                            $('#done', $root).fadeOut(200);
                        }, 6000);
                    }
                }
            });
            return false;
        });
		
		$('.vote-vote').click(function() {
			var $form = $(this).parents('form');
			
			var vts = $form.find('input[type="radio"]').is(':checked');
			
			if( !vts ) {
				alert('Вы должны выбрать один из вариантов голосования!');
				return false;
			}
			
			var form_field = $form.serialize();
			form_field = form_field + '&type=polling';
			$.ajax({
				url : $form.attr('config:uri'),
				type : $form.attr('config:method'),
				dataType : 'json',
				data : form_field,
				success : function( data ) {
					var box = $('<div></div>').hide();
					if( data ) {
						for(var i = 0; i < data['votes'].length; i++) {
							var div = $('<div class="field-set"><span class="vote-title" id="title'+i+'"></span><span class="vote-line"><img src="/images/blank.gif" id="vote'+i+'" style="width: 1px;"/></span>&nbsp;&nbsp;<span class="vote-per" id="per'+i+'"></span></div>');
							var vote = data['votes'][i];
							
							$('#per'+i, div).html( vote.percent + '%' );
							$('#title'+i, div).html( vote.title );
							$('#vote'+ i, div).width( vote.width === 0 ? 1 : vote.width );
							box.append(div);
						}
						$form.find('.work-form').empty().append(box);
						box.show();
						$('.vote-vote').remove();
					}
				}
			})
			return false;
		});
		
		$('.menu dt:not(.have-child)').hover(function() {
			$(this).addClass('active');
		}, function() {
			$(this).removeClass('active');
		});
		$('.menu dt.have-child').hover(function() {
			$(this).addClass('ch-active');
			$(this).find('.sub-level:eq(0)').show();
		}, function() {
			$(this).removeClass('ch-active');
			$(this).find('.sub-level:eq(0)').hide();
		});
		
		$('.visual-table').each(function() {
			$('th:last-child', this).after('<th class="last"><div class="ne"></div></th>');
			$('th:first-child', this).addClass('second');
			$('th:first-child', this).before('<th class="first"><div class="nw"></div></th>');
			
			$('td:last-child', this).addClass('pre-last');
			$('td:last-child', this).after('<td class="last"><div class="ne"></div></td>');
			$('td:first-child', this).addClass('second');
			$('td:first-child', this).before('<td class="first"><div class="nw"></div></td>');
			
			$('tr:not(:first):even', this).addClass('even');
		})
    }
};

function __(){
    try {
        console.log(__.caller, arguments);
    } 
    catch (e) {
    }
}

$(site.init);

$(window).load(function() {
	$('.tab-tool .h3:eq(0)').click();
	
	if ($.browser.msie && $.browser.version <= 8 ) {
		$('.visual-left,.visual-right,.visual-image,.visual-fix').each(function(){
			var w = $(this).width(), h = $(this).height();
			
			w = $.browser.version <= 7 ? w + 2 : w;
			h = $.browser.version <= 7 ? h + 2 : h;
			
			var $wrap = $('<div class="dwrap"><i class="cor-ne"></i><i class="cor-nw"></i><i class="cor-se"></i><i class="cor-sw"></i></div>');
			$(this).after($wrap);
			$wrap.css({
				width: w,
				height: h
			});
			$wrap.addClass(this.className);
			$(this).removeAttr('class');
			$wrap.append(this);
		});
		
		$('div.h2,h2.title').each(function() {
			var $wr = $('<div class="title-wr"></div>');
			$(this).append('<i class="ne"></i><i class="nw"></i><i class="sw"></i><i class="se"></i>');
			$(this).after($wr);
			$wr.append(this);
		});
		
		$('a.pager,strong.current').append('<i class="ne"></i><i class="nw"></i><i class="sw"></i><i class="se"></i>');
	}
})

