/* login / my account block,  out of jquery */
var element_content = null;
$(function(){


/* login / my account block */
/*LG*/
$('#my_account_link').mouseover(function(){$('#my_account_block').show();});
$('#my_account_block, #my_account_link').click(function(event){event.stopPropagation()});

/*/LG*/
/* check for login form */
        $('#login_form').submit(function(){
                if($('#login').val().length < 1){
                        $('#login').focus();
                        return false;
                } else if($('#password').val().length < 1){
                        $('#password').focus();
                        return false;
                } else {
                        return true;
                }
        });

        /* create onClick event for the blue remove buttons */
        $('#content div.item_wrap div.top div.item div.added ul li a').live('click', function(){
                var lid = $('#lightbox #mylightbox').val();
                var cid = $(this).parent().parent().parent().parent().children('a').children('img').attr("id");
                var par = $(this).parent().parent().parent();
                $.ajax({
                    type: "POST",
                    url: '/Lightbox/ajax_delContent',
                    data: 'lid='+lid+'&cid='+cid,
                    success: function(msg){
                        if (msg!=''){
                                alert(msg);
                        }
                        else{
                                $('#lightbox div[image_preview="' + cid + '"]').remove();
                                selopt = $('#lightbox select[name="mylightbox"] option:selected').text();
                                pos = selopt.indexOf('(');
                                pos2 = selopt.indexOf(')');
                                num = selopt.substr(pos+1, pos2-pos-1);
                                resstr = selopt.substr(0, pos+1) + (parseInt(num)-1) + ')';
                                $('#lightbox select[name="mylightbox"] option:selected').text(resstr);
                                par.hide();
                                $('#lightbox div[image_preview="' + cid + '"]').children('.top').children('.item').children('a').children('img').removeAttr('selected_content');
                                $('#content div[image_preview="' + cid + '"] .top .item a img').removeAttr("selected_content");
                        }
                    }
            	});
            return false;
        });

        /* to remember what photo is already added to lightbox and what not */
        var counter = 1;
        $('.photo>div, .photo_large>div').each(function(){
                id = $(this).children('.top').children('.item').children('a').children('img').attr('id');
                $(this).attr('image_preview', id);
                });


/* lightbox_control  open/close */


if($('#lightbox').size()!=0)
{
        var lightbox_state = 0; /* lightbox is closed on init */

                /* проставление блоков удаления для контента */

                $('#sortable .item_wrap').each(function(){
                        id_photo_lb = ($(this).children('.top').children('.item').children('a').children('img').attr('id'));
                        $('#content div[image_preview="' + id_photo_lb + '"]').find('.added').show();
                        var selected_content = $('#lightbox div[image_preview="' + id_photo_lb + '"]').children('.top').children('.item').children('a').children('img').attr('selected_content');
                        $('#content div[image_preview="' + id_photo_lb + '"]').children('.top').children('.item').children('a').children('img').attr('selected_content', selected_content);
                });


                /* окончание */

        function lightbox_toggle() {
                lightbox_state = lightbox_state ? 0 : 1;
                if(lightbox_state){
                        $('#lightbox .box').css({display: 'block'});
                        $('#lightbox').css({height: '205px'});
                        $('#maincontent').css({bottom: '205px'});
                        if($.browser.msie){$('.inside').css({paddingBottom : '205px'});};
                        $('#lightbox_control_container').toggleClass('up').toggleClass('down');

                } else {
                        $('#lightbox .box').css({display: 'none'});
                        $('#lightbox').css({height: '40px'});
                        $('#maincontent').css({bottom: '40px'});
                        if($.browser.msie){$('.inside').css({paddingBottom : '40px'});};
                        $('#lightbox_control_container').toggleClass('up').toggleClass('down');
                }
        }


        $('#lightbox_control_toggle_arrow').click(function(){
                lightbox_toggle();
                $(this).blur();
        });


        /* add to lightbox from photo ballon */
    $('a#photo_nav_addtolightbox_link, a#video_nav_addtolightbox_link').click(function(){
        if (lightbox_state == 0)  lightbox_toggle();
                var parent = $(this);
                var parentwin = window.self;
                var cid = 0;
                if (explode_url[3] == 'Content' && parseInt(explode_url[4])>0) {
                    // страница одного контента
                    var lid = $('#alllb').val();
                    cid = $('#onecontentId').val();
                }
                else if(explode_url[3] == 'Content' && (parseInt(explode_url[4])==0 || isNaN(parseInt(explode_url[4])))){
                    var lid = $('#mylightbox').val();
                    if ($(this).parent().parent().parent().children('#photo_nav_detail').length) {
                    	var our_id = $(this).parent().parent().parent().children('#photo_nav_detail').children('#photo_nav_showfulldetail').find('#our_id').text();
                    }
                    else{
                    	var our_id = $(this).parent().parent().parent().children('#footage_nav_detail').children('#footage_nav_showfulldetail').find('#video_our_id').text();
                    }

                    var parent_elem = $("#draggable .item_wrap:contains(" + our_id+ ")");
					if (parent_elem.html() == null && element_content == null) {
						element_content = $('#lightbox .item_wrap:contains(' + our_id + ')');
					}
					else if (parent_elem.html() && element_content == null) {
						element_content = $("#draggable .item_wrap:contains(" + our_id+ ")");
					}

                    if ($.cookies.get("sizecontent") == "normal" || $.cookies.get("sizecontent") == null) {
                       cid = $(".photo .item_wrap:contains(" + our_id+ ")").children('.top').children('.item').children('a').children('img').attr("id");
                    }
                    else{
                       cid = $(".photo_large .item_wrap:contains(" + our_id+ ")").children('.top').children('.item').children('a').children('img').attr("id");
                    }
                    if (isNaN(cid)) {
                    	if ($('#lightbox').length) {
                    		cid = $('#currentcontent').val();
                    	}
                    }
                }
		/*
		  	В зависимости от текста выполняются действия по
			добавлению контента в лайтбокс или его удаление из лайтбокса
		*/
        if ($(this).text() == 'Add to lightbox')
        {
                if (lid){
                        $.ajax({
                            type: "POST",
                            url: '/Lightbox/ajax_addContent',
                            data: 'lid='+lid+'&cid='+cid,
                            success: function(msg){
                                if (msg!=''){
                                        alert(msg);
                                }
                                else{
                                    if ((explode_url[3] == 'Content' && (parseInt(explode_url[4])==0 || isNaN(parseInt(explode_url[4])))) || (explode_url[3] == 'popup' && parseInt(explode_url[4])>0)) {
                                        /*parentwin.$('#lightbox .lightbox_photo').append('<div class="item_wrap">' + parent_elem + '</div>');
                                        parentwin.$('#lightbox .lightbox_photo .item_wrap div.item .add').hide();
                                        parentwin.$('#sortable img').removeAttr('width').animate({ height: '120' } );*/

                                        if ($.cookies.get("sizecontent") == "normal") {
                                                parentwin.$('#lightbox .lightbox_photo').prepend(element_content.clone(true));
                                        }
                                        else{
                                            var src_img_small = element_content.clone(true);
                                            src_img = src_img_small.find('img').attr('src');
                                            if (src_img.indexOf('work_') != -1) {
                                                str_work_img = src_img.substr(0, pos_work_img);
                                                str_pos_ = src_img.indexOf('_');
                                                str_work_after = src_img.substr(str_pos_+1);
                                                small_src = str_work_img + 'small_' + str_work_after;
                                                src_img_small.find('img').attr('src', small_src);
                                            }
                                            parentwin.$('#lightbox .lightbox_photo').prepend(src_img_small.clone(true));
                                        }


                                    	//parentwin.$('#lightbox .lightbox_photo .item_wrap div.item .add').hide();
                                        element_content.find('.added').show();
                                        parentwin.$('#lightbox .lightbox_photo img:first').removeAttr('width').animate({ height: '120' } );

                                        /*parentwin.$('#lightbox .lightbox_photo').append('<div class="item_wrap">' + parent_elem + '</div>');
                                        parentwin.$('#content div.preview_' + $('#wincont').attr('preview')).find('.added').show();
                                        parentwin.$('#lightbox .lightbox_photo img:first').removeAttr('width').animate({ height: '120' } );*/

                                    	selopt = parentwin.$('select[name="mylightbox"] option:selected').text();
                                        pos = selopt.indexOf('(');
                                        pos2 = selopt.indexOf(')');
                                        num = selopt.substr(pos+1, pos2-pos-1);
                                        resstr = selopt.substr(0, pos+1) + (parseInt(num)+1) + ')';
                                        parentwin.$('select[name="mylightbox"] option:selected').text(resstr);
                                    }
                                    parent.text('Remove from lightbox');
                                }
                            }
                    });
                }

            /*$('#lightbox .lightbox_photo').prepend($('#content div.preview_' + $('#wincont').attr('preview')).clone());
            $('#content div.preview_' + $('#wincont').attr('preview')).find('.added').show();
            $('#lightbox .lightbox_photo img:first').removeAttr('width').animate({ height: '120' } );
            $(this).text('Remove from lightbox');*/
        }
        else
        {

            if (lid){
            	if ($(this).parent().parent().parent().children('#photo_nav_detail').length) {
            		var objid = $(this).parent().parent().parent().children('#photo_nav_detail').children().children().children().children().children('td#our_id').text();
            	}
            	else{
            		var objid = $(this).parent().parent().parent().children('#footage_nav_detail').children().children().children().children().children('#video_our_id').text();
            	}
                var par_photo = parentwin.$("div#lightbox .lightbox_photo .item_wrap:contains(" + objid + ")");
                $.ajax({
                type: "POST",
                url: '/Lightbox/ajax_delContent',
                data: 'lid='+lid+'&cid='+cid,
                success: function(msg){
                    if (msg!=''){
                            alert(msg);
                    }
                    else{
                        if ((explode_url[3] == 'Content' && (parseInt(explode_url[4])==0 || isNaN(parseInt(explode_url[4])))) || (explode_url[3] == 'popup' && parseInt(explode_url[4])>0)) {
                            ip = element_content.attr('image_preview');
                            if (isNaN(ip)) {
                            	if ($('#lightbox').length) {
                            		ip = $("#lightbox .item_wrap:contains(" + our_id+ ")").attr('image_preview');
                            	}
                            }
                       		parentwin.$('#content div[image_preview="' + ip + '"]').find('.added').hide();
                            parentwin.$('#lightbox .lightbox_photo div[image_preview="' + ip + '"]').remove();

                            /*$('#content div.preview_' + $('#wincont').attr('preview')).find('.added').hide();
            				$('#lightbox .lightbox_photo div.preview_' + $('#wincont').attr('preview')).remove();*/
                            //par_photo.remove();
                            selopt = parentwin.$('select[name="mylightbox"] option:selected').text();
                            pos = selopt.indexOf('(');
                            pos2 = selopt.indexOf(')');
                            num = selopt.substr(pos+1, pos2-pos-1);
                            resstr = selopt.substr(0, pos+1) + (parseInt(num)-1) + ')';
                            parentwin.$('select[name="mylightbox"] option:selected').text(resstr);
                        }
                        parent.text('Add to lightbox');
                    }
                }
                });
        	}

            /*$('#content div.preview_' + $('#wincont').attr('preview')).find('.added').hide();
            $('#lightbox .lightbox_photo div.preview_' + $('#wincont').attr('preview')).remove();
            $(this).text('Add to lightbox');*/
        }
        return false;
    });


    $('#lightbox .item_wrap div.item ul li.rem_photo').live('click', function(){
        var lid = $('#mylightbox').val();
        var cid = $(this).parent().parent().parent().children('a').children('img').attr("id");
        var parent_elem = $(this).parent().parent().parent().parent().parent();
            $.ajax({
                type: "POST",
                url: '/Lightbox/ajax_delContent',
                data: 'lid='+lid+'&cid='+cid,
                success: function(msg){
                    if (msg!=''){
                            alert(msg);
                    }
                    else{
                        ip = parent_elem.attr('image_preview');
                		$('#content div[image_preview="' + ip + '"]').find('.added').hide();
                        parent_elem.remove();
                        selopt = $('select[name="mylightbox"] option:selected').text();
                        pos = selopt.indexOf('(');
                        pos2 = selopt.indexOf(')');
                        num = selopt.substr(pos+1, pos2-pos-1);
                        resstr = selopt.substr(0, pos+1) + (parseInt(num)-1) + ')';
                        $('select[name="mylightbox"] option:selected').text(resstr);
                        $('#content div[image_preview="' + cid + '"] .top .item a img').removeAttr("selected_content");
                    }
                }
            });
    });

    /* add button in search results
    $('#content .item_wrap div.item ul li.add_photo').click(function(){
        if (lightbox_state == 0)
            lightbox_toggle();
            $('#lightbox .lightbox_photo').append($(this).parent().parent().parent().parent().parent().clone());
                $('#lightbox .lightbox_photo .item_wrap div.item .add').hide();
            $(this).parent().parent().parent().find('.added').show();
            //$(this).parent().parent().parent().parent().parent().draggable('disable');
        $('#sortable img').removeAttr('width').animate({ height: '120' } );
    });*/

        //      add button in search results
    $('#content .item_wrap div.item ul li.add_photo').click(function(){
        var lid = $('#mylightbox').val();
        var cid = $(this).parent().parent().parent().children('a').children('img').attr("id");
        var parent_elem = $(this).parent().parent().parent().parent().parent();
        var par_content = $(this).parent().parent().parent();
        $.ajax({
                type: "POST",
                url: '/Lightbox/ajax_addContent',
                data: 'lid='+lid+'&cid='+cid,
                success: function(msg){
	                if (msg!=''){
	                        alert(msg);
	                }
	                else{
	                	if (lightbox_state == 0){
	                    	lightbox_toggle();
	                	}
                        if ($.cookies.get("sizecontent") == "normal") {
                        	$('#lightbox .lightbox_photo').prepend(parent_elem.clone(true));
                        }
                        else{
                            var src_img_small = parent_elem.clone(true);
                            src_img = src_img_small.find('img').attr('src');
                            pos_work_img = src_img.indexOf('work_');
                            if (pos_work_img != -1) {
                                str_work_img = src_img.substr(0, pos_work_img);
                                str_pos_ = src_img.indexOf('_');
                                str_work_after = src_img.substr(str_pos_+1);
                                small_src = str_work_img + 'small_' + str_work_after;
                                src_img_small.find('img').attr('src', small_src);
                            }
                            $('#lightbox .lightbox_photo').prepend(src_img_small.clone(true));
                        }

		                $('#lightbox .lightbox_photo .item_wrap div.item .add').hide();
		                par_content.find('.added').show();
		                $('#lightbox .lightbox_photo img:first').removeAttr('width').animate({ height: '120' } );

		                selopt = $('#lightbox select[name="mylightbox"] option:selected').text();
                        pos = selopt.indexOf('(');
                        pos2 = selopt.indexOf(')');
                        num = selopt.substr(pos+1, pos2-pos-1);
                        resstr = selopt.substr(0, pos+1) + (parseInt(num)+1) + ')';
                        $('#lightbox select[name="mylightbox"] option:selected').text(resstr);
	                }
	                intcid = cid.substring(1);
	                if (isNaN(cid)) {
	                	$('#content div[image_preview="' + cid + '"]').children('.top').children('.item').children('a').children('img').attr('selected_content', 'full');
		                $('#lightbox div[image_preview="' + cid + '"]').children('.top').children('.item').children('a').children('img').attr('selected_content', 'full');
		                $('#lightbox div[image_preview="' + cid + '"]').children('.top').children('.item').append('<span class="search-music-numbers-tracks">full</span>');
	                }

	        }
        });
    });

    /* hide/show text in lightbox */
    $('#lightbox_control_hide_text').click(function(){
            $('#lightbox_container').toggleClass('block');
            $('#lightbox_container').toggleClass('notext');
    });
    $('#lightbox_control_hide_text').toggle(function(){
            $('#lightbox_control_hide_text_label').html('Show')
    },function(){
            $('#lightbox_control_hide_text_label').html('Hide')
    });

	$('#lightbox .item_wrap div.item ul li.buy_photo').live('click', function(){
		var cid = $(this).parent().parent().parent().children('a').children('img').attr("id");
		var cselected = $('#lightbox div[image_preview="' + cid + '"]').children('.top').children('.item').children('a').children('img').attr('selected_content');
		if (cselected) {
			if (cselected == 'full') {
				location.href = '/Orders/'+cid;
			}
			else{
				location.href = '/Orders/'+cid+'&contents='+cselected;
			}
		}
		else{
			location.href = '/Orders/'+cid;
		}
	});

};

	$('#content .item_wrap div.item ul li.buy_photo').click(function(){
		var cid = $(this).parent().parent().parent().children('a').children('img').attr("id");
		location.href = '/Orders/'+cid;
	});

        /* actions with lightbox on inside.html */
    $('#new_lightbox>a').click(function(){
        $('#send_lightbox .win_block').hide();
        $('#new_lightbox .win_block').show();
    });
    $('#send_lightbox>a').click(function(){
        $('#new_lightbox .win_block').hide();
        $('#send_lightbox .win_block.send_inside').show();
    });

    $('#new_lightbox, #send_lightbox').click(function(event){
        event.stopPropagation();
    });

        /* radiobuttons select in Find Similar section */
    $('.photo_similar a.select_all').click(function(){
        $('.photo_similar table input:checkbox').attr('checked', 'checked');
        return false;
    });
    $('.photo_similar a.clear_all').click(function(){
        $('.photo_similar table input:checkbox').removeAttr('checked');
        return false;
    });

        $('.listing_bot input.text').bind('keydown keypress keyup',function() {
                if ($(this).val().length > 3)
                    $(this).val($(this).val().substring(0,3));
                if ( parseInt($(this).val()) > 0) $(this).val( parseInt($(this).val()) );
                else $(this).val('');
    });

        $('#lightbox_add').click(function(){
        $('.win_block.add').show();
        $('.win_block_del').hide();
        $('.win_block.send').hide();
        $('.win_block.edit').hide();
        return false;
    });

    /* Lightbox page -> Edit lightbox */
    $('#lightbox_edit').click(function(){
        $('.win_block.add').hide();
        $('.win_block_del').hide();
        $('.win_block.send').hide();
        $('.win_block.edit').show();
        return false;
    });

    /* Lightbox page -> Delete lightbox */
    $('#lightbox_delete').click(function(){
        $('.win_block.add').hide();
        $('.win_block.edit').hide();
        $('.win_block.send').hide();
        $('.win_block_del').show();
        return false;
    });

    /* Lightbox page -> Send lightbox */
    $('#lightbox_send').click(function(){
        $('.win_block.add').hide();
        $('.win_block.edit').hide();
        $('.win_block_del').hide();
        $('.win_block.send').show();
    });

    $('#lightbox_add, #lightbox_edit, #lightbox_delete, #lightbox_send').click(function(event){
            event.stopPropagation();
    });


        /* inner baloon actions */
        var aPhoto_nav = ['photo_nav_detail', 'photo_nav_order', 'photo_nav_findsimular']
        var sPhoto_nav_selected = 'photo_nav_detail';
        var sPhoto_nav_clicked = ''
        $('.photo_nav li a').click(function(){
                $(this).blur();
                sPhoto_nav_clicked = $(this).parent().attr('id');
                sPhoto_nav_clicked = sPhoto_nav_clicked.substring(0,sPhoto_nav_clicked.indexOf('_link'))
                if(sPhoto_nav_clicked == 'photo_nav_detail'){
                        $('#photo_nav_showfulldetail_link').show();
                } else {
                        $('#photo_nav_showfulldetail_link').hide();
                }
                $('#' + sPhoto_nav_selected + '_link').removeClass('active');
                $('#' + sPhoto_nav_selected).slideUp(function(){
                        $('#' + sPhoto_nav_clicked + '_link').addClass('active');
                        $('#' + sPhoto_nav_clicked).slideDown(function(){});
                        sPhoto_nav_selected = sPhoto_nav_clicked;
                });
                return false;
        });

        $('#photo_nav_showfulldetail_link').toggle(function(){
                $('#photo_nav_showfulldetail').slideDown();
                $('#photo_nav_showfulldetail_text_label').html('Hide')
                $(this).blur();
        },function(){
                $('#photo_nav_showfulldetail').slideUp();
                $('#photo_nav_showfulldetail_text_label').html('Show');
                $(this).blur();
        });


		var aMusic_nav = ['music_nav_detail', 'music_nav_order', 'music_nav_findsimular'];
        var sMusic_nav_selected = 'music_nav_detail';
        var sMusic_nav_clicked = '';
        $('.music_nav li a').click(function(){
                sMusic_nav_clicked = $(this).parent().attr('id');
                if (sMusic_nav_clicked != 'music_nav_order')  $(this).blur();
                sMusic_nav_clicked = sMusic_nav_clicked.substring(0,sMusic_nav_clicked.indexOf('_link'))
                if(sMusic_nav_clicked == 'music_nav_detail'){
                    $('#music_nav_showfulldetail_link').show();
                }
                else if (sMusic_nav_clicked == 'music_nav_order') {
					$('#music_order_link').click();
                }
				else {
                    $('#music_nav_showfulldetail_link').hide();
                }
                $('#' + sMusic_nav_selected + '_link').removeClass('active');
                if (sMusic_nav_clicked != 'music_nav_order'){
					$('#' + sMusic_nav_selected).slideUp(function(){
	                        $('#' + sMusic_nav_clicked + '_link').addClass('active');
	                        $('#' + sMusic_nav_clicked).slideDown(function(){});
	                        sMusic_nav_selected = sMusic_nav_clicked;
	                });
	            }
                return false;
        });
        $('#music_nav_showfulldetail').slideDown();


        var aFootage_nav = ['footage_nav_detail', 'footage_nav_order', 'footage_nav_findsimular']
        var sFootage_nav_selected = 'footage_nav_detail';
        var sFootage_nav_clicked = ''
        $('.footage_nav li a').click(function(){
            $(this).blur();
            sFootage_nav_clicked = $(this).parent().attr('id');
            sFootage_nav_clicked = sFootage_nav_clicked.substring(0,sFootage_nav_clicked.indexOf('_link'))
            if(sFootage_nav_clicked == 'footage_nav_detail'){
                $('#footage_nav_showfulldetail_link').show();
            } else {
                $('#footage_nav_showfulldetail_link').hide();
            }
            $('#' + sFootage_nav_selected + '_link').removeClass('active');
            $('#' + sFootage_nav_selected).slideUp(function(){
                $('#' + sFootage_nav_clicked + '_link').addClass('active');
                $('#' + sFootage_nav_clicked).slideDown(function(){});
                sFootage_nav_selected = sFootage_nav_clicked;
        	});
            return false;
        });

        $('#footage_nav_showfulldetail').slideDown();

/* left menu with categiries on search result page */
        $('#menu_main .up > a').click(function(){
                $(this).parent().toggleClass('up');
                $(this).parent().toggleClass('down');
        })
        $('#menu_main .down > a').click(function(){
                $(this).parent().toggleClass('up');
                $(this).parent().toggleClass('down');
        })


/*LG*/$('#editorial_popup').appendTo('#searchform');/* /LG*/

      $('#editorial_choose,a#editorial_links').click(function(event){
                if($('#editorial_popup').css('display') == 'block'){
                        $('#editorial_popup').hide();
                } else {

                        $('#editorial_popup').css({'position':'absolute','top':event.pageY+3,'left':event.pageX+3}).show();
                        if(lightbox_state == 1){
                                lightbox_toggle();
                        }
                }
                event.stopPropagation();
           });


        $('#editorial_popup').click(function(event){
                event.stopPropagation();
                return true;
        });


/* balloons on search area (image | video | music) */
        var balloonLock = false;

        $("li.check_ballons").hover(function () {
            $('.win_block.add').hide();
        $('.win_block_del').hide();
        $('.win_block.send').hide();
        $('.win_block.edit').hide();
/*LG*/  $('#my_account_block').hide();/*/LG*/
                $("div.ballon").hide();
                $("#" + $(this).attr("id") + "_balloon").show();
                balloonLock = true;
        },function () {
                name = "#" + $(this).attr("id") + "_balloon";
                balloonLock = false;
                setTimeout(function(){
                        if (!balloonLock) {
                                $(name).hide();
                        }
                }, 300);
        });

        $("div.ballon").hover(function () {
                $(this).show();
                balloonLock = true;
        },function () {
                name = "#" + $(this).attr("id");
                balloonLock = false;
                setTimeout(function(){
                        if (!balloonLock){
                                $(name).hide();
                        }
                }, 300);
        });


        $('div.ballon').click(function(event){
                event.stopPropagation();
                return true;
        });

        $('li.check_ballons').click(function(event){
                event.stopPropagation();
                return true;
        });




/* lang | country selector */
        $('#country_selected').click(function(){
                $('#country_list').show();
                if(lightbox_state == 1){
                        lightbox_toggle();
                };
/*LG*/          $('#my_account_block').hide();/*/LG*/
                return false;
        })

        $('#country_list span').click(function(){
                $('#country_list').hide();
                $('#country_selected').html($(this).html());
                title = $(this).attr('title');
                $('#country_type').val( title);
        })


        $('#country_list span').hover(function(){
                $(this).addClass('hover');
        },function(){
                $(this).removeClass('hover');
        });


    /* left and right arrows which is appeared when mouse is over the balloon image  */

    $('<div>').css({background:'#000',marginRight:'-81px',width:'81px',float:'left',height:'100%'}).fadeTo(0,0.33).insertBefore('div.photo_prev a');
    $('<div>').css({background:'#000',marginLeft:'-81px',width:'81px',float:'right',height:'100%'}).fadeTo(0,0.33).insertBefore('div.photo_next a');
//    $('div.photo_prev a, div.photo_next a').css('position','relative').hide();

    $('div.photo_prev, div.photo_next').hover(
        function(){ $(this).children().show();},
        function(){ $(this).children().hide();}
    ).children().css('position','relative').hide();

    /* editorial links on editorial.html */
   /* $('a#editorial_links').click(function(){
                if($('#editorial_popup').css('style') == 'block'){
                        $('#editorial_popup').hide();
                } else {
                        $('#editorial_popup').show();
                }
                return false;
        });*/


        $('#choose_btn').click(function(){
                $('#editorial_popup').hide();
                checked = $('#editorial_popup input:checkbox:checked').length;
                if (checked == 0)
                {
                    $('a#editorial_links').text('Choose');
                    if ($('#editorial_popup>input:checkbox').attr('checked'))
                        $('span#editorial_choose').text('choose');
                    else
                        $('span#editorial_choose').text('change');
                }
                else if (checked < 4)
                {
                    links_text = new Array;
                    $('#editorial_popup input:checkbox:checked + label').each(function() {
                        links_text.push($(this).text());
                    });
                    $('a#editorial_links').text(links_text.join(', ') + ' + ' + $('#editorial_popup .column input:radio:checked + label').text());
                    if ($('#editorial_popup>input:checkbox').attr('checked'))
                        $('span#editorial_choose').text('choose');
                    else
                        $('span#editorial_choose').text('change');
                }
                else
                {
                    $('a#editorial_links').text('change');
                    if ($('#editorial_popup>input:checkbox').attr('checked'))
                        $('span#editorial_choose').text('choose');
                    else
                        $('span#editorial_choose').text('change');
                }
                return false;
        });

        $('#editorial_popup .column input:checkbox').click(function(){
            $('#editorial_popup>input:checkbox').removeAttr('checked');
        });



        $('#editorial_popup').click(function(event){
                event.stopPropagation();
                return true;
        });

        /* context suggest for search input */

    /*$('.search_line input:text').focus(function(){
        val = $(this).val();
        $('.ballon2 li').hide();
        var count = 0;
        $('.ballon2 li').each(function(){
            if ($(this).text().indexOf(val) == 0)
            {
                count ++;
                $(this).show();
            }
        });
        if (count != 0)
            $('.ballon2').slideDown();
    }).blur(function(){
        $('.ballon2').slideUp();
    }).keyup(function(){
        val = $(this).val();
        $('.ballon2 li').hide();
        var count = 0;
        $('.ballon2 li').each(function(){
            if ($(this).text().indexOf(val) == 0)
            {
                count ++;
                $(this).show();
            }
        });
        if (count == 0)
            $('.ballon2').hide();
        else
            $('.ballon2').show();
    });
    $('.ballon2 a').click(function(){
        alert('aaaa!');
        alert($(this).text());
        $('.search_line input:text').val($(this).text());
        $('.ballon2').slideUp();
        return false;
    });*/



    /* hover on image (add | buy) for ie   */
        $('.item_wrap .top .item').live('mouseover', function() { $(this).children('.add').show(); } );
        $('.item_wrap .top .item').live('mouseout', function() { $(this).children('.add').hide(); } );

    /* hover for add|buy|remove buttons*/
if($.browser.msie){
        if($('inside').length!=0)
        {
            $('.item_wrap .top .item .add ul li').live('mouseover', function(){ $(this).addClass('hover'); } );
            $('.item_wrap .top .item .add ul li').live('mouseout',  function(){ $(this).removeClass('hover'); } );
        }
        else
        {
            $('.item_wrap .top .item .add ul li').bind('mouseover', function(){ $(this).addClass('hover'); } );
            $('.item_wrap .top .item .add ul li').bind('mouseout',  function(){ $(this).removeClass('hover'); } );
        };
}


    /* actions with lightbox */
    $("input.bn[value='Cancel'], input.btn[value='Cancel']").click(function(){
        $('.win_block.add').hide();
                $('.win_block.edit').hide();
                $('.win_block_del').hide();
                $('.win_block.send').hide();
                $('.win_block.add_inside').hide();
                $('.win_block.send_inside').hide();
                return false;
        });


        /* Lightbox page -> Add lightbox */


   /* $('a.comment_edit_link').click(function(){
        $(this).parent().parent().parent().children('.edit2').show();
        return false;
    });

    $('a.comment_remove_link').click(function(){
        $(this).parent().parent().parent().children('.remove').show();
        return false;
    });

    $('.item_wrap ul li.comment').click(function(){
        $('.comment_form.edit2').hide();
        $('.comment_form.remove').hide();
        $(this).parent().parent().children('.edit2').show();
        return false;
    });*/

    $('a.comment_add_link').click(function(){
        $('.comment_form.edit2').hide();
        $('.comment_form.remove').hide();
        $('.comment_form.addc').hide();
        $(this).parent().parent().parent().parent().children('.addc').show();
        return false;
    });


    $('a.comment_remove_link').click(function(){
        $('.comment_form.edit2').hide();
        $('.comment_form.remove').hide();
        $('.comment_form.addc').hide();
        $(this).parent().parent().parent().parent().children('.remove').show();
        $(this).parent().parent().parent().parent().children('.addc').children('form').children('textarea#comments').val('');
        return false;
    });


    $('.comment_form.edit2, .comment_form.remove, .comment_form.addc').click(function(){
        return false;
    });


    /* Characters count at images comments */
    $('.item_wrap .comment_form.edit2 textarea, .item_wrap .comment_form.addc textarea').bind('keydown keypress keyup',function() {
        $(this).parent().children('span').text($(this).val().length);
        if ($(this).val().length > 50)
        {
            $(this).parent().children('strong').css('color', '#c22b4d').css('fontWeight', '700');
        }
        else
        {
            $(this).parent().children('strong').css('color', '#000').css('fontWeight', '400');
        }
    });

    /* Characters count at Edit lightbox */
    sizeControl('.win_block.edit textarea', 100, 'span#edit_characters_count', 'span#edit_maximum');

    /* Characters count at Send lightbox */
    sizeControl('.win_block.send textarea', 100, 'span#send_characters_count', 'span#send_maximum');

    /* Close comment edit dialog */
    $('.edit2 input[value="Cancel"]').click(function(){
        $(this).parent().parent().hide();
        return false;
    });

    /* Close remove comment dialog */
    $('.remove input[value="Cancel"]').click(function(){
        $(this).parent().hide();
        return false;
    });

    /* sign_in on lightbox_notauth */
    $('.message_not_auth a.pseudo').click(function(){
        $('#my_account_block').show();
        bPointer_on_my_account_block = true;
        return false;
    });

    $('window').click(function() { $('#sign_in').hide(); });

    $('#block_win1').click(function(){
        $(this).hide();
        $('#maincontent').css('overflow','auto');
        $('#content_src').attr('src', '');
        $('div.photo_confirm').hide();
       	$('div.music_confirm').hide();
       	$('div.footage_confirm').hide();
    });

    $('#wincont','#win_white_main1, #win_white_main2, #win_white_main3').click(function(){
    	if ($('#win_white_main1').css('display')=='block') {
    		$('div.photo_confirm').hide();
    	}
    	if ($('#win_white_main2').css('display')=='block') {
    		$('div.music_confirm').hide();
    	}
       	if ($('#win_white_main3').css('display')=='block') {
       		$('div.footage_confirm').hide();
       	}

	});


    $('#block_win1 .win_cont').click(function(event){
        event.stopPropagation();
    });

    /* photo balloon outer popup */




        /* paging scroll */
        pages_visible = 16;
        pages_per_click = 5;

        if ($('ul.listing').length > 0)
        {
            $('.listing_cont_inside').width(Math.min( $('.listing_cont_inside').width(), $('ul.listing li').length * ($('ul.listing li').width() - 1) / 2 + 1));
            $('ul.listing').width( $('ul.listing li').length * $('ul.listing li').width() / 2 );
            $('a.arr_l').click(function(){
                if ($('ul.listing:animated').length == 0)
                {
                    if (parseInt($('ul.listing').css('marginLeft')) <= -pages_per_click * 41)
                        $('ul.listing').animate({marginLeft: '+=' + pages_per_click * 41 + 'px' });
                    else if (parseInt($('ul.listing').css('marginLeft')) < 0)
                        $('ul.listing').animate({marginLeft: 0});
                }
            });
            $('a.arr_r').click(function(){
                if ($('ul.listing:animated').length == 0)
                {
                    var len = $('ul.listing li').length * $('ul.listing li').width() + 2 * parseInt($('ul.listing').css('marginLeft'));
                    if (len > (pages_visible + pages_per_click) * 41 * 2)
                        $('ul.listing').animate({marginLeft: '-=' + pages_per_click * 41 + 'px' });
                    else if (len > pages_visible * 41 * 2)
                        $('ul.listing').animate({marginLeft: '-=' + Math.round((len  - (pages_visible + 1) * 2 * 41) / (2 * 41)) * 41 + 'px' });
                }
            });
        }

        if ($('ul.listing2').length > 0)
        {
            $('.listing_cont_inside').width(Math.min( $('.listing_cont_inside').width(), $('ul.listing2 li').length * ($('ul.listing2 li').width() - 1) / 2 + 1));
            $('ul.listing2').width( $('ul.listing2 li').length * $('ul.listing2 li').width() / 2 );
            $('a.arr_l').click(function(){
                if ($('ul.listing2:animated').length == 0)
                {
                    if (parseInt($('ul.listing2').css('marginLeft')) <= -pages_per_click * 41)
                        $('ul.listing2').animate({marginLeft: '+=' + pages_per_click * 41 + 'px' });
                    else if (parseInt($('ul.listing2').css('marginLeft')) < 0)
                        $('ul.listing2').animate({marginLeft: 0});
                }
            });
            $('a.arr_r').click(function(){
                if ($('ul.listing2:animated').length == 0)
                {
                    var len = $('ul.listing2 li').length * $('ul.listing2 li').width() + 2 * parseInt($('ul.listing2').css('marginLeft'));
                    if (len > (pages_visible + pages_per_click) * 41 * 2)
                        $('ul.listing2').animate({marginLeft: '-=' + pages_per_click * 41 + 'px' });
                    else if (len > pages_visible * 41 * 2)
                        $('ul.listing2').animate({marginLeft: '-=' + Math.round((len  - (pages_visible + 1) * 2 * 41) / (2 * 41)) * 41 + 'px' });
                }
            });
         }



        $('#lbout25').click( function(){
                var lbid = $('#lid').val();
                $.cookies.set("outputlb", "25");
                location.href = '/Lightbox/' + lbid;
        })

        $('#lbout50').click( function(){
                var lbid = $('#lid').val();
                $.cookies.set("outputlb", "50");
                location.href = '/Lightbox/' + lbid;
        })

        $('#lbout100').click( function(){
                var lbid = $('#lid').val();
                $.cookies.set("outputlb", "100");
                location.href = '/Lightbox/' + lbid;
        })

        $('#cntout25').click( function(){
                $.cookies.set("outputcnt", "25");
                location.href = '/Content';
        })

        $('#cntout50').click( function(){
                $.cookies.set("outputcnt", "50");
                location.href = '/Content';
        })

        $('#cntout100').click( function(){
                $.cookies.set("outputcnt", "100");
                location.href = '/Content';
        })


/*Hide All*/


/*LG!*/

   if($('#my_account_block').length!=0){$('body').click(function(){$('#my_account_block').hide();});};
   if($('#editorial_popup').length!=0){$('body').click(function(){$('#editorial_popup').hide();});};
   if($('div.ballon').length!=0){$('body').click(function(){$('div.ballon').hide();});};
   if($('#country_list').length!=0){$('body').click(function(){$('#country_list').hide();});};
   if($('.win_block.add').length!=0){$('body').click(function(){$('.win_block.add').hide();});};
   if($('.win_block_del').length!=0){$('body').click(function(){$('.win_block_del').hide();});};
   if($('.win_block.send').length!=0){$('body').click(function(){$('.win_block.send').hide();});};
   if($('.win_block.edit').length!=0){$('body').click(function(){$('.win_block.edit').hide();});};
   if($('.comment_form.edit2').length!=0){$('body').click(function(){$('.comment_form.edit2').hide();});};
   if($('.comment_form.remove').length!=0){$('body').click(function(){$('.comment_form.remove').hide();});};
   if($('#new_lightbox .win_block').length!=0){$('body').click(function(){$('#new_lightbox .win_block').hide();});};
   if($('#send_lightbox .win_block').length!=0){$('body').click(function(){$('#send_lightbox .win_block').hide();});};

$('#editorial_popup').click(function(){
                        checked = $('#editorial_popup input:checkbox:checked').length;
                        if (checked == 0)
                        {
                            $('a#editorial_links').text('Choose');
                            if ($('#editorial_popup>input:checkbox').attr('checked'))
                                $('span#editorial_choose').text('choose');
                            else
                                $('span#editorial_choose').text('change');
                        }
                        else if (checked < 4)
                        {
                            links_text = new Array;
                            $('#editorial_popup input:checkbox:checked + label').each(function() {
                                links_text.push($(this).text());
                            });
                            $('a#editorial_links').text(links_text.join(', ') + ' + ' + $('#editorial_popup .column input:radio:checked + label').text());
                            if ($('#editorial_popup>input:checkbox').attr('checked'))
                                $('span#editorial_choose').text('choose');
                            else
                                $('span#editorial_choose').text('change');
                        }
                        else
                        {
                            $('a#editorial_links').text('change');
                            if ($('#editorial_popup>input:checkbox').attr('checked'))
                                $('span#editorial_choose').text('choose');
                            else
                                $('span#editorial_choose').text('change');
                        };
                   });
 /* /!LG*/

        /* this is for login box. it has dynamic position */
        alignBox();


    /*LG Lightbox functions.*/

        $('.inside3 .item_wrap .add ul').each(function(){
            $(this).children('li:first').click(function(event){
                var lid = $('#alllb').val();
                //alert($('u.menu_main2 li.active').children('a').text());
                if (!lid) {
                	var lid = $('u.menu_main2 li.active a').text();
                }
                var cid = $(this).parent().parent().parent().children('a').children('img').attr("id");
                if (lid && cid){
                    $.ajax({
                        type: "POST",
                        url: '/Lightbox/ajax_delContent',
                        data: 'lid='+lid+'&cid='+cid,
                        success: function(msg){
                            if (msg!=''){
                                    alert(msg);
                            }
                            else{
                                $('#block_win1').hide();
                                $('#content_src').attr('src', '');
                                $('#maincontent').css('overflow','auto');
                                location.reload(true);
                            }
                        }
                	});
                }
                        //$(this).parent().parent().parent().parent().parent().remove();
                        //Здесь должна быть функция удаления фотографии сервера
            });

            $(this).children('li:first').next().click(function(event){
            	var cid = $(this).parent().parent().parent().children('a').children('img').attr("id");
            	var cselected = $('#content div[image_preview="' + cid + '"]').children('.top').children('.item').children('a').children('img').attr('selected_content');
            	if (cselected) {
					if (cselected == 'full') {
						location.href = '/Orders/'+cid;
					}
					else{
						location.href = '/Orders/'+cid+'&contents='+cselected;
					}
				}
				else{
					location.href = '/Orders/'+cid;
				}
            });
        });


	if($('.inside3 .message_auth,.inside3 .message_not_auth').length !=0){
		$('.inside3 .item_wrap .top .item .add ul').each(function(){
			$(this).children('li:first').addClass('disabled_item').unbind().children('a').unbind().click(function(event){
			event.stopPropagation();return false;});
	    });
	    //alert($('#block_win1 #photo_nav_addtolightbox_link').parent().html());
		$('#block_win1 #photo_nav_addtolightbox_link').parent().remove();//addClass('disabled_item').unbind();
		$('#block_win1 #video_nav_addtolightbox_link').parent().remove();
	}

        $('#numok').click(function() {
        var inputpage = parseInt($("#numpage").val());
        var maxpages = parseInt($('#maxpage').val());
        var maxpagesl = ($('#maxpage').val()).length;
        var inputpagel = ($("#numpage").val()).length;
        currentpage = 0;

                url = location.href;
                pospage = url.indexOf('p=');
                if (inputpage > maxpages){
                        currentpage = maxpages;
                }
                else{
                        currentpage = inputpage;
                }
                if (pospage != -1){
                        posbeforepage = url.substr(pospage+2);
                        if ((posbeforepage.length == maxpagesl) && (inputpage > 0) && (maxpages > 0)){
                                location.href = url.substr(0, pospage+2) + currentpage;
                        }
                        else if ((posbeforepage.length == maxpagesl) && (inputpage < 0) && (maxpages > 0)){
                                location.href = url.substr(0, pospage+2) + 1;
                        }
                        else if((posbeforepage.length > maxpagesl) && (maxpages > 0) && (inputpage > 0)){
                                posdop1 = posbeforepage.indexOf('&');
                                posdop2 = posbeforepage.indexOf('#');
                                if (posdop1 <= posdop2) {
                                        if (posdop1 == -1){
                                                posafterpage = posbeforepage.substr(posdop2);
                                        }
                                        else{
                                                posafterpage = posbeforepage.substr(posdop1);
                                        }
                                }
                                else if (posdop1 > posdop2){
                                        if (posdop2 == -1){
                                                posafterpage = posbeforepage.substr(posdop1);
                                        }
                                        else{
                                                posafterpage = posbeforepage.substr(posdop2);
                                        }
                                }
                                location.href = url.substr(0, pospage+2) + currentpage + posafterpage;
                        }
                }
                else{
                        location.href = url + '&p=' + currentpage;
                }
		});

});

function alignBox()
{
    $('#LG-log').css('marginLeft', ($('body').width() * 0.16 < 240 ) ? '-240px' : '-16%' );
}

window.onresize = alignBox;

function sizeControl(textarea, maxsize, count, message)
{
    $(textarea).bind('keydown keypress keyup',function() {
        $(count).text($(this).val().length);
        if ($(this).val().length > maxsize)
        {
            $(message).css('color', '#c22b4d').css('fontWeight', '700');
        }
        else
        {
            $(message).css('color', '#000').css('fontWeight', '400');
        }
    });
}

function in_array(needle, haystack) {
    var found = false;
    for (var i = 0; i < haystack.length; i++) {
    	if (haystack[i] == needle) {
            found = true;
            break;
        }
    }
    return found;
}

function TrimStr(s) {
  s = s.replace( /^\s+/g, '');
  return s.replace( /\s+$/g, '');
}



// функция для проверки нахождения контента в лайтбоксе
/*function tolightbox(content_id){
        var lbid = $('#mylightbox').val();
        $.ajax({
                type: "POST",
                url: '/Lightbox/ajax_contenttolightbox',
                data: 'lid='+lbid+'&cid='+content_id,
                success: function(msg){
                        if (msg == 'true'){
                                $('#photo_nav_addtolightbox_link').text('Remove from lightbox');
                        }
                        else if (msg == 'false') {
                                $('#photo_nav_addtolightbox_link').text('Add to lightbox');
                        }
                        else {
                                alert(msg);
                        }
                }
        });
}*/
